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