Skip to content

Commit

Permalink
Bump min PHP to 8.2. Misc post-rebase changes
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya committed Jan 6, 2025
1 parent 4c1da02 commit 585cedf
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1', '8.2' ]
php-versions: [ '8.2', '8.3', '8.4' ]
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
steps:
- name: Checkout
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": ">=8.1",
"php": ">=8.2",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
105 changes: 97 additions & 8 deletions src/HL7/SegmentManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ trait SegmentManagerTrait
*/
public function getSegmentAsString(int $index): ?string
{
$seg = $this->getSegmentByIndex($index);
$segment = $this->getSegmentByIndex($index);

if ($seg === null) {
if ($segment === null) {
return null;
}

return $this->segmentToString($seg);
return $this->segmentToString($segment);
}

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ public function addSegment(Segment $segment): bool
* @param bool $replace Replace the current segment in given $index
* @throws HL7Exception
*/
public function insertSegment(Segment $segment, int $index = null, bool $replace = false): void
public function insertSegment(Segment $segment, ?int $index = null, bool $replace = false): void
{
if ($index > count($this->segments)) {
throw new HL7Exception("Index '$index' greater than the number of total segments " .
Expand Down Expand Up @@ -290,15 +290,14 @@ public function getSegments(): array
return $this->segments;
}


/**
* Convert Segment object to string
*/
public function segmentToString(Segment $seg): string
public function segmentToString(Segment $segment): string
{
$segmentName = $seg->getName();
$segmentName = $segment->getName();
$segmentString = $segmentName . $this->fieldSeparator;
$fields = $seg->getFields(($segmentName === 'MSH' ? 2 : 1));
$fields = $segment->getFields(($segmentName === 'MSH' ? 2 : 1));

foreach ($fields as $field) {
if (is_array($field)) {
Expand Down Expand Up @@ -356,4 +355,94 @@ private function getSegmentClass(string $segmentName, array $fields, bool $autoI

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

/**
* Return an array of all segments with the given subclass of Segment
*
* @param string $segmentClass Segment class
* @return array List of segments identified by class
* @throws HL7Exception
*/
public function getSegmentsByClass(string $segmentClass): array
{
if (!is_subclass_of($segmentClass, Segment::class)) {
throw new HL7Exception("$segmentClass is not a subclass of " . Segment::class);
}
$segmentsByClass = [];

foreach ($this->segments as $segment) {
if ($segment instanceof $segmentClass) {
$segmentsByClass[] = $segment;
}
}

return $segmentsByClass;
}

/**
* Remove given segment
*
* @return int Count of segments removed
* @throws HL7Exception
*/
public function removeSegmentsByClass(string $segmentClass): int
{
if (!is_subclass_of($segmentClass, Segment::class)) {
throw new HL7Exception("$segmentClass is not a subclass of " . Segment::class);
}
$count = 0;
foreach ($this->getSegmentsByClass($segmentClass) as $segment) {
$this->removeSegmentByIndex($this->getSegmentIndex($segment));
$count++;
}
return $count;
}

/**
* Reindex all the segments in the message
*/
public function reindexSegments(): void
{
$indexes = [];
foreach ($this->segments as $segment) {
if (method_exists($segment, "setID")) {
if (!array_key_exists($segment->getName(), $indexes)) {
$indexes[$segment->getName()] = 1;
}
$segment->setId($indexes[$segment->getName()]++);
}
}
}

/**
* Return the first segment of the given class in the message
*
* @return mixed|null
* @throws HL7Exception
*/
public function getFirstSegmentInstanceByClass(string $segmentClass): Segment|null
{
if (!$this->hasSegmentOfClass($segmentClass)) {
return null;
}
return $this->getSegmentsByClass($segmentClass)[0];
}

/**
* Check if given segment is present in the message object by class name
* @throws HL7Exception
*/
public function hasSegmentOfClass(string $segmentClass): bool
{
return count($this->getSegmentsByClass($segmentClass)) > 0;
}

/**
* Check if given segment is present in the message object by class name
* @throws HL7Exception
*/
public function hasSegmentByClass(string $segmentClass): bool
{
return count($this->getSegmentsByClass($segmentClass)) > 0;
}
}
2 changes: 1 addition & 1 deletion tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public function segments_can_be_retrieved_by_class(): void
/** @test */
public function reindex_message_segments(): void
{
$message = new Message(autoIncrementIndices: false);
$message = new Message('MSH|^~\&|||||||', autoIncrementIndices: false);
$pid = new PID();
$pid->setId(2);
$message->addSegment($pid);
Expand Down

0 comments on commit 585cedf

Please sign in to comment.