TestingSegmentFactory.php$0 ➔ __construct()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser\Tests\Functional;
6
7
use EdifactParser\Segments\SegmentFactory;
8
use EdifactParser\Segments\SegmentFactoryInterface;
9
use EdifactParser\Segments\SegmentInterface;
10
11
/** @psalm-immutable */
12
final class TestingSegmentFactory implements SegmentFactoryInterface
13
{
14
    private string $customKey;
15
16
    private SegmentFactory $defaultFactory;
17
18
    public function __construct(string $customKey)
19
    {
20
        $this->customKey = $customKey;
21
        $this->defaultFactory = SegmentFactory::withDefaultSegments();
22
    }
23
24
    public function createSegmentFromArray(array $rawArray): SegmentInterface
25
    {
26
        if ($this->customKey !== $rawArray[0]) {
27
            return $this->defaultFactory->createSegmentFromArray($rawArray);
28
        }
29
30
        return new /** @psalm-immutable */ class($rawArray) implements SegmentInterface {
31
            private array $rawArray;
32
33
            public function __construct(array $rawArray)
34
            {
35
                $this->rawArray = $rawArray;
36
            }
37
38
            public function tag(): string
39
            {
40
                return (string) $this->rawArray[0];
41
            }
42
43
            public function subId(): string
44
            {
45
                return (string) $this->rawArray[1];
46
            }
47
48
            public function rawValues(): array
49
            {
50
                return $this->rawArray;
51
            }
52
        };
53
    }
54
}
55