1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Pipeline\AbstractPipelineAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\BuilderBag; |
16
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
17
|
|
|
use ONGR\ElasticsearchDSL\NameAwareTrait; |
18
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AbstractAggregation class. |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractAggregation implements BuilderInterface |
24
|
|
|
{ |
25
|
|
|
use ParametersTrait; |
26
|
|
|
use NameAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $field; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var BuilderBag |
35
|
|
|
*/ |
36
|
|
|
private $aggregations; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var BuilderBag[] |
40
|
|
|
*/ |
41
|
|
|
private $pipelines = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Abstract supportsNesting method. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
abstract protected function supportsNesting(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array|\stdClass |
52
|
|
|
*/ |
53
|
|
|
abstract protected function getArray(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Inner aggregations container init. |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
*/ |
60
|
|
|
public function __construct($name) |
61
|
|
|
{ |
62
|
|
|
$this->setName($name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $field |
67
|
|
|
*/ |
68
|
|
|
public function setField($field) |
69
|
|
|
{ |
70
|
|
|
$this->field = $field; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getField() |
77
|
|
|
{ |
78
|
|
|
return $this->field; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Adds a sub-aggregation. |
83
|
|
|
* |
84
|
|
|
* @param AbstractAggregation $abstractAggregation |
85
|
|
|
*/ |
86
|
|
|
public function addAggregation(AbstractAggregation $abstractAggregation) |
87
|
|
|
{ |
88
|
|
|
if (!$this->aggregations) { |
89
|
|
|
$this->aggregations = $this->createBuilderBag(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->aggregations->add($abstractAggregation); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds a sub-aggregation. |
97
|
|
|
* |
98
|
|
|
* @param AbstractPipelineAggregation $pipeline |
99
|
|
|
*/ |
100
|
|
|
public function addPipeline(AbstractPipelineAggregation $pipeline) |
101
|
|
|
{ |
102
|
|
|
$family = $pipeline->getPipelineFamily(); |
103
|
|
|
if (!isset($this->pipelines[$family])) { |
104
|
|
|
$this->pipelines[$family] = $this->createBuilderBag(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->pipelines[$family]->add($pipeline); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns all sibling pipeline aggregations. |
112
|
|
|
* |
113
|
|
|
* @return AbstractPipelineAggregation[] |
114
|
|
|
*/ |
115
|
|
|
public function getSiblingPipelines() |
116
|
|
|
{ |
117
|
|
|
if (isset($this->pipelines['sibling'])) { |
118
|
|
|
return $this->pipelines['sibling']->all(); |
119
|
|
|
} else { |
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns all parent pipeline aggregations. |
126
|
|
|
* |
127
|
|
|
* @return AbstractPipelineAggregation[] |
128
|
|
|
*/ |
129
|
|
|
public function getParentPipelines() |
130
|
|
|
{ |
131
|
|
|
if (isset($this->pipelines['parent'])) { |
132
|
|
|
return $this->pipelines['parent']->all(); |
133
|
|
|
} else { |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks if pipelines or a given family of pipeline |
140
|
|
|
* aggregations is set |
141
|
|
|
* |
142
|
|
|
* @param string $family |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function hasPipelines($family = null) |
147
|
|
|
{ |
148
|
|
|
return $family ? isset($this->pipelines[$family]) : |
149
|
|
|
!empty($this->pipelines); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns all sub aggregations. |
155
|
|
|
* |
156
|
|
|
* @return BuilderBag[] |
157
|
|
|
*/ |
158
|
|
|
public function getAggregations() |
159
|
|
|
{ |
160
|
|
|
if ($this->aggregations) { |
161
|
|
|
return $this->aggregations->all(); |
|
|
|
|
162
|
|
|
} else { |
163
|
|
|
return []; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns sub aggregation. |
169
|
|
|
* @param string $name Aggregation name to return. |
170
|
|
|
* |
171
|
|
|
* @return AbstractAggregation|null |
172
|
|
|
*/ |
173
|
|
|
public function getAggregation($name) |
174
|
|
|
{ |
175
|
|
|
if ($this->aggregations && $this->aggregations->has($name)) { |
176
|
|
|
return $this->aggregations->get($name); |
177
|
|
|
} else { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function toArray() |
186
|
|
|
{ |
187
|
|
|
$array = $this->getArray(); |
188
|
|
|
$result = [ |
189
|
|
|
$this->getType() => is_array($array) ? $this->processArray($array) : $array, |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
if ($this->supportsNesting() || $this->hasPipelines('parent')) { |
193
|
|
|
$nestedResult = $this->collectNestedAggregations(); |
194
|
|
|
|
195
|
|
|
if (!empty($nestedResult)) { |
196
|
|
|
$result['aggregations'] = $nestedResult; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $result; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Process all nested aggregations. |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
protected function collectNestedAggregations() |
209
|
|
|
{ |
210
|
|
|
$result = []; |
211
|
|
|
/** @var AbstractAggregation $aggregation */ |
212
|
|
|
foreach ($this->getAggregations() as $aggregation) { |
213
|
|
|
$result[$aggregation->getName()] = $aggregation->toArray(); |
|
|
|
|
214
|
|
|
if ($aggregation->hasPipelines('parent')) { |
|
|
|
|
215
|
|
|
foreach ($aggregation->getParentPipelines() as $pipeline) { |
|
|
|
|
216
|
|
|
$result[$pipeline->getName()] = $pipeline->toArray(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
foreach ($this->getParentPipelines() as $pipeline) { |
221
|
|
|
$result[$pipeline->getName()] = $pipeline->toArray(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $result; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Creates BuilderBag new instance. |
229
|
|
|
* |
230
|
|
|
* @return BuilderBag |
231
|
|
|
*/ |
232
|
|
|
private function createBuilderBag() |
233
|
|
|
{ |
234
|
|
|
return new BuilderBag(); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.