Skip to content

Commit

Permalink
Code refactors (#92)
Browse files Browse the repository at this point in the history
Code refactors, add tests
  • Loading branch information
senaranya authored Mar 19, 2023
1 parent 9612811 commit 17d864d
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 110 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
Expand All @@ -27,7 +27,7 @@ jobs:
# run: composer validate --strict
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ $msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", ['SEGMENT_SEPARATOR' =>
// Segment with separator character (~) creates sub-arrays containing each sub-segment
$message = new Message("MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1"); // Creates [[3,0], [4,1]]

// To create a single array instead, pass 'true' as 5th argument. This may be used to retain behavior from previous releases
// To create a single array instead, pass 'true' as 6th argument. This may be used to retain behavior from previous releases
// Notice: Since this leads to a non-standard behavior, it may be removed in future
$message = new Message("MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1", null, false, false, true, true); // Creates ['3', '0~4', '1']
// or
$message = new Message("MSH|^~\&|||||||ADT^A01||P|2.3.1|\nPID|||3^0~4^1", doNotSplitRepetition: true); // Creates ['3', '0~4', '1']
```

### Send messages to remote listeners
Expand Down
46 changes: 19 additions & 27 deletions src/HL7.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Aranyasen;

use Exception;
use InvalidArgumentException;
use Aranyasen\HL7\Message;
use Aranyasen\HL7\Segments\MSH;
Expand All @@ -18,10 +19,7 @@
*/
class HL7
{
/**
* Holds all global HL7 settings.
*/
protected $hl7Globals;
protected array $hl7Globals;

/**
* Create a new instance of the HL7 factory, and set global
Expand All @@ -43,8 +41,8 @@ public function __construct()
* Create a new Message, using the global HL7 variables as defaults.
*
* @param string|null $msgStr Text representation of an HL7 message
* @throws \Exception
* @throws \InvalidArgumentException
* @throws Exception
* @throws InvalidArgumentException
*/
public function createMessage(string $msgStr = null): Message
{
Expand All @@ -53,7 +51,7 @@ public function createMessage(string $msgStr = null): Message

/**
* Create a new MSH segment, using the global HL7 variables as defaults.
* @throws \Exception
* @throws Exception
*/
public function createMSH(): MSH
{
Expand All @@ -65,11 +63,11 @@ public function createMSH(): MSH
*
* @param string $value Component separator char.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setComponentSeparator(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

Expand All @@ -82,11 +80,11 @@ public function setComponentSeparator(string $value): bool
*
* @param string $value Subcomponent separator char.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setSubcomponentSeparator(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

Expand All @@ -99,11 +97,11 @@ public function setSubcomponentSeparator(string $value): bool
*
* @param string $value Repetition separator char.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setRepetitionSeparator(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

Expand All @@ -116,11 +114,11 @@ public function setRepetitionSeparator(string $value): bool
*
* @param string $value Field separator char.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setFieldSeparator(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

Expand All @@ -133,11 +131,11 @@ public function setFieldSeparator(string $value): bool
*
* @param string $value separator char.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setSegmentSeparator(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

Expand All @@ -149,26 +147,20 @@ public function setSegmentSeparator(string $value): bool
*
* @param string $value Escape character.
* @return bool true if value has been set.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setEscapeCharacter(string $value): bool
{
if (\strlen($value) !== 1) {
if (strlen($value) !== 1) {
throw new InvalidArgumentException("Parameter should be of single character. Received: '$value'");
}

return $this->setGlobal('ESCAPE_CHARACTER', $value);
}

/**
* Set the HL7 version to be used by the factory. Default 2.3
*
* @param string HL7 version character.
* @return bool true if value has been set.
*/
public function setHL7Version(string $value): bool
public function setHL7Version(string $hl7Version): bool
{
return $this->setGlobal('HL7_VERSION', $value);
return $this->setGlobal('HL7_VERSION', $hl7Version);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/HL7/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Aranyasen\Exceptions\HL7ConnectionException;
use Aranyasen\Exceptions\HL7Exception;
use Exception;
use ReflectionException;
use Socket;

/**
* Usage:
Expand Down Expand Up @@ -37,10 +37,14 @@
*/
class Connection
{
protected $socket;
protected $timeout;
protected $MESSAGE_PREFIX;
protected $MESSAGE_SUFFIX;
protected Socket $socket;
protected int $timeout;

/** # Octal 13 (Hex: 0B): Vertical Tab */
protected string $MESSAGE_PREFIX = "\013";

/** # 34 (Hex: 1C): file separator character, 15 (Hex: 0D): Carriage return */
protected string $MESSAGE_SUFFIX = "\034\015";

/**
* Creates a connection to a HL7 server, or throws exception when a connection could not be established.
Expand All @@ -56,8 +60,6 @@ public function __construct(string $host, int $port, int $timeout = 10)
throw new HL7ConnectionException('Please install ext-sockets to run Connection');
}
$this->setSocket($host, $port, $timeout);
$this->MESSAGE_PREFIX = "\013"; # Octal 13 (Hex: 0B): Vertical Tab
$this->MESSAGE_SUFFIX = "\034\015"; # 34 (Hex: 1C): file separator character, 15 (Hex: 0D): Carriage return
$this->timeout = $timeout;
}

Expand Down Expand Up @@ -161,9 +163,9 @@ public function send(Message $msg, string $responseCharEncoding = 'UTF-8', bool
}

/*
* Return the socket opened/used by this class
* Return the raw socket opened/used by this class
*/
public function getSocket()
public function getSocket(): Socket
{
return $this->socket;
}
Expand Down
47 changes: 17 additions & 30 deletions src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,24 @@
* ```php $msg->getSegmentByIndex(0) ```
*
* The segment separator defaults to \015. To change this, set the global variable $SEGMENT_SEPARATOR.
*
* @author Aranya Sen
*/
class Message
{
use MessageHelpersTrait;


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

/**
* local value for segment separator
*/
protected $segmentSeparator;
/**
* @var bool Is the bar (|) at the end of each segment required? Default: Yes.
*/
protected $segmentEndingBar;
protected $fieldSeparator;
protected $componentSeparator;
protected $subcomponentSeparator;
protected $repetitionSeparator;
protected $escapeChar;
protected $hl7Version;
protected string $segmentSeparator;
protected bool $segmentEndingBar; # true, if '|' at end of each segment is needed
protected string $fieldSeparator;
protected string $componentSeparator;
protected string $subcomponentSeparator;
protected string $repetitionSeparator;
protected string $escapeChar;
protected string $hl7Version;

/** @var bool|null $doNotSplitRepetition */
protected $doNotSplitRepetition;
// Split (or not) repeated subfields joined by ~. E.g. if true, parses 3^0~4^1 to [3, '0~4', 1]
protected bool $doNotSplitRepetition;

/**
* Constructor for Message. Consider using the HL7 factory to obtain a message instead.
Expand Down Expand Up @@ -77,15 +67,15 @@ public function __construct(
) {
// Control characters and other HL7 properties
$this->segmentSeparator = $hl7Globals['SEGMENT_SEPARATOR'] ?? '\n';
$this->segmentEndingBar = $hl7Globals['SEGMENT_ENDING_BAR'] ?? true; // '|' at end of each segment
$this->segmentEndingBar = $hl7Globals['SEGMENT_ENDING_BAR'] ?? true;
$this->fieldSeparator = $hl7Globals['FIELD_SEPARATOR'] ?? '|';
$this->componentSeparator = $hl7Globals['COMPONENT_SEPARATOR'] ?? '^';
$this->subcomponentSeparator = $hl7Globals['SUBCOMPONENT_SEPARATOR'] ?? '&';
$this->repetitionSeparator = $hl7Globals['REPETITION_SEPARATOR'] ?? '~';
$this->escapeChar = $hl7Globals['ESCAPE_CHAR'] ?? '\\';
$this->hl7Version = $hl7Globals['HL7_VERSION'] ?? '2.3';

$this->doNotSplitRepetition = $doNotSplitRepetition;
$this->doNotSplitRepetition = (bool) $doNotSplitRepetition;

if ($resetIndices) {
$this->resetSegmentIndices();
Expand Down Expand Up @@ -245,7 +235,7 @@ public function removeSegmentsByName(string $segmentName): int
* control characters and hl7 version, based on MSH(1), MSH(2) and MSH(12).
*
* @param int $index Index where segment is set
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setSegment(Segment $segment, int $index): bool
{
Expand All @@ -272,9 +262,9 @@ protected function resetCtrl(Segment $segment): bool
}

if (preg_match('/(.)(.)(.)(.)/', (string) $segment->getField(2), $matches)) {
$this->componentSeparator = $matches[1];
$this->repetitionSeparator = $matches[2];
$this->escapeChar = $matches[3];
$this->componentSeparator = $matches[1];
$this->repetitionSeparator = $matches[2];
$this->escapeChar = $matches[3];
$this->subcomponentSeparator = $matches[4];
}

Expand Down Expand Up @@ -377,10 +367,7 @@ public function resetSegmentIndices(): void
}
}

/**
* @return array|string
*/
private function extractComponentsFromField(string $field, bool $keepEmptySubFields)
private function extractComponentsFromField(string $field, bool $keepEmptySubFields): array|string
{
$pregFlags = $keepEmptySubFields
? 0
Expand Down
8 changes: 5 additions & 3 deletions src/HL7/Messages/ACK.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Aranyasen\HL7\Message;
use Aranyasen\HL7\Segments\MSA;
use Aranyasen\HL7\Segments\MSH;
use Exception;
use InvalidArgumentException;

class ACK extends Message
{
protected $ACK_TYPE;
protected string $ACK_TYPE;

/**
* Usage:
Expand All @@ -24,8 +26,8 @@ class ACK extends Message
* @param Message|null $req
* @param MSH|null $reqMsh
* @param array|null $hl7Globals Set control characters or HL7 properties. e.g., ['HL7_VERSION' => '2.5']
* @throws \Exception
* @throws \InvalidArgumentException
* @throws Exception
* @throws InvalidArgumentException
*/
public function __construct(Message $req = null, MSH $reqMsh = null, array $hl7Globals = null)
{
Expand Down
Loading

0 comments on commit 17d864d

Please sign in to comment.