Skip to content

Commit

Permalink
Merge pull request #31 from Jaimies/master
Browse files Browse the repository at this point in the history
Nicer access to line items
  • Loading branch information
Chemaclass committed Nov 9, 2022
2 parents ef62cd9 + ba9af0d commit 6ab55ff
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 32 deletions.
28 changes: 28 additions & 0 deletions src/HasRetrievableSegments.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace EdifactParser;

use EdifactParser\Segments\SegmentInterface;

trait HasRetrievableSegments
{
/**
* @return array<string, array<string, SegmentInterface>>
*/
abstract public function allSegments(): array;

/**
* @return array<string,SegmentInterface>
*/
public function segmentsByTag(string $tag): array
{
return $this->allSegments()[$tag] ?? [];
}

public function segmentByTagAndSubId(string $tag, string $subId): ?SegmentInterface
{
return $this->allSegments()[$tag][$subId] ?? null;
}
}
24 changes: 24 additions & 0 deletions src/LineItem.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace EdifactParser;

use EdifactParser\Segments\SegmentInterface;

final class LineItem
{
use HasRetrievableSegments;

/**
* @param array<string, array<string, SegmentInterface>> $data
*/
public function __construct(private array $data)
{
}

public function allSegments(): array
{
return $this->data;
}
}
9 changes: 8 additions & 1 deletion src/MessageDataBuilder/Builder.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\MessageDataBuilder;

use EdifactParser\LineItem;
use EdifactParser\Segments\LINLineItem;
use EdifactParser\Segments\SegmentInterface;
use EdifactParser\Segments\UNSSectionControl;
Expand Down Expand Up @@ -40,9 +41,15 @@ public function buildSegments(): array
return $this->buildWhereBuilderIs(SimpleBuilder::class);
}

/**
* @returns array<LineItem>
*/
public function buildLineItems(): array
{
return $this->buildWhereBuilderIs(DetailsSectionBuilder::class);
return array_map(
static fn ($data) => new LineItem($data),
$this->buildWhereBuilderIs(DetailsSectionBuilder::class),
);
}

/**
Expand Down
27 changes: 12 additions & 15 deletions src/TransactionMessage.php
Expand Up @@ -12,9 +12,11 @@
/** @psalm-immutable */
final class TransactionMessage
{
use HasRetrievableSegments;

/**
* @param array<string, array<string,SegmentInterface>> $groupedSegments
* @param array<string, array<string, array<string, SegmentInterface>>> $lineItems
* @param array<string, array<string, SegmentInterface>> $groupedSegments
* @param array<string, LineItem> $lineItems
*/
public function __construct(
private array $groupedSegments,
Expand Down Expand Up @@ -49,29 +51,24 @@ public static function groupSegmentsByMessage(SegmentInterface ...$segments): ar
}

/**
* @return array<string, array<string,SegmentInterface>>
* @return array<string, LineItem>
*/
public function allSegments(): array
{
return $this->groupedSegments;
}

public function lineItems(): array
{
return $this->lineItems;
}

/**
* @return array<string,SegmentInterface>
*/
public function segmentsByTag(string $tag): array
public function lineItemById(string|int $lineItemId): ?LineItem
{
return $this->groupedSegments[$tag] ?? [];
return $this->lineItems[(string) $lineItemId] ?? null;
}

public function segmentByTagAndSubId(string $tag, string $subId): ?SegmentInterface
/**
* @return array<string, array<string,SegmentInterface>>
*/
public function allSegments(): array
{
return $this->groupedSegments[$tag][$subId] ?? null;
return $this->groupedSegments;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/EdifactParserTest.php
Expand Up @@ -147,7 +147,7 @@ public function handles_unknown_segments_in_line_items(): void
$transactionResult = EdifactParser::createWithDefaultSegments()->parse($fileContent);
$message = $transactionResult[0];

self::assertNotNull($message->lineItems()[1]['UNK']['first']);
self::assertNotNull($message->lineItems()[2]['UNK']['first']);
self::assertNotNull($message->lineItemById(1)?->segmentByTagAndSubId('UNK', 'first'));
self::assertNotNull($message->lineItemById(2)?->segmentByTagAndSubId('UNK', 'first'));
}
}
13 changes: 7 additions & 6 deletions tests/Unit/MessageBuilder/MessageBuilderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Tests\Unit\MessageBuilder;

use EdifactParser\LineItem;
use EdifactParser\MessageDataBuilder\Builder as MessageDataBuilder;
use EdifactParser\Segments\DTMDateTimePeriod;
use EdifactParser\Segments\LINLineItem;
Expand Down Expand Up @@ -103,14 +104,14 @@ public function build_line_items(): void
], $builder->buildSegments());

self::assertEquals([
'1' => [
'1' => new LineItem([
'LIN' => ['1' => $this->lineSegment],
'QTY' => ['21' => $this->quantitySegment],
],
'2' => [
]),
'2' => new LineItem([
'LIN' => ['2' => $this->otherLineSegment],
'QTY' => ['23' => $this->otherQuantitySegment],
],
]),
], $builder->buildLineItems());
}

Expand All @@ -133,13 +134,13 @@ public function groups_segments_within_line_items(): void
], $builder->buildSegments());

self::assertEquals([
'1' => [
'1' => new LineItem([
'LIN' => ['1' => $this->lineSegment],
'QTY' => [
'21' => $this->quantitySegment,
'23' => $this->otherQuantitySegment,
],
],
]),
], $builder->buildLineItems());
}
}
25 changes: 17 additions & 8 deletions tests/Unit/TransactionMessageTest.php
Expand Up @@ -5,6 +5,7 @@
namespace EdifactParser\Tests\Unit;

use EDI\Parser;
use EdifactParser\LineItem;
use EdifactParser\SegmentList;
use EdifactParser\Segments\CNTControl;
use EdifactParser\Segments\LINLineItem;
Expand Down Expand Up @@ -278,16 +279,24 @@ public function line_items(): void
$messages = $this->transactionMessages($fileContent);
$firstMessage = reset($messages);

$firstLineItem = new LineItem([
'LIN' => ['1' => new LINLineItem(['LIN', 1])],
'QTY' => ['25' => new QTYQuantity(['QTY', [25, 5]])],
]);

$secondLineItem = new LineItem([
'LIN' => ['2' => new LINLineItem(['LIN', 2])],
'QTY' => ['23' => new QTYQuantity(['QTY', [23, 10]])],
]);

self::assertEquals([
'1' => [
'LIN' => ['1' => new LINLineItem(['LIN', 1])],
'QTY' => ['25' => new QTYQuantity(['QTY', [25, 5]])],
],
'2' => [
'LIN' => ['2' => new LINLineItem(['LIN', 2])],
'QTY' => ['23' => new QTYQuantity(['QTY', [23, 10]])],
],
'1' => $firstLineItem,
'2' => $secondLineItem,
], $firstMessage->lineItems());

self::assertEquals($firstLineItem, $firstMessage->lineItemById(1));
self::assertEquals($secondLineItem, $firstMessage->lineItemById(2));
self::assertNull($firstMessage->lineItemById(3));
}

private function transactionMessages(string $fileContent): array
Expand Down

0 comments on commit 6ab55ff

Please sign in to comment.