Skip to content

Commit

Permalink
Add helper to check if message is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored and Aranya Sen committed Oct 28, 2019
1 parent 0889117 commit b2ce89e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/HL7/MessageHelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}

0 comments on commit b2ce89e

Please sign in to comment.