Passed
Pull Request — master (#3)
by Chema
07:57
created

TestingSegmentFactory.php$0 ➔ __construct()   A

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
final class TestingSegmentFactory implements SegmentFactoryInterface
12
{
13
    private string $customKey;
14
15
    private SegmentFactory $defaultFactory;
16
17
    public function __construct(string $customKey)
18
    {
19
        $this->customKey = $customKey;
20
        $this->defaultFactory = new SegmentFactory();
21
    }
22
23
    public function segmentFromArray(array $rawArray): SegmentInterface
24
    {
25
        if ($this->customKey !== $rawArray[0]) {
26
            return $this->defaultFactory->segmentFromArray($rawArray);
27
        }
28
29
        return new class($rawArray) implements SegmentInterface {
30
            private array $rawArray;
31
32
            public function __construct(array $rawArray)
33
            {
34
                $this->rawArray = $rawArray;
35
            }
36
37
            public function name(): string
38
            {
39
                return $this->rawArray[0];
40
            }
41
42
            public function subSegmentKey(): string
43
            {
44
                return $this->rawArray[1];
45
            }
46
47
            public function rawValues(): array
48
            {
49
                return $this->rawArray;
50
            }
51
        };
52
    }
53
}
54