Passed
Push — master ( 8e4f12...3d58fd )
by Edward
05:59
created

ResultFactory::createSelectResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Processor\Result;
5
6
use Remorhaz\JSON\Path\Processor\PathEncoderInterface;
7
use function array_map;
8
use function count;
9
use Remorhaz\JSON\Data\Export\ValueDecoderInterface;
10
use Remorhaz\JSON\Data\Export\ValueEncoderInterface;
11
use Remorhaz\JSON\Data\Path\PathInterface;
12
use Remorhaz\JSON\Data\Value\NodeValueInterface;
13
use Remorhaz\JSON\Data\Value\ValueInterface;
14
use Remorhaz\JSON\Path\Value\ValueListInterface;
15
16
final class ResultFactory implements ResultFactoryInterface
17
{
18
19
    private $jsonEncoder;
20
21
    private $jsonDecoder;
22
23
    private $pathsEncoder;
24
25 19
    public function __construct(
26
        ValueEncoderInterface $jsonEncoder,
27
        ValueDecoderInterface $jsonDecoder,
28
        PathEncoderInterface $pathEncoder
29
    ) {
30 19
        $this->jsonEncoder = $jsonEncoder;
31 19
        $this->jsonDecoder = $jsonDecoder;
32 19
        $this->pathsEncoder = $pathEncoder;
33 19
    }
34
35 3
    public function createSelectResult(ValueListInterface $values): SelectResultInterface
36
    {
37 3
        return new SelectResult($this->jsonEncoder, $this->jsonDecoder, ...$values->getValues());
38
    }
39
40 5
    public function createSelectOneResult(ValueListInterface $values): ValueResultInterface
41
    {
42 5
        return $this->createValueResult($this->findSingleValue($values));
43
    }
44
45 2
    public function createSelectPathsResult(ValueListInterface $values): SelectPathsResultInterface
46
    {
47 2
        return new SelectPathsResult(
48 2
            $this->pathsEncoder,
49 2
            ...array_map([$this, 'getValuePath'], $values->getValues())
50
        );
51
    }
52
53 5
    private function getValuePath(ValueInterface $value): PathInterface
54
    {
55 5
        if (!$value instanceof NodeValueInterface) {
56 2
            throw new Exception\PathNotFoundInValueException($value);
57
        }
58
59 3
        return $value->getPath();
60
    }
61
62 5
    public function createSelectOnePathResult(ValueListInterface $values): SelectOnePathResultInterface
63
    {
64 5
        $value = $this->findSingleValue($values);
65 4
        $path = isset($value)
66 3
            ? $this->getValuePath($value)
67 3
            : null;
68
69 3
        return isset($path)
70 2
            ? new ExistingSelectOnePathResult($this->pathsEncoder, $path)
71 3
            : new NonExistingSelectOnePathResult;
72
    }
73
74 10
    private function findSingleValue(ValueListInterface $values): ?ValueInterface
75
    {
76 10
        switch (count($values->getValues())) {
77 10
            case 0:
78 2
                return null;
79
80 8
            case 1:
81 6
                return $values->getValue(0);
82
        }
83
84 2
        throw new Exception\MoreThanOneValueInListException($values);
85
    }
86
87 8
    public function createValueResult(?ValueInterface $value): ValueResultInterface
88
    {
89 8
        return isset($value)
90 6
            ? new ExistingValueResult($this->jsonEncoder, $this->jsonDecoder, $value)
91 8
            : new NonExistingValueResult;
92
    }
93
}
94