Completed
Push — master ( 89e2e4...0c0824 )
by Edward
04:32
created

Runtime::calculateIsEqual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Runtime;
5
6
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
7
use Remorhaz\JSON\Data\Value\ValueInterface;
8
use Remorhaz\JSON\Path\Value\EvaluatedValueList;
9
use Remorhaz\JSON\Path\Value\EvaluatedValueListInterface;
10
use Remorhaz\JSON\Path\Value\IndexMap;
11
use Remorhaz\JSON\Path\Value\LiteralArrayValue;
12
use Remorhaz\JSON\Path\Value\LiteralArrayValueList;
13
use Remorhaz\JSON\Path\Value\LiteralScalarValue;
14
use Remorhaz\JSON\Path\Value\LiteralValueInterface;
15
use Remorhaz\JSON\Path\Value\LiteralValueList;
16
use Remorhaz\JSON\Data\Value\NodeValueInterface;
17
use Remorhaz\JSON\Path\Value\NodeValueList;
18
use Remorhaz\JSON\Path\Value\NodeValueListInterface;
19
use Remorhaz\JSON\Path\Value\ValueListInterface;
20
21
final class Runtime implements RuntimeInterface
22
{
23
24
    private $fetcher;
25
26
    public function __construct(Fetcher $fetcher)
27
    {
28
        $this->fetcher = $fetcher;
29
    }
30
31
    public function getInput(NodeValueInterface $rootValue): NodeValueListInterface
32
    {
33
        return new NodeValueList(new IndexMap(0), $rootValue);
34
    }
35
36
    public function createFilterContext(NodeValueListInterface $values): NodeValueListInterface
37
    {
38
        return $this
39
            ->fetcher
40
            ->fetchFilterContext($values);
41
    }
42
43
    public function split(NodeValueListInterface $values): NodeValueListInterface
44
    {
45
        return new NodeValueList(
46
            $values->getIndexMap()->split(),
47
            ...$values->getValues()
48
        );
49
    }
50
51
    public function filter(
52
        NodeValueListInterface $contextValues,
53
        EvaluatedValueListInterface $evaluatedValues
54
    ): NodeValueListInterface {
55
        return $this
56
            ->fetcher
57
            ->filterValues(
58
                new ValueListFilter(
59
                    new EvaluatedValueList(
60
                        $evaluatedValues->getIndexMap()->join($contextValues->getIndexMap()),
61
                        ...$evaluatedValues->getResults()
62
                    )
63
                ),
64
                $contextValues
65
            );
66
    }
67
68
    public function fetchChildren(
69
        NodeValueListInterface $values,
70
        Matcher\ChildMatcherInterface ...$matchers
71
    ): NodeValueListInterface {
72
        return $this
73
            ->fetcher
74
            ->fetchChildren($values, ...$matchers);
75
    }
76
77
    public function fetchChildrenDeep(
78
        NodeValueListInterface $values,
79
        Matcher\ChildMatcherInterface $matcher
80
    ): NodeValueListInterface {
81
        return $this
82
            ->fetcher
83
            ->fetchDeepChildren($matcher, $values);
84
    }
85
86
    public function matchAnyChild(NodeValueListInterface $source): array
87
    {
88
        return array_map(
89
            function (): Matcher\ChildMatcherInterface {
90
                return new Matcher\AnyChildMatcher;
91
            },
92
            $source->getIndexMap()->getInnerIndice()
93
        );
94
    }
95
96
    public function matchPropertyStrictly(array $nameLists): array
97
    {
98
        return array_map(
99
            function (array $nameList): Matcher\ChildMatcherInterface {
100
                return new Matcher\StrictPropertyMatcher(...$nameList);
101
            },
102
            $nameLists
103
        );
104
    }
105
106
    public function matchElementStrictly(array $indexLists): array
107
    {
108
        return array_map(
109
            function (array $indexList): Matcher\ChildMatcherInterface {
110
                return new Matcher\StrictElementMatcher(...$indexList);
111
            },
112
            $indexLists
113
        );
114
    }
115
116
    public function populateLiteral(NodeValueListInterface $source, LiteralValueInterface $value): ValueListInterface
117
    {
118
        return new LiteralValueList($source->getIndexMap(), $value);
119
    }
120
121
    public function populateArrayElements(
122
        NodeValueListInterface $source,
123
        ValueListInterface ...$values
124
    ): array {
125
        foreach ($values as $valueList) {
126
            if (!$source->getIndexMap()->equals($valueList->getIndexMap())) {
127
                throw new Exception\IndexMapMatchFailedException($valueList, $source);
128
            }
129
        }
130
        $elementLists = array_fill_keys($source->getIndexMap()->getInnerIndice(), []);
131
        foreach ($values as $valueList) {
132
            foreach ($valueList->getValues() as $innerIndex => $value) {
133
                $elementLists[$innerIndex][] = $value;
134
            }
135
        }
136
137
        $createArrayElement = function (array $elements) use ($source): ValueInterface {
138
            return new LiteralArrayValue($source->getIndexMap(), ...$elements);
139
        };
140
141
        return array_map($createArrayElement, $elementLists);
142
    }
143
144
    public function populateIndexList(NodeValueListInterface $source, int ...$indexList): array
145
    {
146
        return array_fill_keys(
147
            $source
148
                ->getIndexMap()
149
                ->getInnerIndice(),
150
            $indexList
151
        );
152
    }
153
154
    public function populateIndexSlice(NodeValueListInterface $source, ?int $start, ?int $end, ?int $step): array
155
    {
156
        return $this
157
            ->fetcher
158
            ->fetchSliceIndice($source, $start, $end, $step);
159
    }
160
161
    public function populateNameList(NodeValueListInterface $source, string ...$nameList): array
162
    {
163
        return array_fill_keys(
164
            $source
165
                ->getIndexMap()
166
                ->getInnerIndice(),
167
            $nameList
168
        );
169
    }
170
171
    public function createScalar($value): LiteralValueInterface
172
    {
173
        return new LiteralScalarValue($value);
174
    }
175
176
    public function createArray(ValueListInterface $source, ArrayValueInterface ...$elements): ValueListInterface
177
    {
178
        return new LiteralArrayValueList($source->getIndexMap(), ...$elements);
179
    }
180
}
181