Completed
Push — master ( d59ceb...907509 )
by Edward
04:31
created

ValueListFetcher   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 99
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchDeepChildren() 0 16 3
A fetchChildren() 0 17 3
A fetchFilteredValues() 0 23 5
A fetchFilterContext() 0 19 5
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\Path\Value\EvaluatedValueInterface;
8
use Remorhaz\JSON\Path\Value\EvaluatedValueListInterface;
9
use Remorhaz\JSON\Data\Value\NodeValueInterface;
10
use Remorhaz\JSON\Path\Value\NodeValueListBuilder;
11
use Remorhaz\JSON\Path\Value\NodeValueListInterface;
12
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactory;
13
14
final class ValueListFetcher
15
{
16
17
    private $valueIteratorFactory;
18
19
    private $valueFetcher;
20
21
    public function __construct(ValueIteratorFactory $valueIteratorFactory, ValueFetcherInterface $valueFetcher)
22
    {
23
        $this->valueIteratorFactory = $valueIteratorFactory;
24
        $this->valueFetcher = $valueFetcher;
25
    }
26
27
    /**
28
     * @param NodeValueListInterface $source
29
     * @param Matcher\ChildMatcherInterface ...$matcherList
30
     * @return NodeValueListInterface
31
     */
32
    public function fetchChildren(
33
        NodeValueListInterface $source,
34
        Matcher\ChildMatcherInterface ...$matcherList
35
    ): NodeValueListInterface {
36
        $nodesBuilder = new NodeValueListBuilder;
37
        foreach ($source->getValues() as $sourceIndex => $sourceValue) {
38
            $matcher = $matcherList[$sourceIndex];
39
            $children = $this
40
                ->valueFetcher
41
                ->fetchValueChildren($matcher, $sourceValue);
42
            $outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex);
43
            foreach ($children as $child) {
44
                $nodesBuilder->addValue($child, $outerIndex);
45
            }
46
        }
47
48
        return $nodesBuilder->build();
49
    }
50
51
    public function fetchDeepChildren(
52
        Matcher\ChildMatcherInterface $matcher,
53
        NodeValueListInterface $source
54
    ): NodeValueListInterface {
55
        $nodesBuilder = new NodeValueListBuilder;
56
        foreach ($source->getValues() as $sourceIndex => $sourceValue) {
57
            $children = $this
58
                ->valueFetcher
59
                ->fetchValueDeepChildren($matcher, $sourceValue);
60
            $outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex);
61
            foreach ($children as $child) {
62
                $nodesBuilder->addValue($child, $outerIndex);
63
            }
64
        }
65
66
        return $nodesBuilder->build();
67
    }
68
69
    public function fetchFilterContext(NodeValueListInterface $source): NodeValueListInterface
70
    {
71
        $nodesBuilder = new NodeValueListBuilder;
72
        foreach ($source->getValues() as $sourceIndex => $sourceValue) {
73
            if (!$sourceValue instanceof NodeValueInterface) {
74
                throw new Exception\InvalidContextValueException($sourceValue);
75
            }
76
            $outerIndex = $source->getIndexMap()->getOuterIndex($sourceIndex);
77
            $children = $sourceValue instanceof ArrayValueInterface
78
                ? $this
79
                    ->valueFetcher
80
                    ->fetchValueChildren(new Matcher\AnyChildMatcher, $sourceValue)
81
                : [$sourceValue];
82
            foreach ($children as $child) {
83
                $nodesBuilder->addValue($child, $outerIndex);
84
            }
85
        }
86
87
        return $nodesBuilder->build();
88
    }
89
90
    public function fetchFilteredValues(
91
        EvaluatedValueListInterface $results,
92
        NodeValueListInterface $values
93
    ): NodeValueListInterface {
94
        if (!$values->getIndexMap()->equals($results->getIndexMap())) {
95
            throw new Exception\IndexMapMatchFailedException($values, $results);
96
        }
97
        $nodesBuilder = new NodeValueListBuilder;
98
        foreach ($values->getValues() as $index => $value) {
99
            $evaluatedValue = $results->getValue($index);
100
            if (!$evaluatedValue instanceof EvaluatedValueInterface) {
101
                throw new Exception\InvalidFilterValueException($evaluatedValue);
102
            }
103
            if (!$evaluatedValue->getData()) {
104
                continue;
105
            }
106
            $nodesBuilder->addValue(
107
                $value,
108
                $values->getIndexMap()->getOuterIndex($index)
109
            );
110
        }
111
112
        return $nodesBuilder->build();
113
    }
114
}
115