Skip to content

Commit

Permalink
Add removeSegment() method
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored and Aranya Sen committed Jul 10, 2019
1 parent 791e06d commit 062cf7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
25 changes: 25 additions & 0 deletions src/HL7/MessageHelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,29 @@ public function getFirstSegmentInstance(string $segment)
}
return $this->getSegmentsByName($segment)[0];
}

/**
* Remove a segment from the message
*
* @param Segment $segment
* @param bool $reIndex After deleting, re-index remaining segments of same name
*/
public function removeSegment(Segment $segment, bool $reIndex = false): void
{
if(($key = array_search($segment, $this->segments, true)) !== false) {
unset($this->segments[$key]);
}

if (!$reIndex) {
return;
}

$segments = $this->getSegmentsByName($segment->getName());
$index = 1;
/** @var Segment $seg */
foreach ($segments as $seg) {
$seg->setField(1, $index++);
}
}

}
66 changes: 39 additions & 27 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class MessageTest extends TestCase
{
/** @test */
public function subfields_can_be_retained_when_required()
public function subfields_can_be_retained_when_required(): void
{
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true);
$pv1 = $msg->getSegmentByIndex(1);
Expand All @@ -21,23 +21,21 @@ public function subfields_can_be_retained_when_required()
}

/** @test */
public function segments_can_be_added_to_existing_message()
public function segments_can_be_added_to_existing_message(): void
{
$msg = new Message();
$msg->addSegment(new MSH());
$msg->addSegment(new Segment('PID'));

// When
$seg0 = $msg->getSegmentByIndex(0);
$seg1 = $msg->getSegmentByIndex(1);

// Then
$this->assertSame('MSH', $seg0->getName(), 'Segment 0 name MSH');
$this->assertSame('PID', $seg1->getName(), 'Segment 1 name PID');
}

/** @test */
public function fields_can_be_added_to_existing_segments()
public function fields_can_be_added_to_existing_segments(): void
{
$msg = new Message();
$msg->addSegment(new MSH());
Expand All @@ -54,14 +52,14 @@ public function fields_can_be_added_to_existing_segments()
}

/** @test */
public function control_characters_are_properly_set_from_MSH_Segment()
public function control_characters_are_properly_set_from_MSH_Segment(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|||xxx|\r");
$this->assertSame("^~\\&", $msg->getSegmentByIndex(0)->getField(2), 'Encoding characters');
}

/** @test */
public function control_characters_can_be_customized_using_second_argument()
public function control_characters_can_be_customized_using_second_argument(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n", ['SEGMENT_SEPARATOR' => '\r\n']);
$this->assertSame('MSH|^~\\&|1|\r\nABC|||xxx|\r\n', $msg->toString(), 'Custom line-endings');
Expand All @@ -70,7 +68,7 @@ public function control_characters_can_be_customized_using_second_argument()
}

/** @test */
public function message_can_be_converted_to_string()
public function message_can_be_converted_to_string(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|||xxx|\r");
$this->assertSame('MSH|^~\\&|1|\nABC|||xxx|\n', $msg->toString(), 'String representation of message');
Expand All @@ -79,7 +77,7 @@ public function message_can_be_converted_to_string()
}

/** @test */
public function fields_and_subfields_can_be_set_properly()
public function fields_and_subfields_can_be_set_properly(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|||xx^x&y&z^yy^zz|\r");
$seg1 = $msg->getSegmentByIndex(1);
Expand All @@ -90,7 +88,7 @@ public function fields_and_subfields_can_be_set_properly()
}

/** @test */
public function fields_can_be_separated_by_custom_character()
public function fields_can_be_separated_by_custom_character(): void
{
$msg = new Message("MSH*^~\\&*1\rABC***xxx\r"); // Use * as a field separator

Expand All @@ -102,31 +100,45 @@ public function fields_can_be_separated_by_custom_character()
}

/** @test */
public function components_of_a_field_can_be_separated_by_custom_character()
public function components_of_a_field_can_be_separated_by_custom_character(): void
{
$msg = new Message("MSH|*~\\&|1\rABC|||x*y*z\r");
$field = $msg->getSegmentByIndex(1)->getField(3);
$this->assertSame('x', $field[0], 'Composed field with * as separator between subfields');
}

/** @test */
public function subcomponents_can_be_separated_by_custom_character()
public function subcomponents_can_be_separated_by_custom_character(): void
{
$msg = new Message("MSH|^~\\@|1\rABC|||a^x@y@z^b\r");
$field = $msg->getSegmentByIndex(1)->getField(3);
$this->assertSame('y', $field[1][1], 'Subcomposed field with @ as separator');
}

/** @test */
public function segments_can_be_added_from_message()
public function segments_can_be_added_from_message(): void
{
$msg = new Message();
$msg->addSegment(new Segment('XXX'));
$this->assertSame('XXX', $msg->getSegmentByIndex(0)->getName(), 'Add segment');
}

/** @test */
public function segments_can_be_removed_from_message()
public function segment_can_be_removed_from_message(): void
{
$msg = new Message("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|\nBBB|2|");
$segment = $msg->getFirstSegmentInstance('BBB');
$msg->removeSegment($segment);
$this->assertSame("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|2|\n", $msg->toString(true));

$msg = new Message("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|a|\nBBB|2|b|\nBBB|3|c|");
$segment = $msg->getSegmentsByName('BBB')[1];
$msg->removeSegment($segment, true);
$this->assertSame("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|a|\nBBB|2|c|\n", $msg->toString(true), 'Should reset index of subsequent segments');
}

