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\ElasticsearchBundle\Result\Aggregation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the class for plain aggregation result with nested aggregations support. |
16
|
|
|
*/ |
17
|
|
|
class AggregationValue implements \ArrayAccess, \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $rawData; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $rawData |
28
|
|
|
*/ |
29
|
|
|
public function __construct($rawData) |
30
|
|
|
{ |
31
|
|
|
$this->rawData = $rawData; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns aggregation value by name. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getValue($name) |
42
|
|
|
{ |
43
|
|
|
if (!isset($this->rawData[$name])) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->rawData[$name]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns array of bucket values. |
52
|
|
|
* |
53
|
|
|
* @return AggregationValue[]|null |
54
|
|
|
*/ |
55
|
|
|
public function getBuckets() |
56
|
|
|
{ |
57
|
|
|
if (!isset($this->rawData['buckets'])) { |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$buckets = []; |
62
|
|
|
|
63
|
|
|
foreach ($this->rawData['buckets'] as $bucket) { |
64
|
|
|
$buckets[] = new self($bucket); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $buckets; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns sub-aggregation. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return AggregationValue|null |
76
|
|
|
*/ |
77
|
|
|
public function getAggregation($name) |
78
|
|
|
{ |
79
|
|
|
if (!isset($this->rawData[$name])) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return new self($this->rawData[$name]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Applies path method to aggregations. |
88
|
|
|
* |
89
|
|
|
* @param string $path |
90
|
|
|
* |
91
|
|
|
* @return AggregationValue|null |
92
|
|
|
*/ |
93
|
|
|
public function find($path) |
94
|
|
|
{ |
95
|
|
|
$name = explode('.', $path, 2); |
96
|
|
|
$aggregation = $this->getAggregation($name[0]); |
97
|
|
|
|
98
|
|
|
if ($aggregation === null || !isset($name[1])) { |
99
|
|
|
return $aggregation; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $aggregation->find($name[1]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function offsetExists($offset) |
109
|
|
|
{ |
110
|
|
|
return array_key_exists($offset, $this->rawData); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function offsetGet($offset) |
117
|
|
|
{ |
118
|
|
|
if (!isset($this->rawData[$offset])) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->rawData[$offset]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function offsetSet($offset, $value) |
129
|
|
|
{ |
130
|
|
|
throw new \LogicException('Aggregation result can not be changed on runtime.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function offsetUnset($offset) |
137
|
|
|
{ |
138
|
|
|
throw new \LogicException('Aggregation result can not be changed on runtime.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getIterator() |
145
|
|
|
{ |
146
|
|
|
$buckets = $this->getBuckets(); |
147
|
|
|
|
148
|
|
|
if ($buckets === null) { |
149
|
|
|
throw new \LogicException('Can not iterate over aggregation without buckets!'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return new \ArrayIterator($this->getBuckets()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|