diff --git a/README.md b/README.md
index 78d4913..dbd3da3 100644
--- a/README.md
+++ b/README.md
@@ -5,11 +5,14 @@
-**Important: Minimum supported PHP version has been updated to 8.0
-Last supported versions:
--> PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)
--> PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)
--> 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
+ Last supported versions:
+ - PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)
+ - PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)
+ - 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
@@ -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
+$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']);
diff --git a/src/HL7.php b/src/HL7.php
index 7f6a971..3dc48f1 100644
--- a/src/HL7.php
+++ b/src/HL7.php
@@ -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'] = '&';
diff --git a/src/HL7/Message.php b/src/HL7/Message.php
index d75adb6..11cc51b 100644
--- a/src/HL7/Message.php
+++ b/src/HL7/Message.php
@@ -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;
@@ -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'] ?? '&';
@@ -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
diff --git a/tests/MessageTest.php b/tests/MessageTest.php
index 754f58f..dc601ec 100644
--- a/tests/MessageTest.php
+++ b/tests/MessageTest.php
@@ -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#^~\\#\nABC###xxx#\n",
+ ['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' => false, 'FIELD_SEPARATOR' => '#']
+ );
+ self::assertSame("MSH#^~\\\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
{