HL7
HL7 copied to clipboard
Message::toString fails to detect repeated segments.
public function testfield_with_repetition_separator_can_be_split_into_array(): void
{
$messageStr = "MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1|\n";
$message = new Message($messageStr, autoIncrementIndices: false);
self::assertSame($messageStr, $message->toString(true));
}
This test fails, as it returns 3&0^4&1
instead of 3^0~4^1
. I don't think that doNotSplitRepetition: true is the correct solution here.
This is what doNotSplitRepetition
is meant for. toString()
just dumps how the Message object has interpreted the HL7 string as. If you don't want the repetition to be split in the output of toString()
, you need to tell Message to treat it as a string and not an array, i.e., by using doNotSplitRepetition: true
Thanks for the response, and thanks for the project!
I would expect that 3^0~4^1
is parsed into [[3, 0], [4, 1]]
. If I use doNotSplitRepetition, then it is parsed into [3, 0~4, 1]
, which is not correct in my case.
With toString()
, it'll always convert into string. If you need array you need to fetch the value directly from the field, i.e.
public function testfield_with_repetition_separator_can_be_split_into_array(): void
{
$messageStr = "MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1|\n";
$message = new Message($messageStr, doNotSplitRepetition: false);
$field = $message->getSegmentByIndex(1)->getField(3);
self::assertIsArray($field);
self::assertSame([['3', '0'], ['4', '1']], $field);
}
Alternatively, you can also use the method meant for the given segment's given field, since you're using PID:
public function testfield_with_repetition_separator_can_be_split_into_array(): void
{
$messageStr = "MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1|\n";
$message = new Message($messageStr, doNotSplitRepetition: false);
$patientIdentifierList = $message->getFirstSegmentInstance('PID')->getPatientIdentifierList();
self::assertIsArray($patientIdentifierList);
self::assertSame([['3', '0'], ['4', '1']], $patientIdentifierList);
}