|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EdifactParser; |
|
6
|
|
|
|
|
7
|
|
|
use EdifactParser\Segments\SegmentInterface; |
|
8
|
|
|
use EdifactParser\Segments\UNHMessageHeader; |
|
9
|
|
|
use EdifactParser\Segments\UNTMessageFooter; |
|
10
|
|
|
|
|
11
|
|
|
/** @psalm-immutable */ |
|
12
|
|
|
final class TransactionMessage |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var array<string, array<string,SegmentInterface>> */ |
|
15
|
|
|
private array $groupedSegments; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A transaction message starts with the "UNHMessageHeader" segment and finalizes with |
|
19
|
|
|
* the "UNTMessageFooter" segment, this process is repeated for each pair of segments. |
|
20
|
|
|
* |
|
21
|
|
|
* @psalm-pure |
|
22
|
|
|
* @psalm-return list<TransactionMessage> |
|
23
|
|
|
*/ |
|
24
|
11 |
|
public static function groupSegmentsByMessage(SegmentInterface...$segments): array |
|
25
|
|
|
{ |
|
26
|
11 |
|
$messages = []; |
|
27
|
11 |
|
$groupedSegments = []; |
|
28
|
|
|
|
|
29
|
11 |
|
foreach ($segments as $segment) { |
|
30
|
11 |
|
if ($segment instanceof UNHMessageHeader) { |
|
31
|
11 |
|
$groupedSegments = []; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
11 |
|
$groupedSegments[] = $segment; |
|
35
|
|
|
|
|
36
|
11 |
|
if ($segment instanceof UNTMessageFooter) { |
|
37
|
11 |
|
$messages[] = static::groupSegmentsByName(...$groupedSegments); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
11 |
|
return static::hasUnhSegment(...$messages); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @param array<string, array<string,SegmentInterface>> $groupedSegments */ |
|
45
|
11 |
|
public function __construct(array $groupedSegments) |
|
46
|
|
|
{ |
|
47
|
11 |
|
$this->groupedSegments = $groupedSegments; |
|
48
|
11 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @return array|array<string, SegmentInterface> */ |
|
51
|
11 |
|
public function segmentsByTag(string $tag): array |
|
52
|
|
|
{ |
|
53
|
11 |
|
return $this->groupedSegments[$tag] ?? []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @return ?SegmentInterface */ |
|
57
|
3 |
|
public function segmentByTagAndSubId(string $tag, string $subId): ?SegmentInterface |
|
58
|
|
|
{ |
|
59
|
3 |
|
return $this->groupedSegments[$tag][$subId] ?? null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @psalm-pure |
|
64
|
|
|
* @psalm-return list<TransactionMessage> |
|
65
|
|
|
*/ |
|
66
|
11 |
|
private static function hasUnhSegment(self...$messages): array |
|
67
|
|
|
{ |
|
68
|
11 |
|
return array_values( |
|
69
|
|
|
array_filter($messages, static function (self $m) { |
|
70
|
11 |
|
return !empty($m->segmentsByTag(UNHMessageHeader::class)); |
|
71
|
11 |
|
}) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @psalm-pure */ |
|
76
|
11 |
|
private static function groupSegmentsByName(SegmentInterface...$segments): self |
|
77
|
|
|
{ |
|
78
|
11 |
|
$return = []; |
|
79
|
|
|
|
|
80
|
11 |
|
foreach ($segments as $s) { |
|
81
|
11 |
|
$return[$s->tag()] ??= []; |
|
82
|
11 |
|
$return[$s->tag()][$s->subId()] = $s; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
11 |
|
return new self($return); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|