diff --git a/src/HL7/Segments/MSH.php b/src/HL7/Segments/MSH.php index b196096..d051b8e 100644 --- a/src/HL7/Segments/MSH.php +++ b/src/HL7/Segments/MSH.php @@ -128,26 +128,23 @@ public function setSecurity($value, int $position = 8) * * Sets message type to MSH segment. * - * If trigger event is already set, then it is preserved + * Note: If trigger event is already set, then it is preserved * - * Example: - * - * If field value is ORU^R01 and you call - * - * ``` - * $msh->setMessageType('ORM'); - * ``` + * Example: If field value is ORU^R01 and you call $msh->setMessageType('ORM'), then the new field value will be + * ORM^R01. If it was empty then the new value will be just ORM. * - * Then the new field value will be ORM^R01. - * If it was empty then the new value will be just ORM. - * - * @param string $value + * Ref: https://hl7-definition.caristix.com/v2/HL7v2.5/Fields/MSH.9 + * @param string|array $value */ public function setMessageType($value, int $position = 9): bool { $typeField = $this->getField($position); - if (is_array($typeField) && !empty($typeField[1])) { - $value = [$value, $typeField[1]]; + if (is_array($typeField)) { + if (!empty($typeField[2])) { + $value = [$value, $typeField[1], $typeField[2]]; + } elseif (!empty($typeField[1])) { + $value = [$value, $typeField[1]]; + } } return $this->setField($position, $value); } diff --git a/tests/Segments/MSHTest.php b/tests/Segments/MSHTest.php index 930a9cd..8a23949 100644 --- a/tests/Segments/MSHTest.php +++ b/tests/Segments/MSHTest.php @@ -79,4 +79,38 @@ public function index_2_in_MSH_accepts_only_4_character_strings(): void $msh->setField(2, 'yyyyy'); self::assertSame('xxxx', $msh->getField(2), 'Special fields not changed'); } + + /** @test */ + public function messageType_can_be_set_in_message(): void + { + $msh = new MSH(); + $msh->setMessageType('ORM'); + self::assertSame('ORM', $msh->getField(9)); + $msh->setMessageType('ORM^O01'); + self::assertSame('ORM^O01', $msh->getField(9)); + + $msh = new MSH(); + $msh->setMessageType(['ORU', 'R01']); // For v2.3 + self::assertSame(['ORU', 'R01'], $msh->getField(9)); + + $msh = new MSH(); + $msh->setMessageType(['ORU', 'R01', 'ORU_R01']); // For 2.5 + self::assertSame(['ORU', 'R01', 'ORU_R01'], $msh->getField(9)); + } + + /** @test */ + public function messageType_can_be_changed_in_message(): void + { + // For v2.3... + $msh = new MSH(); + $msh->setMessageType(['ORU', 'R01']); + $msh->setMessageType('ORM'); + self::assertSame(['ORM', 'R01'], $msh->getField(9)); + + // For v2.5... + $msh = new MSH(); + $msh->setMessageType(['ORU', 'R01', 'ORU_R01']); + $msh->setMessageType('ORM'); + self::assertSame(['ORM', 'R01', 'ORU_R01'], $msh->getField(9)); + } }