HL7 icon indicating copy to clipboard operation
HL7 copied to clipboard

Message::toString fails to detect repeated segments.

Open nigulh opened this issue 11 months ago • 3 comments

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.

nigulh avatar Mar 01 '24 17:03 nigulh

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

senaranya avatar Mar 02 '24 08:03 senaranya

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.

nigulh avatar Mar 04 '24 07:03 nigulh

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);
    }

senaranya avatar Mar 04 '24 11:03 senaranya