/** @test */
public function segments_can_be_removed_from_message_using_index(): void
{
$msg = new Message();
$msg->addSegment(new Segment('XXX'));
Expand All @@ -136,7 +148,7 @@ public function segments_can_be_removed_from_message()
}

/** @test */
public function segments_can_be_removed_by_name()
public function segments_can_be_removed_by_name(): void
{
$msg = new Message("MSH|^~\\&|1|\nAAA|1||xxx|\nAAA|2||\nBBB|1|\n");
$count = $msg->removeSegmentsByName('AAA');
Expand All @@ -150,7 +162,7 @@ public function segments_can_be_removed_by_name()
}

/** @test */
public function a_new_segment_can_be_inserted_between_two_existing_segments()
public function a_new_segment_can_be_inserted_between_two_existing_segments(): void
{
$msg = new Message();
$msg->addSegment(new Segment('AAA'));
Expand All @@ -162,7 +174,7 @@ public function a_new_segment_can_be_inserted_between_two_existing_segments()
}

/** @test */
public function it_should_not_be_possible_to_insert_segment_beyond_last_index()
public function it_should_not_be_possible_to_insert_segment_beyond_last_index(): void
{
$msg = new Message();
$this->expectException(InvalidArgumentException::class);
Expand All @@ -171,7 +183,7 @@ public function it_should_not_be_possible_to_insert_segment_beyond_last_index()
}

/** @test */
public function a_segment_can_be_overwritten()
public function a_segment_can_be_overwritten(): void
{
$msg = new Message();
$msg->addSegment(new Segment('AAA'));
Expand All @@ -181,7 +193,7 @@ public function a_segment_can_be_overwritten()
}

/** @test */
public function same_segment_type_can_be_added_multiple_times()
public function same_segment_type_can_be_added_multiple_times(): void
{
$msg = new Message();
$msg->addSegment(new Segment('AAA'));
Expand All @@ -190,7 +202,7 @@ public function same_segment_type_can_be_added_multiple_times()
}

/** @test */
public function field_separator_can_be_changed_after_message_creation()
public function field_separator_can_be_changed_after_message_creation(): void
{
$msg = new Message();
$msh = new MSH([]);
Expand All @@ -206,23 +218,23 @@ public function field_separator_can_be_changed_after_message_creation()
}

/** @test */
public function a_message_can_be_constructed_from_a_string()
public function a_message_can_be_constructed_from_a_string(): void
{
$str = 'MSH|^~\&|Aranya HL7|Aranya HQ|VISION|MISYS|200404061744||DFT^P03|TC-2743|P^T|2.3|||AL|NE||ASCII||| |';
$msg = new Message($str);
$this->assertCount(1, $msg->getSegments(), 'Message from string');
}

/** @test */
public function a_message_can_be_converted_to_a_string()
public function a_message_can_be_converted_to_a_string(): void
{
$str = 'MSH|^~\&|Aranya HL7|Aranya HQ|VISION|MISYS|200404061744||DFT^P03|TC-2743|P^T|2.3|||AL|NE||ASCII||| |';
$msg = new Message($str);
$this->assertSame("$str\n", $msg->toString(true), 'Message to string with subcomponents');
}

/** @test */
public function a_segment_can_be_retrieved_as_a_string()
public function a_segment_can_be_retrieved_as_a_string(): void
{
$msg = new Message("MSH*^~\\&*1\rXXX*a^b^c*a^b1&b2^c*xxx\r");
$this->assertSame("MSH*^~\\&*1*", $msg->getSegmentAsString(0), 'MSH segment as string');
Expand All @@ -242,15 +254,15 @@ public function a_segment_can_be_retrieved_as_a_string()
/**
* @test
*/
public function an_exception_will_be_throw_in_invalid_string()
public function an_exception_will_be_throw_in_invalid_string(): void
{
$this->expectException(\Aranyasen\Exceptions\HL7Exception::class);
$this->expectExceptionMessage('Not a valid message: invalid control segment');
$msg = new Message("I'm an invalid message");
}

/** @test */
public function segment_ending_bar_can_be_omitted()
public function segment_ending_bar_can_be_omitted(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n", ['SEGMENT_ENDING_BAR' => false]);
$this->assertSame("MSH|^~\\&|1\nABC|||xxx\n", $msg->toString(true), 'No ending bar on each segment');
Expand All @@ -260,7 +272,7 @@ public function segment_ending_bar_can_be_omitted()
}

/** @test */
public function segment_index_can_be_retrieved_from_a_message()
public function segment_index_can_be_retrieved_from_a_message(): void
{
$message = new Message("MSH|^~\\&|1|\n");
$pid = new PID();
Expand All @@ -274,7 +286,7 @@ public function segment_index_can_be_retrieved_from_a_message()
}

/** @test */
public function message_type_can_be_checked()
public function message_type_can_be_checked(): void
{
$msg = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|");
$this->assertTrue($msg->isOrm());
Expand Down

0 comments on commit 062cf7a

Please sign in to comment.