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\DecoderInterface; |
9
|
|
|
use Remorhaz\JSON\Data\Export\EncoderInterface; |
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(EncoderInterface $jsonEncoder, DecoderInterface $jsonDecoder, PathEncoder $pathEncoder) |
26
|
|
|
{ |
27
|
|
|
$this->jsonEncoder = $jsonEncoder; |
28
|
|
|
$this->jsonDecoder = $jsonDecoder; |
29
|
|
|
$this->pathsEncoder = $pathEncoder; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function createSelectResult(ValueListInterface $values): SelectResultInterface |
33
|
|
|
{ |
34
|
|
|
return new SelectResult($this->jsonEncoder, $this->jsonDecoder, ...$values->getValues()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function createSelectOneResult(ValueListInterface $values): SelectOneResultInterface |
38
|
|
|
{ |
39
|
|
|
$value = $this->findSingleValue($values); |
40
|
|
|
|
41
|
|
|
return isset($value) |
42
|
|
|
? new ExistingSelectOneResult( |
43
|
|
|
$this->jsonEncoder, |
44
|
|
|
$this->jsonDecoder, |
45
|
|
|
$values->getValue(0) |
46
|
|
|
) |
47
|
|
|
: new NonExistingSelectOneResult; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function createSelectPathsResult(ValueListInterface $values): SelectPathsResultInterface |
51
|
|
|
{ |
52
|
|
|
return new SelectPathsResult( |
53
|
|
|
$this->pathsEncoder, |
54
|
|
|
...array_map([$this, 'getValuePath'], $values->getValues()) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getValuePath(ValueInterface $value): PathInterface |
59
|
|
|
{ |
60
|
|
|
if (!$value instanceof NodeValueInterface) { |
61
|
|
|
throw new Exception\PathNotFoundInValueException($value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $value->getPath(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function createSelectOnePathResult(ValueListInterface $values): SelectOnePathResultInterface |
68
|
|
|
{ |
69
|
|
|
$value = $this->findSingleValue($values); |
70
|
|
|
|
71
|
|
|
return isset($values) |
72
|
|
|
? new ExistingSelectOnePathResult($this->pathsEncoder, $this->getValuePath($value)) |
|
|
|
|
73
|
|
|
: new NonExistingSelectOnePathResult; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function findSingleValue(ValueListInterface $values): ?ValueInterface |
77
|
|
|
{ |
78
|
|
|
switch (count($values->getValues())) { |
79
|
|
|
case 0: |
80
|
|
|
return null; |
81
|
|
|
|
82
|
|
|
case 1: |
83
|
|
|
return $values->getValue(0); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new Exception\MoreThanOneValueInListException($values); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|