/
FacetSet.php
292 lines (268 loc) · 10.1 KB
/
FacetSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Component\ResponseParser;
use Solarium\QueryType\Select\Query\Query;
use Solarium\Component\FacetSet as QueryFacetSet;
use Solarium\Component\Facet\Field as QueryFacetField;
use Solarium\Component\Facet\Query as QueryFacetQuery;
use Solarium\Component\Facet\MultiQuery as QueryFacetMultiQuery;
use Solarium\Component\Facet\Range as QueryFacetRange;
use Solarium\Component\Facet\Pivot as QueryFacetPivot;
use Solarium\Component\Result\FacetSet as ResultFacetSet;
use Solarium\Component\Result\Facet\Field as ResultFacetField;
use Solarium\Component\Result\Facet\Query as ResultFacetQuery;
use Solarium\Component\Result\Facet\MultiQuery as ResultFacetMultiQuery;
use Solarium\Component\Result\Facet\Range as ResultFacetRange;
use Solarium\Component\Result\Facet\Pivot\Pivot as ResultFacetPivot;
use Solarium\Component\Result\Facet\Interval as ResultFacetInterval;
use Solarium\Exception\RuntimeException;
use Solarium\Core\Query\AbstractResponseParser as ResponseParserAbstract;
/**
* Parse select component FacetSet result from the data.
*/
class FacetSet extends ResponseParserAbstract implements ComponentParserInterface
{
/**
* Parse result data into result objects.
*
* @throws RuntimeException
*
* @param Query $query
* @param QueryFacetSet $facetSet
* @param array $data
*
* @return ResultFacetSet
*/
public function parse($query, $facetSet, $data)
{
if ($facetSet->getExtractFromResponse() === true) {
if (empty($data['facet_counts']) === false) {
foreach ($data['facet_counts'] as $key => $facets) {
switch ($key) {
case 'facet_fields':
$method = 'createFacetField';
break;
case 'facet_queries':
$method = 'createFacetQuery';
break;
case 'facet_ranges':
$method = 'createFacetRange';
break;
case 'facet_pivot':
$method = 'createFacetPivot';
break;
case 'facet_interval':
$method = 'createFacetInterval';
break;
default:
throw new RuntimeException('Unknown facet class identifier');
}
foreach ($facets as $k => $facet) {
$facetObject = $facetSet->$method($k);
if ($key == 'facet_pivot') {
/* @var \Solarium\Component\Facet\Pivot $facetObject */
$facetObject->setFields($k);
}
}
}
}
}
$facets = array();
foreach ($facetSet->getFacets() as $key => $facet) {
switch ($facet->getType()) {
case QueryFacetSet::FACET_FIELD:
$result = $this->facetField($query, $facet, $data);
break;
case QueryFacetSet::FACET_QUERY:
$result = $this->facetQuery($facet, $data);
break;
case QueryFacetSet::FACET_MULTIQUERY:
$result = $this->facetMultiQuery($facet, $data);
break;
case QueryFacetSet::FACET_RANGE:
$result = $this->facetRange($query, $facet, $data);
break;
case QueryFacetSet::FACET_PIVOT:
$result = $this->facetPivot($query, $facet, $data);
break;
case QueryFacetSet::FACET_INTERVAL:
$result = $this->facetInterval($query, $facet, $data);
break;
default:
throw new RuntimeException('Unknown facet type');
}
if ($result !== null) {
$facets[$key] = $result;
}
}
return $this->createFacetSet($facets);
}
/**
* Create a facetset result object.
*
* @param array $facets
*
* @return ResultFacetSet
*/
protected function createFacetSet($facets)
{
return new ResultFacetSet($facets);
}
/**
* Add a facet result for a field facet.
*
* @param Query $query
* @param QueryFacetField $facet
* @param array $data
*
* @return ResultFacetField|null
*/
protected function facetField($query, $facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_fields'][$key])) {
return;
}
if ($query->getResponseWriter() == $query::WT_JSON) {
$data['facet_counts']['facet_fields'][$key] = $this->convertToKeyValueArray(
$data['facet_counts']['facet_fields'][$key]
);
}
return new ResultFacetField($data['facet_counts']['facet_fields'][$key]);
}
/**
* Add a facet result for a facet query.
*
* @param QueryFacetQuery $facet
* @param array $data
*
* @return ResultFacetQuery|null
*/
protected function facetQuery($facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_queries'][$key])) {
return;
}
return new ResultFacetQuery($data['facet_counts']['facet_queries'][$key]);
}
/**
* Add a facet result for a multiquery facet.
*
* @param QueryFacetMultiQuery $facet
* @param array $data
*
* @return ResultFacetMultiQuery|null
*/
protected function facetMultiQuery($facet, $data)
{
$values = array();
foreach ($facet->getQueries() as $query) {
$key = $query->getKey();
if (isset($data['facet_counts']['facet_queries'][$key])) {
$count = $data['facet_counts']['facet_queries'][$key];
$values[$key] = $count;
}
}
if (count($values) <= 0) {
return;
}
return new ResultFacetMultiQuery($values);
}
/**
* Add a facet result for a range facet.
*
* @param Query $query
* @param QueryFacetRange $facet
* @param array $data
*
* @return ResultFacetRange|null
*/
protected function facetRange($query, $facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_ranges'][$key])) {
return;
}
$data = $data['facet_counts']['facet_ranges'][$key];
$before = (isset($data['before'])) ? $data['before'] : null;
$after = (isset($data['after'])) ? $data['after'] : null;
$between = (isset($data['between'])) ? $data['between'] : null;
$start = (isset($data['start'])) ? $data['start'] : null;
$end = (isset($data['end'])) ? $data['end'] : null;
$gap = (isset($data['gap'])) ? $data['gap'] : null;
if ($query->getResponseWriter() == $query::WT_JSON) {
$data['counts'] = $this->convertToKeyValueArray($data['counts']);
}
return new ResultFacetRange($data['counts'], $before, $after, $between, $start, $end, $gap);
}
/**
* Add a facet result for a interval facet
*
* @param Query $query
* @param QueryFacetInterval $facet
* @param array $data
* @return ResultFacetInterval|null
*/
protected function facetInterval($query, $facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_intervals'][$key])) {
return null;
}
return new ResultFacetInterval($data['facet_counts']['facet_intervals'][$key]);
}
/**
* Add a facet result for a range facet.
*
* @param Query $query
* @param QueryFacetPivot $facet
* @param array $data
*
* @return ResultFacetPivot|null
*/
protected function facetPivot($query, $facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_pivot'][$key])) {
return;
}
return new ResultFacetPivot($data['facet_counts']['facet_pivot'][$key]);
}
}