1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saxulum\ElasticSearchQueryBuilder; |
6
|
|
|
|
7
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode; |
8
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode; |
9
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\BoolNode; |
10
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode; |
11
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\IntNode; |
12
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\NullNode; |
13
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode; |
14
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\StringNode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @deprecated use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode |
18
|
|
|
*/ |
19
|
|
|
final class QueryBuilder implements QueryBuilderInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ObjectNode |
23
|
|
|
*/ |
24
|
|
|
private $rootNode; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AbstractNode |
28
|
|
|
*/ |
29
|
|
|
private $node; |
30
|
|
|
|
31
|
19 |
|
public function __construct() |
32
|
|
|
{ |
33
|
19 |
|
@trigger_error(sprintf('Use "%s" instead of the "%s"', ObjectNode::class, self::class), E_USER_DEPRECATED); |
|
|
|
|
34
|
|
|
|
35
|
19 |
|
$this->rootNode = ObjectNode::create(); |
36
|
19 |
|
$this->node = $this->rootNode; |
37
|
19 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array ...$arguments |
41
|
|
|
* |
42
|
|
|
* @return QueryBuilderInterface |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
16 |
|
public function add(...$arguments): QueryBuilderInterface |
47
|
|
|
{ |
48
|
16 |
|
if ($this->node instanceof ObjectNode) { |
49
|
16 |
|
return $this->addToObjectNode(...$arguments); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
return $this->addToArrayNode(...$arguments); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AbstractNode $node |
57
|
|
|
* |
58
|
|
|
* @return QueryBuilderInterface |
59
|
|
|
* |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
5 |
View Code Duplication |
public function addToArrayNode(AbstractNode $node): QueryBuilderInterface |
|
|
|
|
63
|
|
|
{ |
64
|
5 |
|
if (!$this->node instanceof ArrayNode) { |
65
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node))); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$this->node->add($node); |
69
|
4 |
|
$this->reassignParent($node); |
70
|
|
|
|
71
|
4 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $key |
76
|
|
|
* @param AbstractNode $node |
77
|
|
|
* |
78
|
|
|
* @return QueryBuilderInterface |
79
|
|
|
* |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
17 |
View Code Duplication |
public function addToObjectNode(string $key, AbstractNode $node): QueryBuilderInterface |
|
|
|
|
83
|
|
|
{ |
84
|
17 |
|
if (!$this->node instanceof ObjectNode) { |
85
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node))); |
86
|
|
|
} |
87
|
|
|
|
88
|
17 |
|
$this->node->add($key, $node); |
89
|
17 |
|
$this->reassignParent($node); |
90
|
|
|
|
91
|
17 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param AbstractNode $node |
96
|
|
|
*/ |
97
|
17 |
|
private function reassignParent(AbstractNode $node) |
98
|
|
|
{ |
99
|
17 |
|
if ($node instanceof ArrayNode || $node instanceof ObjectNode) { |
100
|
17 |
|
$this->node = $node; |
101
|
|
|
} |
102
|
17 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return QueryBuilderInterface |
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
2 |
|
public function end(): QueryBuilderInterface |
110
|
|
|
{ |
111
|
2 |
|
if (null === $this->node = $this->node->getParent()) { |
112
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on main node', __FUNCTION__)); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param bool $allowSerializeEmpty |
120
|
|
|
* |
121
|
|
|
* @return ArrayNode |
122
|
|
|
*/ |
123
|
5 |
|
public function arrayNode(bool $allowSerializeEmpty = false): ArrayNode |
124
|
|
|
{ |
125
|
5 |
|
return ArrayNode::create($allowSerializeEmpty); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param bool|null $value |
130
|
|
|
* @param bool $allowSerializeEmpty |
131
|
|
|
* |
132
|
|
|
* @return BoolNode |
133
|
|
|
*/ |
134
|
1 |
|
public function boolNode($value = null, bool $allowSerializeEmpty = false): BoolNode |
135
|
|
|
{ |
136
|
1 |
|
return BoolNode::create($value, $allowSerializeEmpty); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param float|null $value |
141
|
|
|
* @param bool $allowSerializeEmpty |
142
|
|
|
* |
143
|
|
|
* @return FloatNode |
144
|
|
|
*/ |
145
|
1 |
|
public function floatNode($value = null, bool $allowSerializeEmpty = false): FloatNode |
146
|
|
|
{ |
147
|
1 |
|
return FloatNode::create($value, $allowSerializeEmpty); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int|null $value |
152
|
|
|
* @param bool $allowSerializeEmpty |
153
|
|
|
* |
154
|
|
|
* @return IntNode |
155
|
|
|
*/ |
156
|
4 |
|
public function intNode($value = null, bool $allowSerializeEmpty = false): IntNode |
157
|
|
|
{ |
158
|
4 |
|
return IntNode::create($value, $allowSerializeEmpty); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return NullNode |
163
|
|
|
*/ |
164
|
1 |
|
public function nullNode(): NullNode |
165
|
|
|
{ |
166
|
1 |
|
return NullNode::create(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param bool $allowSerializeEmpty |
171
|
|
|
* |
172
|
|
|
* @return ObjectNode |
173
|
|
|
*/ |
174
|
16 |
|
public function objectNode(bool $allowSerializeEmpty = false): ObjectNode |
175
|
|
|
{ |
176
|
16 |
|
return ObjectNode::create($allowSerializeEmpty); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string|null $value |
181
|
|
|
* @param bool $allowSerializeEmpty |
182
|
|
|
* |
183
|
|
|
* @return StringNode |
184
|
|
|
*/ |
185
|
13 |
|
public function stringNode($value = null, bool $allowSerializeEmpty = false): StringNode |
186
|
|
|
{ |
187
|
13 |
|
return StringNode::create($value, $allowSerializeEmpty); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return \stdClass|null |
192
|
|
|
*/ |
193
|
16 |
|
public function serialize() |
194
|
|
|
{ |
195
|
16 |
|
return $this->rootNode->serialize(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param bool $beautify |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
16 |
View Code Duplication |
public function json(bool $beautify = false): string |
|
|
|
|
204
|
|
|
{ |
205
|
16 |
|
if (null === $serialized = $this->serialize()) { |
206
|
2 |
|
return ''; |
207
|
|
|
} |
208
|
|
|
|
209
|
14 |
|
if ($beautify) { |
210
|
1 |
|
return json_encode($serialized, JSON_PRETTY_PRINT); |
211
|
|
|
} |
212
|
|
|
|
213
|
13 |
|
return json_encode($serialized); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.