lampi87 / hl7
2.0.1
2020-11-09 02:29 UTC
Requires
- php: >=7.2
- ext-mbstring: *
- ext-sockets: *
Requires (Dev)
- ext-pcntl: *
- dms/phpunit-arraysubset-asserts: ^0.1.0
- phpunit/phpunit: ^8
README
重要:支持的PHP版本已更新为7.2+。若要与7.0或7.1版本一起使用,请使用之前的版本 1.5.4
简介
基于PHP的HL7 v2.x解析、生成和发送库,灵感来源于著名的Perl Net-HL7包。
安装
composer require aranyasen/hl7
用法
解析
// Create a Message object from a HL7 string $msg = new Message("MSH|^~\\&|1|\rPID|||abcd|\r"); // Either \n or \r can be used as segment endings $pid = $msg->getSegmentByIndex(1); echo $pid->getField(3); // prints 'abcd' echo $msg->toString(true); // Prints entire HL7 string // Get the first segment $msg->getFirstSegmentInstance('PID'); // Returns the first PID segment. Same as $msg->getSegmentsByName('PID')[0]; // Check if a segment is present in the message object $msg->hasSegment('PID'); // return true or false based on whether PID is present in the $msg object // Check if a message is empty $msg = new Message(); $msg->isempty(); // Returns true
创建新消息
// Create an empty Message object, and populate MSH and PID segments... $msg = new Message(); $msh = new MSH(); $msg->addSegment($msh); // Message is: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\n" // Create any custom segment $abc = new Segment('ABC'); $abc->setField(1, 'xyz'); $abc->setField(2, 0); $abc->setField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty $abc->clearField(2); // Clear the value from field 2 $msg->setSegment($abc, 1); // Message is now: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\nABC|xyz|\n" // Create a defined segment (To know which segments are defined in this package, look into Segments/ directory) // Advantages of defined segments over custom ones (shown above) are 1) Helpful setter methods, 2) Auto-incrementing segment index $pid = new PID(); // Automatically creates PID segment, and adds segment index at PID.1 $pid->setPatientName([$lastname, $firstname, $middlename, $suffix]); // Use a setter method to add patient's name at standard position (PID.5) $pid->setField('abcd', 5); // Apart from standard setter methods, you can manually set a value at any position too unset $pid; // Destroy the segment and decrement the id number. Useful when you want to discard a segment.
// Creating multiple message objects may have an unexpected side-effect: segments start with wrong index values (Check tests/MessageTest for explanation)... // Use 4th argument as true, or call resetSegmentIndices() on $msg object to reset segment indices to 1 $msg = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|", null, true, true); // ... any segments added here will now start index from 1, as expected.
// Sometimes you may want to have exact index values, rather than auto-incrementing for each instance of a segment // Use 5th argument as false... $hl7String = "MSH|^~\&|||||||ORU^R01|00001|P|2.3.1|\n" . "OBX|1||11^AA|\n" . "OBX|1||22^BB|\n"; $msg = new Message($hl7String, null, true, true, false); // $msg contains both OBXs with given indexes in the string
// Create a segment with empty sub-fields retained $msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true); // Third argument 'true' forces to keep all sub fields $pv1 = $msg->getSegmentByIndex(1); $fields = $pv1->getField(3); // $fields is ['', 'AAAA1', '', '', 'BB'] // Create/send message with segment-ending bar character (|) removed $msg = new Message("MSH|^~\\&|1|\nABC|||xxx\n", ['SEGMENT_ENDING_BAR' => false]); $msg->toString(true); // Returns "MSH|^~\&|1\nABC|||xxx\n" (new Connection($ip, $port))->send($msg); // Sends the message without ending bar-characters (details on Connection below) // Specify custom values for separators, HL7 version etc. $msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.3']);
将消息发送到远程监听器
$ip = '127.0.0.1'; // An IP $port = '12001'; // And Port where a HL7 listener is listening $message = new Message($hl7String); // Create a Message object from your HL7 string // Create a Socket and get ready to send message. Optionally add timeout in seconds as 3rd argument (default: 10 sec) $connection = new Connection($ip, $port); $response = $connection->send($message); // Send to the listener, and get a response back echo $response->toString(true); // Prints ACK from the listener
确认(ACK)
处理从远程HL7监听器返回的确认(ACK)消息...
$ack = (new Connection($ip, $port))->send($message); // Send a HL7 to remote listener $returnString = $ack->toString(true); if (strpos($returnString, 'MSH') === false) { echo "Failed to send HL7 to 'IP' => $ip, 'Port' => $port"; } $msa = $ack->getSegmentsByName('MSA')[0]; $ackCode = $msa->getAcknowledgementCode(); if ($ackCode[1] === 'A') { echo "Recieved ACK from remote\n"; } else { echo "Recieved NACK from remote\n"; echo "Error text: " . $msa->getTextMessage(); }
从给定的HL7消息创建确认(ACK)响应
$msg = new Message("MSH|^~\\&|1|\rABC|1||^AAAA1^^^BB|", null, true); $ackResponse = new ACK($msg);
创建确认(ACK)对象时可以传递选项
$msg = new Message("MSH|^~\\&|1|\rABC|1||^AAAA1^^^BB|", null, true); $ackResponse = new ACK($msg, ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.5']);
API
此包公开了多个公共方法,以便方便地处理HL7。一些示例包括
- 假设您有一个消息对象(例如,
$msg = new Message(file_get_contents('somefile.hl7'));
)
$msg->toFile('/path/to/some.hl7'); // Write to a file $msg->isOru(); // Check if it's an ORU $msg->isOrm(); // Check if it's an ORM
访问docs\README获取关于可用API的详细信息
所有段级别的getter/setter API可以使用两种方式 -
-
如果没有提供位置索引作为参数(getters的第一个参数,setters的第二个参数),则使用标准索引。
$pid->setPatientName('John Doe')
-> 根据HL7 v2.3 标准设置位置5的患者姓名
$pid->getPatientAddress()
-> 从标准第11个位置获取患者地址 -
要使用自定义位置索引,请将其作为参数提供
$pid->setPatientName('John Doe', 6)
-> 在PID段中第6个位置设置患者姓名
$pid->getPatientAddress(12)
-> 从第12个位置获取患者地址
待办事项
- 数据验证
- 通过正则表达式搜索并返回段/字段/索引
- 定义更多段