Passed
Push — master ( ed8f97...95efe5 )
by Chema
03:24
created

TransactionMessage::addSegment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser;
6
7
use EdifactParser\Segments\SegmentInterface;
8
9
final class TransactionMessage
10
{
11
    /**
12
     * First string: segment key
13
     * Second key: sub segment key.
14
     *
15
     * @var array<string, array<string,SegmentInterface>>
16
     */
17
    private $segments = [];
18
19 8
    public function __construct(array $segments = [])
20
    {
21 8
        foreach ($segments as $segment) {
22 6
            $this->addSegment($segment);
23
        }
24 8
    }
25
26 8
    public function addSegment(SegmentInterface $segment): void
27
    {
28 8
        $name = $segment->name();
29
30 8
        if (!isset($this->segments[$name])) {
31 8
            $this->segments[$name] = [];
32
        }
33
34 8
        $this->segments[$name][$segment->subSegmentKey()] = $segment;
35 8
    }
36
37
    /** @return array<string, array<string,SegmentInterface>> */
38 4
    public function segments(): array
39
    {
40 4
        return $this->segments;
41
    }
42
}
43