ReferenceFilter::checkReference()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rs\XmlFilter\Filter;
6
7
use Rs\XmlFilter\Document\Element;
8
use Rs\XmlFilter\Exception\ReferenceException;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
class ReferenceFilter extends AbstractFilter implements Filter
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 5
    public function find(Element $element, array $options)
17
    {
18 5
        if (!$options['value']) {
19 1
            $options['value'] = $options['reference'];
20
        }
21
22 5
        $this->checkReference($element, $options);
23
24 3
        list($filter, $nestedOptions) = $this->splitFilterAndOptions($options['value']);
25
26 3
        return $this->filter($element, $filter, $options, $nestedOptions);
27
    }
28
29 5
    private function checkReference(Element $element, array $options)
30
    {
31 5
        $ref = $this->manager->filter(
32 5
            $this->getSearchElement($element, $options),
33 5
            ScalarFilter::class,
34 5
            ['path' => $this->preparePath($options['reference'], $options)]
35
        );
36
37 5
        if (!$ref) {
38 2
            $errorElement = $element;
39 2
            if ($options['element']) {
40 1
                $errorElement = $element->find($options['element'])[0];
41
            }
42 2
            throw new ReferenceException(sprintf('reference (%s) could not be resolved', $options['reference']), $errorElement, $options);
43
        }
44 3
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 5
    public function getOptions(OptionsResolver $resolver)
50
    {
51 5
        $resolver->setRequired(['reference']);
52
53 5
        $resolver->setDefault('element', null);
54 5
        $resolver->setDefault('value', null);
55
56 5
        $resolver->setAllowedTypes('element', ['string', 'null']);
57 5
        $resolver->setAllowedTypes('reference', 'string');
58 5
        $resolver->setAllowedTypes('value', ['string', 'array', 'null']);
59
60 5
        parent::getOptions($resolver);
61 5
    }
62
}
63