Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MSH.9.3 Message Structure #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/HL7/Segments/MSH.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $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);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Segments/MSHTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}