Aranyasen HL7 PHP Package

repository·master·Indexed 41 days ago

https://github.com/senaranya/hl7

A PHP library for creating, parsing, and transmitting HL7 v2 messages. It features a fluent API for message composition, support for common segments like PID and MSH, and a Connection class for sending messages via sockets and handling Acknowledgments (ACK).

Tokens
52.8K
Snippets
107
Records
194
Agent score
50%

What's inside Aranyasen HL7

  1. Overview of Aranyasen\HL7

    master
    Aranyasen\HL7 is a PHP library designed for working with HL7 (Health Level Seven) messages. It provides tools for parsing existing HL7 messages, composing new messages from scratch, and sending messages to remote listeners. The library supports core HL7 operations including handling Acknowledgments (ACK) and managing connections for message transmission.
  2. Use the NTE segment class

    master
    The Aranyasen\HL7\Segments\NTE class represents an HL7 NTE (Notes and Comments) segment. It is used to store and manage supplemental information, such as comments, their types, and their sources. This class extends Aranyasen\HL7\Segment.
  3. Use the EQU segment class

    master
    The Aranyasen\HL7\Segments\EQU class represents the Equipment segment in HL7 messages. It extends Aranyasen\HL7\Segment and provides specialized methods to get and set equipment-specific data fields such as alert levels, equipment state, and event timestamps.
  4. Use the EVN segment class

    master
    The Aranyasen\HL7\Segments\EVN class represents the Event Type segment in an HL7 message. It provides specialized methods to get and set specific fields related to event timing, types, and operators, abstracting away the raw field indices of the segment.
  5. Use the DG1 segment class

    master

    The Aranyasen\HL7\Segments\DG1 class represents the Diagnosis segment in HL7 messages. It extends Aranyasen\HL7\Segment and provides specialized methods to get and set specific diagnosis-related fields.

    Commonly used fields include:

    • Diagnosis Code
    • Diagnosis Description
    • Diagnosing Clinician
    • Diagnosis Type
    • Diagnostic Related Group (DRG) information
    • Outlier information (Cost, Days, Type)

    You can use the inherited methods from Aranyasen\HL7\Segment to perform generic operations like setField, getField, or clearField using index-based access.

    // Example of using DG1 methods (conceptual)
    $dg1 = new Aranyasen\HL7\Segments\DG1();
    $dg1->setDiagnosisCodeDG1('value');
    $dg1->setDiagnosisDescription('description');
    
    echo $dg1->getDiagnosisCodeDG1();
  6. Use the Aranyasen\HL7\Message class

    master

    The Aranyasen\HL7\Message class represents an HL7 message, serving as both a request and a response.

    Best Practice: While you can instantiate this class directly via the constructor, it is generally recommended to use the HL7 factory class to create message instances instead.

    Key Concept: Segment Indexing When interacting with segments, note that the segment index starts at 0. To access the first segment in a message, use index 0.

    Segment Separators The segment separator defaults to \015 (Carriage Return). To change this globally, set the $SEGMENT_SEPARATOR variable.

  7. HL7 Message Parsing and Serialization Behavior

    master

    The library performs parsing and serialization using pure string processing (regex split/implode) for high performance. Key behaviors include:

    • Separators: You can override default separators using with*Separator('x') methods. Note that these must be single characters; providing multi-character strings will throw an HL7Exception.
    • Segment Separators: You can specify a literal segment separator (e.g., \r\n) using withSegmentSeparator().
    • Repetitions and Subcomponents:
      • Repetitions (e.g., 3^0~4^1) are split into nested arrays.
      • Subcomponents (e.g., A&B) are parsed into nested arrays. Single-component fields remain scalars.
      • By default, empty subfields are dropped (compressed representation). Use keepEmptySubfields() to preserve exact positions.
    • ID Auto-incrementing: By default, repeated segments (like multiple OBX segments) have their IDs automatically incremented (1, 2, 3...). You can disable this using autoIncrementIndices(false) to keep the IDs as they were in the parsed input.
    • Round-tripping: The library is designed so that parse -> toString() -> re-parse yields an identical object model.
  8. Understand the HL7 object model

    master

    The library uses a hierarchical object model to represent HL7 v2 messages:

    • HL7 (Factory): The entry point used to create Message or MSH instances.
    • Message: A collection of ordered Segment objects. It maintains the state of separators (field, component, repetition, etc.) and the HL7 version.
    • Segment: Represents an HL7 segment. Fields are 1-based per the HL7 standard. Index 0 is reserved for the segment name and is immutable.
    • Segments\*: The library provides 30 predefined segment classes (e.g., MSH, PID, OBR, OBX) with named accessors for easier data manipulation.
    • Connection: An MLLP/TCP client used to send Message objects to remote listeners and receive parsed Message responses.
    • ACK: A specialized Message (extending Message) used to build acknowledgment (MSH and MSA segments) responses.
  9. Manage segment ID auto-incrementing

    master

    Predefined segment classes use a static $setId to automatically assign IDs to segments when auto-increment is enabled. This state persists across different message instances within the same process.

    Warning: If you are building multiple messages in a single execution loop, the IDs will continue to increment across messages. To ensure each message starts with fresh IDs, you must reset the indices.

    How to reset indices

    You can reset the counters using either the factory or the message instance:

    // Using the HL7 factory
    $hl7->resetIndices()->create();
    
    // Or on a specific message instance
    $message->resetSegmentIndices();
    // Example of resetting indices to prevent ID leakage across messages
    $hl7 = new Aranyasen\HL7();
    $hl7->resetIndices();
    $message = $hl7->create();
  10. Use the PID segment class for Patient Identification

    master

    The Aranyasen\HL7\Segments\PID class represents the Patient Identification (PID) segment in an HL7 message. It extends Aranyasen\HL7\Segment and provides methods to both retrieve (get) and set (set) various patient-related data fields such as name, ID, birth date, sex, and address. This class is used when parsing existing HL7 messages to extract patient info or when composing new messages to include patient details.

    use Aranyasen\HL7\Segments\PID;
    
    // Example of interacting with a PID segment
    $pid = new PID();
    $pid->setPatientName('Doe^John');
    $pid->setPatientID('12345');
    
    echo $pid->getPatientName(); // Outputs: Doe^John
  11. Use the AIG segment class

    master

    The Aranyasen\HL7\Segments\AIG class represents the AIG (Appointment information - general resource) segment. This segment is used to contain information about various kinds of resources (such as equipment) that can be scheduled and are controlled by a schedule on a schedule filler application.

    It extends Aranyasen\HL7\Segment and provides specific methods to get and set fields related to resource identification, quantities, durations, and scheduling offsets.

  12. Reindex segments

    master

    If you manually modify Set IDs (field 1) or remove segments, you may need to reassign Set IDs sequentially (1…n) per segment name to maintain compliance. Use $message->reindexSegments() to reassign IDs in message order for every segment that has a setID.

    Note: Predefined segments (PID, OBR, etc.) auto-assign Set IDs via static counters. If you build multiple messages in a single process without calling HL7::build()->resetIndices(), the static counters will continue incrementing across messages.

    $message->reindexSegments();