Skip to content

Commit

Permalink
Upgrade minimum php version to 8.0 (#90)
Browse files Browse the repository at this point in the history
* #89 Update minimum php version to 8.0

* Code refactor and cleanup
* Declare strict
* Remove redundant phpdocs
  • Loading branch information
senaranya authored Dec 31, 2022
1 parent f4a1cef commit 9612811
Show file tree
Hide file tree
Showing 36 changed files with 132 additions and 369 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '7.3', '7.4', '8.0', '8.1' ]
php-versions: [ '8.0', '8.1', '8.2' ]
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
steps:
- name: Checkout
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
<a href="https://packagist.org/packages/aranyasen/hl7"><img src="https://poser.pugx.org/aranyasen/hl7/license" alt="License"></a>
</p>

**Important: Supported PHP version has been updated to 7.3+. Last supported versions: for PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2), for PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)**
**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)**

## Introduction

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"minimum-stability": "dev",
"prefer-stable" : true,
"require": {
"php": ">=7.3",
"php": "^8.0",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
31 changes: 13 additions & 18 deletions src/HL7.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Aranyasen;

use InvalidArgumentException;
Expand Down Expand Up @@ -40,8 +42,7 @@ public function __construct()
/**
* Create a new Message, using the global HL7 variables as defaults.
*
* @param string|null Text representation of an HL7 message
* @return Message
* @param string|null $msgStr Text representation of an HL7 message
* @throws \Exception
* @throws \InvalidArgumentException
*/
Expand All @@ -52,8 +53,7 @@ public function createMessage(string $msgStr = null): Message

/**
* Create a new MSH segment, using the global HL7 variables as defaults.
* @return MSH
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function createMSH(): MSH
{
Expand All @@ -64,7 +64,7 @@ public function createMSH(): MSH
* Set the component separator to be used by the factory. Should be a single character. Default ^
*
* @param string $value Component separator char.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setComponentSeparator(string $value): bool
Expand All @@ -81,7 +81,7 @@ public function setComponentSeparator(string $value): bool
* Set the subcomponent separator to be used by the factory. Should be a single character. Default: &
*
* @param string $value Subcomponent separator char.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setSubcomponentSeparator(string $value): bool
Expand All @@ -98,7 +98,7 @@ public function setSubcomponentSeparator(string $value): bool
* Set the repetition separator to be used by the factory. Should be a single character. Default: ~
*
* @param string $value Repetition separator char.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setRepetitionSeparator(string $value): bool
Expand All @@ -115,7 +115,7 @@ public function setRepetitionSeparator(string $value): bool
* Set the field separator to be used by the factory. Should be a single character. Default: |
*
* @param string $value Field separator char.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setFieldSeparator(string $value): bool
Expand All @@ -132,7 +132,7 @@ public function setFieldSeparator(string $value): bool
* Set the segment separator to be used by the factory. Should be a single character. Default: \015
*
* @param string $value separator char.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setSegmentSeparator(string $value): bool
Expand All @@ -148,7 +148,7 @@ public function setSegmentSeparator(string $value): bool
* Set the escape character to be used by the factory. Should be a single character. Default: \
*
* @param string $value Escape character.
* @return boolean true if value has been set.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
*/
public function setEscapeCharacter(string $value): bool
Expand All @@ -164,7 +164,7 @@ public function setEscapeCharacter(string $value): bool
* Set the HL7 version to be used by the factory. Default 2.3
*
* @param string HL7 version character.
* @return boolean true if value has been set.
* @return bool true if value has been set.
*/
public function setHL7Version(string $value): bool
{
Expand All @@ -174,8 +174,8 @@ public function setHL7Version(string $value): bool
/**
* Set the NULL string to be used by the factory.
*
* @param string NULL string.
* @return boolean true if value has been set.
* @param string $value NULL string.
* @return bool true if value has been set.
*/
public function setNull($value): bool
{
Expand All @@ -194,11 +194,6 @@ public function getNull()

/**
* Set the HL7 global variable
*
* @access protected
* @param string $name
* @param string $value
* @return bool
*/
protected function setGlobal(string $name, string $value): bool
{
Expand Down
13 changes: 3 additions & 10 deletions src/HL7/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public function __construct(string $host, int $port, int $timeout = 10)
/**
* Create a client-side TCP socket
*
* @param string $host
* @param int $port
* @param int $timeout Connection timeout
* @throws HL7ConnectionException
*/
Expand Down Expand Up @@ -96,7 +94,7 @@ protected function setSocket(string $host, int $port, int $timeout = 10): void
$result = null;
try {
$result = socket_connect($socket, $host, $port);
} catch (Exception $exception) {
} catch (Exception) {
$this->throwSocketError("Failed to connect to server ($host:$port)");
}
if (!$result) {
Expand All @@ -107,7 +105,6 @@ protected function setSocket(string $host, int $port, int $timeout = 10): void
}

/**
* @param string $message
* @throws HL7ConnectionException
*/
protected function throwSocketError(string $message): void
Expand All @@ -118,14 +115,10 @@ protected function throwSocketError(string $message): void
/**
* Sends a Message object over this connection.
*
* @param Message $msg
* @param string $responseCharEncoding The expected character encoding of the response.
* @param bool $noWait Do no wait for ACK. Helpful for building load testing tools...
* @return Message|null
* @param string $responseCharEncoding The expected character encoding of the response.
* @param bool $noWait Do no wait for ACK. Helpful for building load testing tools...
* @throws HL7ConnectionException
* @throws HL7Exception
* @throws ReflectionException
* @access public
*/
public function send(Message $msg, string $responseCharEncoding = 'UTF-8', bool $noWait = false): ?Message
{
Expand Down
78 changes: 23 additions & 55 deletions src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class Message
{
use MessageHelpersTrait;

/**
* Array holding all segments of this message.
*/
protected $segments;

/** @var array Array holding all segments of this message */
protected array $segments = [];

/**
* local value for segment separator
Expand Down Expand Up @@ -76,9 +75,6 @@ public function __construct(
bool $autoIncrementIndices = true,
bool $doNotSplitRepetition = null
) {
// Array holding the segments
$this->segments = [];

// Control characters and other HL7 properties
$this->segmentSeparator = $hl7Globals['SEGMENT_SEPARATOR'] ?? '\n';
$this->segmentEndingBar = $hl7Globals['SEGMENT_ENDING_BAR'] ?? true; // '|' at end of each segment
Expand Down Expand Up @@ -109,43 +105,22 @@ public function __construct(
$segmentName = array_shift($fields);

foreach ($fields as $j => $field) {
// Skip control field
// Skip control field (i.e. first field in MSH segment)
if ($index === 0 && $j === 0) {
continue;
}

$fields[$j] = $this->extractComponentsFromField($field, $keepEmptySubFields);
}

$segment = null;

// If a class exists for the segment under segments/, (e.g., MSH)
$className = "Aranyasen\\HL7\\Segments\\$segmentName";
if (class_exists($className)) {
if ($segmentName === 'MSH') {
array_unshift($fields, $this->fieldSeparator); # First field for MSH is '|'
$segment = new $className($fields);
} else {
$segment = new $className($fields, $autoIncrementIndices);
}
} else {
$segment = new Segment($segmentName, $fields);
}

if (!$segment) {
trigger_error('Segment not created', E_USER_WARNING);
}
$segment = $this->getSegmentClass($segmentName, $fields, $autoIncrementIndices);

$this->addSegment($segment);
}
}

/**
* Append a segment to the end of the message
*
* @param Segment $segment
* @return bool
* @access public
*/
public function addSegment(Segment $segment): bool
{
Expand All @@ -161,9 +136,8 @@ public function addSegment(Segment $segment): bool
/**
* Insert a segment.
*
* @param Segment $segment
* @param null|int $index Index where segment is inserted
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function insertSegment(Segment $segment, int $index = null): void
{
Expand Down Expand Up @@ -193,7 +167,6 @@ public function insertSegment(Segment $segment, int $index = null): void
* Note: Segment count within the message starts at 0.
*
* @param int $index Index where segment is inserted
* @return Segment
*/
public function getSegmentByIndex(int $index): ?Segment
{
Expand All @@ -204,10 +177,6 @@ public function getSegmentByIndex(int $index): ?Segment
return $this->segments[$index];
}

/**
* @param Segment $segment
* @return int|null
*/
public function getSegmentIndex(Segment $segment): ?int
{
foreach ($this->segments as $ii => $value) {
Expand Down Expand Up @@ -244,8 +213,6 @@ public function getSegmentsByName(string $name): array
* after this one will be moved one index up.
*
* @param int $index Index where segment is removed
* @return boolean
* @access public
*/
public function removeSegmentByIndex(int $index): bool
{
Expand All @@ -259,7 +226,6 @@ public function removeSegmentByIndex(int $index): bool
/**
* Remove given segment
*
* @param string $segmentName
* @return int Count of segments removed
*/
public function removeSegmentsByName(string $segmentName): int
Expand All @@ -278,9 +244,7 @@ public function removeSegmentsByName(string $segmentName): int
* If index is out of range, or not provided, do nothing. Setting MSH on index 0 will re-validate field separator,
* control characters and hl7 version, based on MSH(1), MSH(2) and MSH(12).
*
* @param Segment $segment
* @param int $index Index where segment is set
* @return boolean
* @throws \InvalidArgumentException
*/
public function setSegment(Segment $segment, int $index): bool
Expand All @@ -300,10 +264,6 @@ public function setSegment(Segment $segment, int $index): bool

/**
* After change of MSH, reset control fields
*
* @param Segment $segment
* @return bool
* @access protected
*/
protected function resetCtrl(Segment $segment): bool
{
Expand Down Expand Up @@ -372,8 +332,6 @@ public function toString(bool $pretty = false)

/**
* Convert Segment object to string
* @param Segment $seg
* @return string
*/
public function segmentToString(Segment $seg): string
{
Expand Down Expand Up @@ -404,8 +362,6 @@ public function segmentToString(Segment $seg): string

/**
* Reset index attribute of each given segment, so when those are added the indices start from 1
*
* @return void
*/
public function resetSegmentIndices(): void
{
Expand All @@ -422,8 +378,6 @@ public function resetSegmentIndices(): void
}

/**
* @param string $field
* @param bool $keepEmptySubFields
* @return array|string
*/
private function extractComponentsFromField(string $field, bool $keepEmptySubFields)
Expand All @@ -432,7 +386,7 @@ private function extractComponentsFromField(string $field, bool $keepEmptySubFie
? 0
: PREG_SPLIT_NO_EMPTY;

if ((strpos($field, $this->repetitionSeparator) !== false) && (! $this->doNotSplitRepetition)) {
if ((str_contains($field, $this->repetitionSeparator)) && (! $this->doNotSplitRepetition)) {
$components = preg_split("/\\" . $this->repetitionSeparator . '/', $field, -1, $pregFlags);
$fields = [];
foreach ($components as $index => $component) {
Expand All @@ -459,8 +413,6 @@ private function extractComponentsFromField(string $field, bool $keepEmptySubFie
/**
* Set various separators - segment, field etc.
*
* @param string $msh
* @return void
* @throws HL7Exception
*/
private function setSeparators(string $msh): void
Expand All @@ -485,4 +437,20 @@ private function setSeparators(string $msh): void
$this->escapeChar = $esc;
$this->repetitionSeparator = $repSep;
}

private function getSegmentClass(string $segmentName, array $fields, bool $autoIncrementIndices): Segment
{
// If a class exists for the segment under segments/, (e.g., MSH)
$className = "Aranyasen\\HL7\\Segments\\$segmentName";
if (!class_exists($className)) {
return new Segment($segmentName, $fields);
}

if ($segmentName === 'MSH') {
array_unshift($fields, $this->fieldSeparator); # First field for MSH is '|'
return new $className($fields);
}

return new $className($fields, $autoIncrementIndices);
}
}
Loading

0 comments on commit 9612811

Please sign in to comment.