1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saxulum\ElasticSearchQueryBuilder\Converter; |
6
|
|
|
|
7
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode; |
8
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractParentNode; |
9
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode; |
10
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode; |
11
|
|
|
|
12
|
|
|
final class IteratableToNodeConverter implements IteratableToNodeConverterInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ScalarToNodeConverterInterface |
16
|
|
|
*/ |
17
|
|
|
private $scalarToNodeConverter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ScalarToNodeConverterInterface $scalarToNodeConverter |
21
|
|
|
*/ |
22
|
3 |
|
public function __construct(ScalarToNodeConverterInterface $scalarToNodeConverter) |
23
|
|
|
{ |
24
|
3 |
|
$this->scalarToNodeConverter = $scalarToNodeConverter; |
25
|
3 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array|\Traversable $data |
29
|
|
|
* @param string $path |
30
|
|
|
* @return AbstractParentNode |
31
|
|
|
* @throws \InvalidArgumentException |
32
|
|
|
*/ |
33
|
3 |
|
public function convert($data, string $path = ''): AbstractParentNode |
34
|
|
|
{ |
35
|
3 |
|
if (!is_array($data) && !$data instanceof \Traversable) { |
36
|
1 |
|
throw new \InvalidArgumentException(sprintf('Params need to be array or %s', \Traversable::class)); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$isArray = $this->isArray($data); |
40
|
|
|
|
41
|
2 |
|
$dataNode = $isArray ? ArrayNode::create() : ObjectNode::create(); |
42
|
2 |
|
foreach ($data as $key => $value) { |
43
|
2 |
|
$node = $this->getNode($value, $this->getSubPath($path, $key, $isArray)); |
44
|
|
|
|
45
|
1 |
|
if ($isArray) { |
46
|
1 |
|
$dataNode->add($node); |
|
|
|
|
47
|
|
|
} else { |
48
|
1 |
|
$dataNode->add((string) $key, $node); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $dataNode; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array|\Traversable $data |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
2 |
|
private function isArray($data): bool |
60
|
|
|
{ |
61
|
2 |
|
$counter = 0; |
62
|
2 |
|
foreach ($data as $key => $value) { |
63
|
2 |
|
if ($key !== $counter) { |
64
|
2 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$counter++; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $path |
75
|
|
|
* @param string|int $key |
76
|
|
|
* @param bool $isArray |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
2 |
|
private function getSubPath(string $path, $key, bool $isArray): string |
80
|
|
|
{ |
81
|
2 |
|
$key = (string) $key; |
82
|
|
|
|
83
|
2 |
|
if ($isArray) { |
84
|
2 |
|
return $path . '[' . $key . ']'; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $path !== '' ? $path . '.' . $key : $key; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @param string $path |
93
|
|
|
* @return AbstractNode |
94
|
|
|
*/ |
95
|
2 |
|
private function getNode($value, string $path): AbstractNode |
96
|
|
|
{ |
97
|
2 |
|
if (is_array($value) || $value instanceof \Traversable) { |
98
|
2 |
|
return $this->convert($value, $path); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $this->scalarToNodeConverter->convert($value, $path); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for function calls that miss required arguments.