From b2ce89eccea76255a44152c9841ac88857404525 Mon Sep 17 00:00:00 2001 From: Aranya Sen Date: Mon, 28 Oct 2019 16:54:08 +0530 Subject: [PATCH] Add helper to check if message is empty --- README.md | 4 ++++ src/HL7/Message.php | 9 ++++++--- src/HL7/MessageHelpersTrait.php | 9 +++++++++ tests/MessageTest.php | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b580c9b..3311d1a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ $msg->getFirstSegmentInstance('PID'); // Returns the first PID segment. Same as // Check if a segment is present in the message object $msg->hasSegment('PID'); // return true or false based on whether PID is present in the $msg object + +// Check if a message is empty +$msg = new Message(); +$msg->isempty(); // Returns true ``` ### Creating new messages diff --git a/src/HL7/Message.php b/src/HL7/Message.php index ee3418a..f1f9451 100644 --- a/src/HL7/Message.php +++ b/src/HL7/Message.php @@ -363,15 +363,18 @@ public function getSegments(): array * @param boolean $pretty Whether to use \n as separator or default (\r). * @return mixed String representation of HL7 message * @access public + * @throws HL7Exception */ public function toString(bool $pretty = false) { - $message = ''; + if (empty($this->segments)) { + throw new HL7Exception('Message contains no data. Can not convert to string'); + } // Make sure MSH(1) and MSH(2) are ok, even if someone has changed these values - $msh = $this->segments[0]; - $this->resetCtrl($msh); + $this->resetCtrl($this->segments[0]); + $message = ''; foreach ($this->segments as $segment) { $segmentString = $this->segmentToString($segment); if (!$this->segmentEndingBar) { diff --git a/src/HL7/MessageHelpersTrait.php b/src/HL7/MessageHelpersTrait.php index 25ad354..989fd15 100644 --- a/src/HL7/MessageHelpersTrait.php +++ b/src/HL7/MessageHelpersTrait.php @@ -167,4 +167,13 @@ public function removeSegment(Segment $segment, bool $reIndex = false): void } } + /** + * Check if the message has any data + * + * @return bool + */ + public function isEmpty(): bool + { + return empty($this->getSegments()); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 78d3397..8ca3d02 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -3,6 +3,7 @@ namespace Aranyasen\HL7\Tests; +use Aranyasen\Exceptions\HL7Exception; use Aranyasen\HL7\Message; use Aranyasen\HL7\Segment; use Aranyasen\HL7\Segments\MSH; @@ -77,6 +78,14 @@ public function message_can_be_converted_to_string(): void 'Pretty print representation of message'); } + /** @test */ + public function toString_method_throws_exception_when_message_empty(): void + { + $msg = new Message(); + $this->expectException(HL7Exception::class); + $msg->toString(); + } + /** @test */ public function fields_and_subfields_can_be_set_properly(): void { @@ -374,4 +383,11 @@ public function first_of_the_given_segment_name_can_be_easily_obtained_using_a_h $this->assertNull($message->getFirstSegmentInstance('XXX'), 'Non existing segment should return null'); } + + /** @test */ + public function message_can_be_verified_as_empty(): void + { + $this->assertTrue((new Message())->isEmpty()); + $this->assertFalse((new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|"))->isEmpty()); + } }