senaranya/HL7

Issue parsing some valid HL7 message strings

rayalois22 opened this issue · 2 comments

Below is a well-formed HL7 ORU message.

$oruMessageString = "MSH|^~\&|HL7Soup|Instance1|HL7Soup|Instance2|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL|AL
        PID||75675|1478895^4^M10^PA||XTEST^PATIENT^||19591123|M||||||||||||||||||||||
        ORC|RE|F4334|51013174200601|||||^|||||||||||
        OBR|1|F4334|51013174200601|80048^BASIC METABOLIC PANEL|||20060922152300||||||||^^^^^|023901^PACLAB| ||||||^|CH|F|^^|^^^20060922162659^^GHA||^|||^^^^^^ ^^^^|^^^^^^^^^^|^^^^^^^^^^|^^^^^^^^^^||||||||
        OBX|1|NM|84295^SODIUM^GH|1|145|mmol/L|||||F|||20060922152300|GH";  

I have tried parsing the message as follows:

$oruMessage = new \Aranyasen\HL7\Message($oruMessageString);  

But I get an exception with the following message:

// Segment name ' PID' should be 3 characters and in uppercase  

I have traced the exception to the following line:

if ((!$name) || (\strlen($name) !== 3) || (strtoupper($name) !== $name)) {

I have checked the $oruMessageString to ensure there are no unnecessary space characters. I have also validated against other HL7 tools like HL7 Soup to ensure that $oruMessageString is a valid HL7 message string.

I believe that trimming whitespaces around $name in the Segment constructor before

if ((!$name) || (\strlen($name) !== 3) || (strtoupper($name) !== $name)) {
is called will sort out this issue.
If this is okay, I will go ahead and submit a pull request with the fix.
Please advice.
Thank you.

Thank you for opening this issue.

Unfortunately, sanitizing a string should be the job of the caller code. This library can only work with a valid HL7 where the segments are separated with standard segment separators (/r or /n). Ideally a valid separator is \r (0x0D), but this library also supports \n as it's present since beginning.

I understand some tool might sanitize before parsing, but we have to draw the line between a library and full-fledged HL7 tool to keep the goal focused.

@senaranya Thank you for your response.
Well noted.