MapFilter::findNested()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rs\XmlFilter\Filter;
6
7
use Rs\XmlFilter\Document\Element;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class MapFilter extends AbstractFilter implements Filter
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 12
    public function find(Element $element, array $options)
16
    {
17 12
        $results = [];
18 12
        $sort = [];
19
20 12
        foreach ($this->getNodes($element, $options) as $index => $node) {
21 12
            $key = $this->getKey($node, $options, $index);
22 12
            $value = $this->getValue($node, $options, $element);
23
24 12
            if (false === $this->checkCondition($key, $value, $options, $node, $this->manager)) {
25 2
                continue;
26
            }
27
28 12
            if (is_string($options['sort'])) {
29 1
                $sort[$key] = $this->manager->filter($node, ScalarFilter::class, ['path' => $options['sort'], 'nullable' => false]);
30
            }
31
32 12
            $results[$key] = $value;
33
        }
34
35 12
        return $this->prepareResult($options, $sort, $results);
36
    }
37
38 12
    private function prepareResult(array $options, $sort, array $results) : array
39
    {
40 12
        if ($sort) {
41 1
            asort($sort);
42 1
            foreach ($sort as $key => $result) {
43 1
                $sort[$key] = $results[$key];
44
            }
45 1
            $results = $sort;
46
47 1
            return $results;
48
        } else {
49 11
            $results = $this->sort($results, $options);
50
        }
51
52 11
        if ($options['valuesOnly'] ?? false) {
53 1
            return array_values($results);
54
        }
55
56 10
        return $results;
57
    }
58
59 12
    private function getNodes(Element $element, array $options) : array
60
    {
61 12
        return $element->find($this->preparePath($options['basePath'], $options));
62
    }
63
64 12
    private function getKey(Element $node, array $options, $default)
65
    {
66 12
        if ($options['key']) {
67 10
            $path = $this->preparePath($options['key'], $options);
68
69 10
            return $this->manager->filter($node, ScalarFilter::class, ['path' => $path, 'nullable' => false]);
70
        }
71
72 2
        return $default;
73
    }
74
75 12
    private function getValue(Element $node, array $options, Element $element)
76
    {
77 12
        if (is_string($options['value'])) {
78 6
            $path = $this->preparePath($options['value'], $options);
79
80 6
            return $this->manager->filter($node, ScalarFilter::class, ['path' => $path]);
81
        }
82
83 6
        return $this->findNested($node, $options, $element);
84
    }
85
86 6
    private function findNested(Element $node, array $options, Element $element)
87
    {
88 6
        list($filter, $nestedOptions) = $this->splitFilterAndOptions($options['value']);
89 6
        $nestedOptions['context'] = $node;
90 6
        $element = $filter === ScalarFilter::class ? $node : $element;
91
92 6
        return $this->manager->filter($element, $filter, $nestedOptions);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 12
    public function getOptions(OptionsResolver $resolver)
99
    {
100 12
        parent::getOptions($resolver);
101 12
        $resolver->setRequired(['basePath', 'value']);
102
103 12
        $resolver->setDefaults([
104 12
            'key'        => null,
105
            'valuesOnly' => false,
106
        ]);
107
108 12
        $resolver->setAllowedTypes('basePath', 'string');
109 12
        $resolver->setAllowedTypes('key', ['string', 'null']);
110 12
        $resolver->setAllowedTypes('valuesOnly', ['boolean', 'null']);
111 12
        $resolver->setAllowedTypes('value', ['string', 'array']);
112 12
    }
113
}
114