Skip to content

Commit

Permalink
Merge pull request #9 from Chemaclass/segment-tag-renaming
Browse files Browse the repository at this point in the history
Segment tag renaming
  • Loading branch information
Chemaclass committed Jun 4, 2020
2 parents 1238f9c + 624b61e commit 1d257e2
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 80 deletions.
8 changes: 4 additions & 4 deletions docu/segments/README.md
@@ -1,15 +1,15 @@
# Segments
# EDIFACT Segments

## EDIFACT Segment Definition
## Segment definition

EDIFACT Segment is a collection of logically-related data elements in a fixed, defined sequence.

### EDIFACT provides a hierarchical structure for messages
### Hierarchical structure for messages

EDIFACT messages begin with the `Message Header Segment` (UNH) and end with the` Message Trailer Segment` (UNT).
These two segments are the first, and innermost, level of the three levels of “electronic envelopes” within EDIFACT.

EDIFACT Segment contains:
### A Segment contains

* A **three-character alphanumeric** code that identifies the segment. This is called the segment tag.
* Variable length data elements. These can be either simple or composite.
Expand Down
8 changes: 5 additions & 3 deletions example.php
Expand Up @@ -9,6 +9,7 @@
use EdifactParser\Segments\MEADimensions;
use EdifactParser\Segments\NADNameAddress;
use EdifactParser\Segments\PCIPackageId;
use EdifactParser\Segments\SegmentInterface;
use EdifactParser\Segments\UNHMessageHeader;
use EdifactParser\Segments\UNTMessageFooter;
use EdifactParser\TransactionMessage;
Expand Down Expand Up @@ -80,15 +81,16 @@ function printMessage(TransactionMessage $message): void
printSegment($message->segmentByName(UNTMessageFooter::class));
}

