Passed
Push — master ( f6567e...6c178d )
by Edward
09:59 queued 07:10
created

IndexMap::getOuterIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Value;
5
6
use function array_keys;
7
use function count;
8
use function in_array;
9
10
final class IndexMap implements IndexMapInterface
11
{
12
13
    private $outerIndexes;
14
15 35
    public function __construct(?int ...$outerIndexes)
16
    {
17 35
        $this->outerIndexes = $outerIndexes;
18 35
    }
19
20 11
    public function count()
21
    {
22 11
        return count($this->outerIndexes);
23
    }
24
25 8
    public function getInnerIndexes(): array
26
    {
27 8
        return array_keys($this->outerIndexes);
28
    }
29
30 21
    public function getOuterIndexes(): array
31
    {
32 21
        return $this->outerIndexes;
33
    }
34
35 3
    public function getOuterIndex(int $innerIndex): int
36
    {
37 3
        if (!isset($this->outerIndexes[$innerIndex])) {
38 2
            throw new Exception\OuterIndexNotFoundException($innerIndex, $this);
39
        }
40
41 1
        return $this->outerIndexes[$innerIndex];
42
    }
43
44 5
    public function outerIndexExists(int $outerIndex): bool
45
    {
46 5
        return in_array($outerIndex, $this->outerIndexes, true);
47
    }
48
49 4
    public function split(): IndexMapInterface
50
    {
51 4
        return new self(...$this->getInnerIndexes());
52
    }
53
54 4
    public function join(IndexMapInterface $indexMap): IndexMapInterface
55
    {
56 4
        $outerIndexes = [];
57 4
        foreach ($indexMap->getOuterIndexes() as $innerIndex => $outerIndex) {
58 3
            $outerIndexes[] = $this->outerIndexExists($innerIndex)
59 3
                ? $outerIndex
60 1
                : null;
61
        }
62
63 4
        return new self(...$outerIndexes);
64
    }
65
66 3
    public function equals(IndexMapInterface $indexMap): bool
67
    {
68 3
        return $this->outerIndexes === $indexMap->getOuterIndexes();
69
    }
70
71 7
    public function isCompatible(IndexMapInterface $indexMap): bool
72
    {
73 7
        if (count($indexMap) != count($this)) {
74 1
            return false;
75
        }
76
77 6
        $anotherMap = $indexMap->getOuterIndexes();
78 6
        foreach ($this->outerIndexes as $innerIndex => $outerIndex) {
79 5
            if (!isset($outerIndex, $anotherMap[$innerIndex])) {
80 4
                continue;
81
            }
82
83 2
            if ($outerIndex !== $anotherMap[$innerIndex]) {
84 1
                return false;
85
            }
86
        }
87
88 5
        return true;
89
    }
90
}
91