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

Fix SEGMENT_ENDING_BAR not working with a different FIELD_SEPARATOR #129

Merged
merged 9 commits into from
Jan 24, 2025
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
<a href="https://packagist.org/packages/aranyasen/hl7"><img src="https://poser.pugx.org/aranyasen/hl7/license" alt="License"></a>
</p>

**Important: Minimum supported PHP version has been updated to 8.0 <br>
Last supported versions: <br>
-> PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)<br>
-> PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)<br>
-> PHP 7.4 => [2.1.7](https://github.com/senaranya/HL7/tree/2.1.7)**
## Important
- Minimum supported PHP version has been updated to 8.0 <br>
Last supported versions: <br>
- PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)<br>
- PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)<br>
- PHP 7.4 => [2.1.7](https://github.com/senaranya/HL7/tree/2.1.7)
- The global setting `SEGMENT_ENDING_BAR` is deprecated and will be removed in a future release. Use
`WITH_SEGMENT_ENDING_FIELD_SEPARATOR` instead.

## Introduction

Expand Down Expand Up @@ -92,10 +95,10 @@ $message = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true); // Thi
$pv1 = $message->getSegmentByIndex(1);
$fields = $pv1->getField(3); // $fields is ['', 'AAAA1', '', '', 'BB']

// Create/send message with segment-ending bar character (|) removed
$message = new Message("MSH|^~\\&|1|\nABC|||xxx\n", ['SEGMENT_ENDING_BAR' => false]);
// Create/send message with segment-ending field-separator character (default "|") removed
senaranya marked this conversation as resolved.
Show resolved Hide resolved
$message = new Message("MSH|^~\\&|1|\nABC|||xxx\n", ['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' => false]);
$message->toString(true); // Returns "MSH|^~\&|1\nABC|||xxx\n"
(new Connection($ip, $port))->send($message); // Sends the message without ending bar-characters (details on Connection below)
(new Connection($ip, $port))->send($message); // Sends the message without ending field-separator character (details on Connection below)

// Specify custom values for separators, HL7 version etc.
$message = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.3']);
Expand Down
2 changes: 1 addition & 1 deletion src/HL7.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function setGlobal(string $name, string $value): self
private function setDefaults(): void
{
$this->hl7Globals['SEGMENT_SEPARATOR'] = '\n';
$this->hl7Globals['SEGMENT_ENDING_BAR'] = true;
$this->hl7Globals['WITH_SEGMENT_ENDING_FIELD_SEPARATOR'] = true;
$this->hl7Globals['FIELD_SEPARATOR'] = '|';
$this->hl7Globals['COMPONENT_SEPARATOR'] = '^';
$this->hl7Globals['SUBCOMPONENT_SEPARATOR'] = '&';
Expand Down
9 changes: 5 additions & 4 deletions src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Message
protected array $segments = [];

protected string $segmentSeparator;
protected bool $segmentEndingBar; # true, if '|' at end of each segment is needed
protected bool $withSegmentEndingFieldSeparator; # true, if '|' at end of each segment is needed
protected $fieldSeparator;
protected string $componentSeparator;
protected string $subcomponentSeparator;
Expand Down Expand Up @@ -68,7 +68,8 @@ public function __construct(
) {
// Control characters and other HL7 properties
$this->segmentSeparator = $hl7Globals['SEGMENT_SEPARATOR'] ?? '\n';
$this->segmentEndingBar = $hl7Globals['SEGMENT_ENDING_BAR'] ?? true;
$this->withSegmentEndingFieldSeparator =
$hl7Globals['WITH_SEGMENT_ENDING_FIELD_SEPARATOR'] ?? $hl7Globals['SEGMENT_ENDING_BAR'] ?? true;
$this->fieldSeparator = $hl7Globals['FIELD_SEPARATOR'] ?? '|';
$this->componentSeparator = $hl7Globals['COMPONENT_SEPARATOR'] ?? '^';
$this->subcomponentSeparator = $hl7Globals['SUBCOMPONENT_SEPARATOR'] ?? '&';
Expand Down Expand Up @@ -365,8 +366,8 @@ public function toString(bool $pretty = false)
$message = '';
foreach ($this->segments as $segment) {
$segmentString = $this->segmentToString($segment);
if (!$this->segmentEndingBar) {
$segmentString = preg_replace('/\|$/', '', $segmentString);
if (! $this->withSegmentEndingFieldSeparator) {
$segmentString = preg_replace('/' . preg_quote($this->fieldSeparator, '/') . '$/', '', $segmentString);
}
$message .= $segmentString;
$message .= $pretty
Expand Down
16 changes: 16 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ public function segment_ending_bar_can_be_omitted(): void
self::assertSame("MSH|^~\\&|1|\nABC|||xxx|\n", $msg->toString(true), 'Ending bar retains by default');
}

/** @test */
public function segment_ending_character_can_be_omitted(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n");
self::assertSame("MSH|^~\\&|1|\nABC|||xxx|\n", $msg->toString(true), 'Ending bar retains by default');

$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n", ['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' => false]);
self::assertSame("MSH|^~\\&|1\nABC|||xxx\n", $msg->toString(true), 'No ending bar on each segment');

$msg = new Message(
"MSH#^~\\&#1#\nABC###xxx#\n",
['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' => false, 'FIELD_SEPARATOR' => '#']
);
self::assertSame("MSH#^~\\&#1\nABC###xxx\n", $msg->toString(true), 'No ending field separator on each segment');
}

/** @test */
public function segment_index_can_be_retrieved_from_a_message(): void
{
Expand Down
Loading