{"id":17966310,"url":"https://github.com/senaranya/hl7","last_synced_at":"2025-05-14T16:15:22.662Z","repository":{"id":26834921,"uuid":"110927196","full_name":"senaranya/HL7","owner":"senaranya","description":"PHP library for Parsing, Generation and Sending HL7 v2 messages","archived":false,"fork":false,"pushed_at":"2025-03-27T12:10:01.000Z","size":356,"stargazers_count":185,"open_issues_count":19,"forks_count":87,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-13T01:56:51.589Z","etag":null,"topics":["hl7","hl7-builder","hl7-message","hl7-parser","hl7-sending","hl7v2","php-library","php7"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/senaranya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-16T05:27:06.000Z","updated_at":"2025-03-27T12:08:57.000Z","dependencies_parsed_at":"2023-01-14T05:21:51.736Z","dependency_job_id":"b2f0873f-6230-4e96-be36-63fbb315fad0","html_url":"https://github.com/senaranya/HL7","commit_stats":{"total_commits":98,"total_committers":21,"mean_commits":4.666666666666667,"dds":0.4591836734693877,"last_synced_commit":"98c0053697851ebb6c49bf9cdd3a855f09fcbfc2"},"previous_names":[],"tags_count":63,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senaranya%2FHL7","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senaranya%2FHL7/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senaranya%2FHL7/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senaranya%2FHL7/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/senaranya","download_url":"https://codeload.github.com/senaranya/HL7/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654049,"owners_count":21140235,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hl7","hl7-builder","hl7-message","hl7-parser","hl7-sending","hl7v2","php-library","php7"],"created_at":"2024-10-29T13:09:14.632Z","updated_at":"2025-04-13T01:57:21.084Z","avatar_url":"https://github.com/senaranya.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/senaranya/HL7/actions\"\u003e\u003cimg src=\"https://github.com/senaranya/hl7/actions/workflows/main_ci.yml/badge.svg?branch=master\" alt=\"CI Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/aranyasen/hl7\"\u003e\u003cimg src=\"https://poser.pugx.org/aranyasen/hl7/downloads\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/aranyasen/hl7\"\u003e\u003cimg src=\"https://poser.pugx.org/aranyasen/hl7/v/stable\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/aranyasen/hl7\"\u003e\u003cimg src=\"https://poser.pugx.org/aranyasen/hl7/license\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Important\n- Minimum supported PHP version has been updated to 8.0 \u003cbr\u003e\n    Last supported versions: \u003cbr\u003e\n    - PHP 7.0 or 7.1 =\u003e [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)\u003cbr\u003e\n    - PHP 7.2 =\u003e [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)\u003cbr\u003e\n    - PHP 7.4 =\u003e [2.1.7](https://github.com/senaranya/HL7/tree/2.1.7)\n- The global setting `SEGMENT_ENDING_BAR` is deprecated and will be removed in a future release. Use\n`WITH_SEGMENT_ENDING_FIELD_SEPARATOR` instead.\n\n## Introduction\n\nA PHP-based HL7 v2.x Parsing, Generation and Sending library, inspired from the famous Perl Net-HL7 package.\n\n## Installation\n\n```bash\ncomposer require aranyasen/hl7\n```\n\n## Usage\n### Import library\n```php\n// First, import classes from the library as needed...\nuse Aranyasen\\HL7; // HL7 factory class\nuse Aranyasen\\HL7\\Message; // If Message is used\nuse Aranyasen\\HL7\\Segment; // If Segment is used\nuse Aranyasen\\HL7\\Segments\\MSH; // If MSH is used\n// ... and so on\n```\n\n### Parsing\n```php\n// Create a Message object from a HL7 string\n$message = HL7::from(\"MSH|^~\\\\\u0026|1|\")-\u003ecreateMessage(); // Returns Message object\n\n// Or, using Message class...\n$message = new Message(\"MSH|^~\\\\\u0026|1|\\rPID|||abcd|\\r\"); // Either \\n or \\r can be used as segment endings\n\n// Get string form of the message\necho $message-\u003etoString(true);\n\n// Extracting segments and fields from a Message object...\n$message-\u003egetSegmentByIndex(1); // Get the first segment\n$message-\u003egetSegmentsByName('ABC'); // Get an array of all 'ABC' segments\n$message-\u003egetFirstSegmentInstance('ABC'); // Returns the first ABC segment. Same as $message-\u003egetSegmentsByName('ABC')[0];\n\n// Check if a segment is present in the message object\n$message-\u003ehasSegment('ABC'); // return true or false based on whether PID is present in the $message object\n\n// Check if a message is empty\n$message = new Message();\n$message-\u003eisempty(); // Returns true\n```\n\n### Composing new messages\n```php\n// The class `HL7` can be used to build HL7 object. It is a factory class with various helper methods to help build a hl7.\n$message = HL7::build()-\u003ecreateMessage(); // Creates an empty message\n\n// The HL7 factory class provides methods that can be chained together in a fluent fashion\n$message = HL7::build()\n    -\u003ewithComponentSeparator('#')\n    -\u003ewithFieldSeparator('-')\n    -\u003ecreateMessage();\n\n// Or, using Message class...\n$message = new Message();\n```\n#### Message constructor parameters\n```php\n// When a message is composed using Message class, there are multiple parameters available to define the properties of the HL7.\n// Note: All of these properties are available as fluent methods in HL7 factory class (shown above). So it's recommended to use that for readability\n\n// Creating multiple message objects may have an unexpected side effect: segments start with wrong index values (Check tests/MessageTest for explanation)...\n// Use 4th argument as true, or call resetSegmentIndices() on $message object to reset segment indices to 1\n$message = new Message(\"MSH|^~\\\u0026|||||||ORM^O01||P|2.3.1|\", null, true, true);\n// ... any segments added here will now start index from 1, as expected.\n```\n```php\n// Sometimes you may want to have exact index values, rather than auto-incrementing for each instance of a segment\n// Use 5th argument as false...\n$hl7String = \"MSH|^~\\\u0026|||||||ORU^R01|00001|P|2.3.1|\\n\" . \"OBX|1||11^AA|\\n\" . \"OBX|1||22^BB|\\n\";\n$message = new Message($hl7String, null, true, true, false); $// $message contains both OBXs with given indexes in the string\n```\n```php\n// Create a segment with empty sub-fields retained\n$message = new Message(\"MSH|^~\\\\\u0026|1|\\rPV1|1|O|^AAAA1^^^BB|\", null, true); // Third argument 'true' forces to keep all sub-fields\n$pv1 = $message-\u003egetSegmentByIndex(1);\n$fields = $pv1-\u003egetField(3); // $fields is ['', 'AAAA1', '', '', 'BB']\n\n// Create/send message with segment-ending field-separator character (default \"|\") removed\n$message = new Message(\"MSH|^~\\\\\u0026|1|\\nABC|||xxx\\n\", ['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' =\u003e false]);\n$message-\u003etoString(true); // Returns \"MSH|^~\\\u0026|1\\nABC|||xxx\\n\"\n(new Connection($ip, $port))-\u003esend($message); // Sends the message without ending field-separator character (details on Connection below)\n\n// Specify custom values for separators, HL7 version etc.\n$message = new Message(\"MSH|^~\\\\\u0026|1|\\rPV1|1|O|^AAAA1^^^BB|\", ['SEGMENT_SEPARATOR' =\u003e '\\r\\n', 'HL7_VERSION' =\u003e '2.3']);\n\n// Segment with separator character (~) creates sub-arrays containing each sub-segment\n$message = new Message(\"MSH|^~\\\u0026|||||||ADT^A01||P|2.3.1|\\nPID|||3^0~4^1\"); // Creates [[3,0], [4,1]]\n\n// To create a single array instead, pass 'true' as 6th argument. This may be used to retain behavior from previous releases\n// Notice: Since this leads to a non-standard behavior, it may be removed in future\n$message = new Message(\"MSH|^~\\\u0026|||||||ADT^A01||P|2.3.1|\\nPID|||3^0~4^1\", null, false, false, true, true); // Creates ['3', '0~4', '1']\n// or\n$message = new Message(\"MSH|^~\\\u0026|||||||ADT^A01||P|2.3.1|\\nPID|||3^0~4^1\", doNotSplitRepetition: true); // Creates ['3', '0~4', '1']\n```\n\n#### Handling segments and fields\n```php\n// Once a message object is created, we can now add, insert, set segments and fields.\n\n// Create a MSH segment and add to message object\n$msh = new MSH();\n$message-\u003eaddSegment($msh); // Message is: \"MSH|^~\\\u0026|||||20171116140058|||2017111614005840157||2.3|\\n\"\n\n// Create a custom segment\n$abc = new Segment('ABC');\n$abc-\u003esetField(1, 'xyz');\n$abc-\u003esetField(2, 0);\n$abc-\u003esetField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty\n$abc-\u003eclearField(2); // Clear the value from field 2\n$message-\u003esetSegment($abc, 1); // Message is now: \"MSH|^~\\\u0026|||||20171116140058|||2017111614005840157||2.3|\\nABC|xyz|\\n\"\n\n// Create a defined segment (To know which segments are defined in this package, look into Segments/ directory)\n// Advantages of defined segments over custom ones (shown above) are 1) Helpful setter methods, 2) Auto-incrementing segment index\n$pid = new PID(); // Automatically creates PID segment, and adds segment index at PID.1\n$pid-\u003esetPatientName([$lastname, $firstname, $middlename, $suffix]); // Use a setter method to add patient's name at standard position (PID.5)\n$pid-\u003esetField('abcd', 5); // Apart from standard setter methods, you can manually set a value at any position too\nunset($pid); // Destroy the segment and decrement the id number. Useful when you want to discard a segment.\n\n// It is possible that segments get added in a way that the Set IDs/Sequence IDs within the message are not in order or leave gaps. To reset all Set/Sequence IDs in the message:\n\n$message-\u003ereindexSegments();\n```\n\n### Send messages to remote listeners\n\nSide note: In order to run Connection you need to install PHP ext-sockets [https://www.php.net/manual/en/sockets.installation.php](https://www.php.net/manual/en/sockets.installation.php)\n\n```php\n$ip = '127.0.0.1'; // An IP\n$port = '12001'; // And Port where a HL7 listener is listening\n$message = new Message($hl7String); // Create a Message object from your HL7 string\n\n// Create a Socket and get ready to send message. Optionally add timeout in seconds as 3rd argument (default: 10 sec)\n$connection = new Connection($ip, $port);\n$response = $connection-\u003esend($message); // Send to the listener, and get a response back\necho $response-\u003etoString(true); // Prints ACK from the listener\n```\n### ACK\nHandle ACK message returned from a remote HL7 listener...\n```php\n$ack = (new Connection($ip, $port))-\u003esend($message); // Send a HL7 to remote listener\n$returnString = $ack-\u003etoString(true);\nif (strpos($returnString, 'MSH') === false) {\n    echo \"Failed to send HL7 to 'IP' =\u003e $ip, 'Port' =\u003e $port\";\n}\n$msa = $ack-\u003egetFirstSegmentInstance('MSA');\n$ackCode = $msa-\u003egetAcknowledgementCode();\nif ($ackCode[1] === 'A') {\n    echo \"Received ACK from remote\\n\";\n}\nelse {\n    echo \"Received NACK from remote\\n\";\n    echo \"Error text: \" . $msa-\u003egetTextMessage();\n}\n```\nCreate an ACK response from a given HL7 message:\n```php\n$msg = new Message(\"MSH|^~\\\\\u0026|1|\\rABC|1||^AAAA1^^^BB|\", null, true);\n$ackResponse = new ACK($msg);\n```\nOptions can be passed while creating ACK object:\n```php\n$msg = new Message(\"MSH|^~\\\\\u0026|1|\\rABC|1||^AAAA1^^^BB|\", null, true);\n$ackResponse = new ACK($msg, null, ['SEGMENT_SEPARATOR' =\u003e '\\r\\n', 'HL7_VERSION' =\u003e '2.5']);\n```\n\n## APIs\nThis package exposes a number of public methods for convenient HL7 handling. Some examples are:\n1) Considering you have a Message object (say, `$msg = new Message(file_get_contents('somefile.hl7'));`)\n```php\n    $msg-\u003etoFile('/path/to/some.hl7'); // Write to a file\n    $msg-\u003eisOru(); // Check if it's an ORU\n    $msg-\u003eisOrm(); // Check if it's an ORM\n```\n\nVisit [docs\\README](docs/README.md) for details on available APIs\n\nAll segment level getter/setter APIs can be used in two ways -\n* If a position index isn't provided as argument (1st argument for getters, 2nd for setters), a standard index is used.\n`$pid-\u003esetPatientName('John Doe')` -\u003e Set patient name at position 5 as per HL7 v2.3 [standard](https://corepointhealth.com/resource-center/hl7-resources/hl7-pid-segment)\n`$pid-\u003egetPatientAddress()` -\u003e Get patient address from standard 11th position\n\n\n* To use a custom position index, provide it in the argument:\n`$pid-\u003esetPatientName('John Doe', 6)` -\u003e Set patient name at 6th position in PID segment\n`$pid-\u003egetPatientAddress(12)` -\u003e Get patient address from 12th position\n\n### Issues\nBug reports and feature requests can be submitted on the [Github Issue Tracker](https://github.com/senaranya/HL7/issues).\n\n### Contributing\nSee [CONTRIBUTING.md](CONTRIBUTING.md) for information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsenaranya%2Fhl7","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsenaranya%2Fhl7","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsenaranya%2Fhl7/lists"}