Completed
Push — master ( c16c79...ab8250 )
by Edward
04:36
created

ResultFactory::createValueResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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