/** @var SegmentInterface[] $segments */
function printSegment(array $segments): void
{
$first = $segments[array_key_first($segments)];
print sprintf("> %s:\n", $first->name());
$first = $segments[array_key_first($segments)];
print sprintf("> %s:\n", $first->tag());

foreach ($segments as $segment) {
print sprintf(
" %s |> %s \n",
str_pad($segment->subSegmentKey(), 3),
str_pad($segment->subId(), 3),
json_encode($segment->rawValues())
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/MissingSubId.php
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace EdifactParser\Exception;

final class MissingSubId extends \Exception
{
public function __construct(string $missingId, array $rawValues)
{
parent::__construct("SubId '$missingId' not found in " . json_encode($rawValues));
}
}
13 changes: 0 additions & 13 deletions src/Exception/MissingSubSegmentKey.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Segments/BGMBeginningOfMessage.php
Expand Up @@ -14,12 +14,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawValues[1];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Segments/CNTControl.php
Expand Up @@ -6,7 +6,7 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Exception\MissingSubId;

/** @psalm-immutable */
final class CNTControl implements SegmentInterface
Expand All @@ -18,15 +18,15 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
if (!isset($this->rawValues[1][0])) {
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
throw new MissingSubId('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
Expand Down
8 changes: 4 additions & 4 deletions src/Segments/DTMDateTimePeriod.php
Expand Up @@ -4,7 +4,7 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Exception\MissingSubId;

/** @psalm-immutable */
final class DTMDateTimePeriod implements SegmentInterface
Expand All @@ -16,15 +16,15 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
if (!isset($this->rawValues[1][0])) {
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
throw new MissingSubId('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
Expand Down
4 changes: 2 additions & 2 deletions src/Segments/MEADimensions.php
Expand Up @@ -16,12 +16,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawValues[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Segments/NADNameAddress.php
Expand Up @@ -16,12 +16,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawValues[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Segments/PCIPackageId.php
Expand Up @@ -14,12 +14,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawValues[1];
}
Expand Down
7 changes: 5 additions & 2 deletions src/Segments/SegmentInterface.php
Expand Up @@ -7,9 +7,12 @@
/** @psalm-immutable */
interface SegmentInterface
{
public function name(): string;
/** A three-character alphanumeric code that identifies the segment. */
public function tag(): string;

public function subSegmentKey(): string;
/** The identifier for multiple segments with the same tag. */
public function subId(): string;

/** Variable length data elements. These can be either simple or composite. */
public function rawValues(): array;
}
8 changes: 4 additions & 4 deletions src/Segments/UNHMessageHeader.php
Expand Up @@ -4,7 +4,7 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Exception\MissingSubId;

/** @psalm-immutable */
final class UNHMessageHeader implements SegmentInterface
Expand All @@ -16,15 +16,15 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
if (!isset($this->rawValues[1][0])) {
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
throw new MissingSubId('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
Expand Down
4 changes: 2 additions & 2 deletions src/Segments/UNTMessageFooter.php
Expand Up @@ -14,12 +14,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawValues[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Segments/UnknownSegment.php
Expand Up @@ -14,12 +14,12 @@ public function __construct(array $rawValues)
$this->rawValues = $rawValues;
}

public function name(): string
public function tag(): string
{
return self::class;
}

public function subSegmentKey(): string
public function subId(): string
{
$encodedValues = json_encode($this->rawValues);

Expand Down
4 changes: 2 additions & 2 deletions src/TransactionMessage.php
Expand Up @@ -59,8 +59,8 @@ private static function groupSegmentsByName(SegmentInterface...$segments): self
$return = [];

foreach ($segments as $s) {
$return[$s->name()] ??= [];
$return[$s->name()][$s->subSegmentKey()] = $s;
$return[$s->tag()] ??= [];
$return[$s->tag()][$s->subId()] = $s;
}

return new self($return);
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/TestingSegmentFactory.php
Expand Up @@ -35,12 +35,12 @@ public function __construct(array $rawArray)
$this->rawArray = $rawArray;
}

public function name(): string
public function tag(): string
{
return (string) $this->rawArray[0];
}

public function subSegmentKey(): string
public function subId(): string
{
return (string) $this->rawArray[1];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Segments/BGMBeginningOfMessageTest.php
Expand Up @@ -15,8 +15,8 @@ public function segmentValues(): void
$rawValues = ['BGM', '340', '00250559268149700889', '9'];
$segment = new BGMBeginningOfMessage($rawValues);

self::assertEquals(BGMBeginningOfMessage::class, $segment->name());
self::assertEquals('340', $segment->subSegmentKey());
self::assertEquals(BGMBeginningOfMessage::class, $segment->tag());
self::assertEquals('340', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}
}
12 changes: 6 additions & 6 deletions tests/Unit/Segments/CNTControlTest.php
Expand Up @@ -4,7 +4,7 @@

namespace EdifactParser\Tests\Unit\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Exception\MissingSubId;
use EdifactParser\Segments\CNTControl;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,16 +16,16 @@ public function segmentValues(): void
$rawValues = ['CNT', ['7', '0.1', 'KGM']];
$segment = new CNTControl($rawValues);

self::assertEquals(CNTControl::class, $segment->name());
self::assertEquals('7', $segment->subSegmentKey());
self::assertEquals(CNTControl::class, $segment->tag());
self::assertEquals('7', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}

/** @test */
public function missingSubSegmentKey(): void
public function missingSubId(): void
{
$segment = new CNTControl(['CNT']);
$this->expectException(MissingSubSegmentKey::class);
$segment->subSegmentKey();
$this->expectException(MissingSubId::class);
$segment->subId();
}
}
12 changes: 6 additions & 6 deletions tests/Unit/Segments/DTMDateTimePeriodTest.php
Expand Up @@ -4,7 +4,7 @@

namespace EdifactParser\Tests\Unit\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Exception\MissingSubId;
use EdifactParser\Segments\DTMDateTimePeriod;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,16 +16,16 @@ public function segmentValues(): void
$rawValues = ['DTM', ['10', '20191002', '102']];
$segment = new DTMDateTimePeriod($rawValues);

self::assertEquals(DTMDateTimePeriod::class, $segment->name());
self::assertEquals('10', $segment->subSegmentKey());
self::assertEquals(DTMDateTimePeriod::class, $segment->tag());
self::assertEquals('10', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}

/** @test */
public function missingSubSegmentKey(): void
public function missingSubId(): void
{
$segment = new DTMDateTimePeriod(['DTM']);
$this->expectException(MissingSubSegmentKey::class);
$segment->subSegmentKey();
$this->expectException(MissingSubId::class);
$segment->subId();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Segments/MEADimensionsTest.php
Expand Up @@ -15,8 +15,8 @@ public function segmentValues(): void
$rawValues = ['MEA', 'WT', 'G', ['KGM', '0.1']];
$segment = new MEADimensions($rawValues);

self::assertEquals(MEADimensions::class, $segment->name());
self::assertEquals('WT', $segment->subSegmentKey());
self::assertEquals(MEADimensions::class, $segment->tag());
self::assertEquals('WT', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Segments/NADNameAddressTest.php
Expand Up @@ -26,8 +26,8 @@ public function segmentValues(): void
];
$segment = new NADNameAddress($rawValues);

self::assertEquals(NADNameAddress::class, $segment->name());
self::assertEquals('CZ', $segment->subSegmentKey());
self::assertEquals(NADNameAddress::class, $segment->tag());
self::assertEquals('CZ', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Segments/PCIPackageIdTest.php
Expand Up @@ -15,8 +15,8 @@ public function segmentValues(): void
$rawValues = ['PCI', '18', '00250559268149700889'];
$segment = new PCIPackageId($rawValues);

self::assertEquals(PCIPackageId::class, $segment->name());
self::assertEquals('18', $segment->subSegmentKey());
self::assertEquals(PCIPackageId::class, $segment->tag());
self::assertEquals('18', $segment->subId());
self::assertEquals($rawValues, $segment->rawValues());
}
}

0 comments on commit 1d257e2

Please sign in to comment.