Skip to content

Commit

Permalink
Add ability to reset segment array indexes (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored Feb 10, 2025
1 parent 03ea6fe commit b14f53e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ public function reindexSegments(): void
}
}

/**
* After removing segments from the middle of the message, $msg->getSemgnets() returns an array with gaps in the
* keys. This method can be used to re-key the segments array
*/
public function rekeySegmentsInArray(): void
{
$this->segments = array_values($this->segments);
}

/**
* Return a string representation of this message.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Exception;
use InvalidArgumentException;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use PHPUnit\Framework\Attributes\Test;

class MessageTest extends TestCase
{
Expand Down Expand Up @@ -679,4 +680,16 @@ public function reindex_message_segments(): void
self::assertSame(2, $OBXs[1]->getId());
self::assertSame("CWE", $OBXs[1]->getValueType());
}

#[Test] public function it_can_reset_segment_array_keys_after_segments_are_removed(): void
{
$msgObj = new Message("MSH|^~\&|||||||ORU^01||P|2.3.1|\nABC|1|||\nABC|2|||\nABC|3|||");
$segments = $msgObj->getSegmentsByName('ABC');
$msgObj->removeSegment($segments[1]);
self::assertSame([0, 1, 3], array_keys($msgObj->getSegments()));

$msgObj->rekeySegmentsInArray();

self::assertSame([0, 1, 2], array_keys($msgObj->getSegments()));
}
}

0 comments on commit b14f53e

Please sign in to comment.