From 03ea6fe7ac8b8be3a784592d515e04e259d33f6f Mon Sep 17 00:00:00 2001 From: Leo Lutz Date: Fri, 31 Jan 2025 22:26:58 -0700 Subject: [PATCH] Add missing HL7::withSegmentEndingFieldSeparator() (#130) --- src/HL7.php | 10 +++++++++- tests/HL7Test.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/HL7.php b/src/HL7.php index 3dc48f1..bd7fc76 100644 --- a/src/HL7.php +++ b/src/HL7.php @@ -134,6 +134,14 @@ public function withFieldSeparator(string $value): self return $this->setGlobal('FIELD_SEPARATOR', $value); } + /** + * Set whether the field separator should be added to the end of each segment. Default: true + */ + public function withSegmentEndingFieldSeparator(bool $value = true): self + { + return $this->setGlobal('WITH_SEGMENT_ENDING_FIELD_SEPARATOR', $value); + } + /** * Set the segment separator to be used by the factory. Should be a single character. Default: \015 * @@ -187,7 +195,7 @@ public function doNotSplitRepetition(bool $value = true): self return $this; } - private function setGlobal(string $name, string $value): self + private function setGlobal(string $name, bool|string $value): self { $this->hl7Globals[$name] = $value; return $this; diff --git a/tests/HL7Test.php b/tests/HL7Test.php index d8718ee..58a85f3 100644 --- a/tests/HL7Test.php +++ b/tests/HL7Test.php @@ -64,8 +64,42 @@ public function hl7_properties_can_be_set_using_chained_methods(): void ->withRepetitionSeparator('`') ->withEscapeCharacter('=') ->withHL7Version('555.666') + ->withSegmentEndingFieldSeparator(false) ->createMessage(); self::assertStringContainsString('MSH#*`=}#', $msg->toString(true)); + self::assertStringEndsWith('555.666[', $msg->toString(true)); + } + + /** + * @test + * @throws HL7Exception + */ + public function withSegmentEndingFieldSeparator_defaults_to_true(): void + { + $msg = HL7::build() + ->withHL7Version('555.666') + ->withSegmentEndingFieldSeparator() + ->createMessage(); + self::assertStringEndsWith('555.666|\n', $msg->toString()); + } + + /** + * @test + * @throws HL7Exception + */ + public function withSegmentEndingFieldSeparator_works_with_values(): void + { + $msg = HL7::build() + ->withHL7Version('555.666') + ->withSegmentEndingFieldSeparator(true) + ->createMessage(); + self::assertStringEndsWith('555.666|\n', $msg->toString()); + + $msg = HL7::build() + ->withHL7Version('555.666') + ->withSegmentEndingFieldSeparator(false) + ->createMessage(); + self::assertStringEndsWith('555.666\n', $msg->toString()); } /**