Aranyasen HL7 PHP Package
repository·master·Indexed 41 days ago
https://github.com/senaranya/hl7A 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).
What's inside Aranyasen HL7
- 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.
Use the NTE segment class
masterTheAranyasen\HL7\Segments\NTEclass 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 extendsAranyasen\HL7\Segment.Use the EQU segment class
masterTheAranyasen\HL7\Segments\EQUclass represents the Equipment segment in HL7 messages. It extendsAranyasen\HL7\Segmentand provides specialized methods to get and set equipment-specific data fields such as alert levels, equipment state, and event timestamps.Use the EVN segment class
masterTheAranyasen\HL7\Segments\EVNclass 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.Use the DG1 segment class
masterThe
Aranyasen\HL7\Segments\DG1class represents the Diagnosis segment in HL7 messages. It extendsAranyasen\HL7\Segmentand 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\Segmentto perform generic operations likesetField,getField, orclearFieldusing index-based access.// Example of using DG1 methods (conceptual) $dg1 = new Aranyasen\HL7\Segments\DG1(); $dg1->setDiagnosisCodeDG1('value'); $dg1->setDiagnosisDescription('description'); echo $dg1->getDiagnosisCodeDG1();Use the Aranyasen\HL7\Message class
masterThe
Aranyasen\HL7\Messageclass 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_SEPARATORvariable.HL7 Message Parsing and Serialization Behavior
masterThe 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 anHL7Exception. - Segment Separators: You can specify a literal segment separator (e.g.,
\r\n) usingwithSegmentSeparator(). - 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.
- Repetitions (e.g.,
- ID Auto-incrementing: By default, repeated segments (like multiple
OBXsegments) have their IDs automatically incremented (1, 2, 3...). You can disable this usingautoIncrementIndices(false)to keep the IDs as they were in the parsed input. - Round-tripping: The library is designed so that
parse -> toString() -> re-parseyields an identical object model.
- Separators: You can override default separators using
Understand the HL7 object model
masterThe library uses a hierarchical object model to represent HL7 v2 messages:
HL7(Factory): The entry point used to createMessageorMSHinstances.Message: A collection of orderedSegmentobjects. 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 sendMessageobjects to remote listeners and receive parsedMessageresponses.ACK: A specializedMessage(extendingMessage) used to build acknowledgment (MSH and MSA segments) responses.
Manage segment ID auto-incrementing
masterPredefined segment classes use a
static $setIdto 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();Use the PID segment class for Patient Identification
masterThe
Aranyasen\HL7\Segments\PIDclass represents the Patient Identification (PID) segment in an HL7 message. It extendsAranyasen\HL7\Segmentand 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^JohnUse the AIG segment class
masterThe
Aranyasen\HL7\Segments\AIGclass 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\Segmentand provides specific methods to get and set fields related to resource identification, quantities, durations, and scheduling offsets.Reindex segments
masterIf 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 asetID.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();