1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | * |
---|
31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
33 | * @link http://www.solarium-project.org/ |
---|
34 | * |
---|
35 | * @package Solarium |
---|
36 | * @subpackage Query |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Select Query |
---|
41 | * |
---|
42 | * Can be used to select documents and/or facets from Solr. This querytype has |
---|
43 | * lots of options and there are many Solarium subclasses for it. |
---|
44 | * See the Solr documentation and the relevant Solarium classes for more info. |
---|
45 | * |
---|
46 | * @package Solarium |
---|
47 | * @subpackage Query |
---|
48 | */ |
---|
49 | class Solarium_Query_Select extends Solarium_Query |
---|
50 | { |
---|
51 | |
---|
52 | /** |
---|
53 | * Solr sort mode descending |
---|
54 | */ |
---|
55 | const SORT_DESC = 'desc'; |
---|
56 | |
---|
57 | /** |
---|
58 | * Solr sort mode ascending |
---|
59 | */ |
---|
60 | const SORT_ASC = 'asc'; |
---|
61 | |
---|
62 | /** |
---|
63 | * Solr query operator AND |
---|
64 | */ |
---|
65 | const QUERY_OPERATOR_AND = 'AND'; |
---|
66 | |
---|
67 | /** |
---|
68 | * Solr query operator OR |
---|
69 | */ |
---|
70 | const QUERY_OPERATOR_OR = 'OR'; |
---|
71 | |
---|
72 | /** |
---|
73 | * Query component facetset |
---|
74 | */ |
---|
75 | const COMPONENT_FACETSET = 'facetset'; |
---|
76 | |
---|
77 | /** |
---|
78 | * Query component dismax |
---|
79 | */ |
---|
80 | const COMPONENT_DISMAX = 'dismax'; |
---|
81 | |
---|
82 | /** |
---|
83 | * Query component morelikethis |
---|
84 | */ |
---|
85 | const COMPONENT_MORELIKETHIS = 'morelikethis'; |
---|
86 | |
---|
87 | /** |
---|
88 | * Query component highlighting |
---|
89 | */ |
---|
90 | const COMPONENT_HIGHLIGHTING = 'highlighting'; |
---|
91 | |
---|
92 | /** |
---|
93 | * Query component spellcheck |
---|
94 | */ |
---|
95 | const COMPONENT_SPELLCHECK = 'spellcheck'; |
---|
96 | |
---|
97 | /** |
---|
98 | * Query component grouping |
---|
99 | */ |
---|
100 | const COMPONENT_GROUPING = 'grouping'; |
---|
101 | |
---|
102 | /** |
---|
103 | * Query component distributed search |
---|
104 | */ |
---|
105 | const COMPONENT_DISTRIBUTEDSEARCH = 'distributedsearch'; |
---|
106 | |
---|
107 | /** |
---|
108 | * Query component stats |
---|
109 | */ |
---|
110 | const COMPONENT_STATS = 'stats'; |
---|
111 | |
---|
112 | /** |
---|
113 | * Query component debug |
---|
114 | */ |
---|
115 | const COMPONENT_DEBUG = 'debug'; |
---|
116 | |
---|
117 | /** |
---|
118 | * Get type for this query |
---|
119 | * |
---|
120 | * @return string |
---|
121 | */ |
---|
122 | public function getType() |
---|
123 | { |
---|
124 | return Solarium_Client::QUERYTYPE_SELECT; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Default options |
---|
129 | * |
---|
130 | * @var array |
---|
131 | */ |
---|
132 | protected $_options = array( |
---|
133 | 'handler' => 'select', |
---|
134 | 'resultclass' => 'Solarium_Result_Select', |
---|
135 | 'documentclass' => 'Solarium_Document_ReadOnly', |
---|
136 | 'query' => '*:*', |
---|
137 | 'start' => 0, |
---|
138 | 'rows' => 10, |
---|
139 | 'fields' => '*,score', |
---|
140 | ); |
---|
141 | |
---|
142 | /** |
---|
143 | * Default select query component types |
---|
144 | * |
---|
145 | * @var array |
---|
146 | */ |
---|
147 | protected $_componentTypes = array( |
---|
148 | self::COMPONENT_FACETSET => array( |
---|
149 | 'component' => 'Solarium_Query_Select_Component_FacetSet', |
---|
150 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_FacetSet', |
---|
151 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_FacetSet', |
---|
152 | ), |
---|
153 | self::COMPONENT_DISMAX => array( |
---|
154 | 'component' => 'Solarium_Query_Select_Component_DisMax', |
---|
155 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DisMax', |
---|
156 | 'responseparser' => null, |
---|
157 | ), |
---|
158 | self::COMPONENT_MORELIKETHIS => array( |
---|
159 | 'component' => 'Solarium_Query_Select_Component_MoreLikeThis', |
---|
160 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis', |
---|
161 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_MoreLikeThis', |
---|
162 | ), |
---|
163 | self::COMPONENT_HIGHLIGHTING => array( |
---|
164 | 'component' => 'Solarium_Query_Select_Component_Highlighting', |
---|
165 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Highlighting', |
---|
166 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Highlighting', |
---|
167 | ), |
---|
168 | self::COMPONENT_GROUPING => array( |
---|
169 | 'component' => 'Solarium_Query_Select_Component_Grouping', |
---|
170 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Grouping', |
---|
171 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Grouping', |
---|
172 | ), |
---|
173 | self::COMPONENT_SPELLCHECK => array( |
---|
174 | 'component' => 'Solarium_Query_Select_Component_Spellcheck', |
---|
175 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Spellcheck', |
---|
176 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Spellcheck', |
---|
177 | ), |
---|
178 | self::COMPONENT_DISTRIBUTEDSEARCH => array( |
---|
179 | 'component' => 'Solarium_Query_Select_Component_DistributedSearch', |
---|
180 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DistributedSearch', |
---|
181 | 'responseparser' => null, |
---|
182 | ), |
---|
183 | self::COMPONENT_STATS => array( |
---|
184 | 'component' => 'Solarium_Query_Select_Component_Stats', |
---|
185 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Stats', |
---|
186 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Stats', |
---|
187 | ), |
---|
188 | self::COMPONENT_DEBUG => array( |
---|
189 | 'component' => 'Solarium_Query_Select_Component_Debug', |
---|
190 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Debug', |
---|
191 | 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Debug', |
---|
192 | ), |
---|
193 | ); |
---|
194 | |
---|
195 | /** |
---|
196 | * Fields to fetch |
---|
197 | * |
---|
198 | * @var array |
---|
199 | */ |
---|
200 | protected $_fields = array(); |
---|
201 | |
---|
202 | /** |
---|
203 | * Items to sort on |
---|
204 | * |
---|
205 | * @var array |
---|
206 | */ |
---|
207 | protected $_sorts = array(); |
---|
208 | |
---|
209 | /** |
---|
210 | * Filterqueries |
---|
211 | * |
---|
212 | * @var array |
---|
213 | */ |
---|
214 | protected $_filterQueries = array(); |
---|
215 | |
---|
216 | /** |
---|
217 | * Search components |
---|
218 | * |
---|
219 | * @var array |
---|
220 | */ |
---|
221 | protected $_components = array(); |
---|
222 | |
---|
223 | /** |
---|
224 | * Initialize options |
---|
225 | * |
---|
226 | * Several options need some extra checks or setup work, for these options |
---|
227 | * the setters are called. |
---|
228 | * |
---|
229 | * @return void |
---|
230 | */ |
---|
231 | protected function _init() |
---|
232 | { |
---|
233 | foreach ($this->_options AS $name => $value) { |
---|
234 | switch ($name) { |
---|
235 | case 'query': |
---|
236 | $this->setQuery($value); |
---|
237 | break; |
---|
238 | case 'filterquery': |
---|
239 | $this->addFilterQueries($value); |
---|
240 | break; |
---|
241 | case 'sort': |
---|
242 | $this->addSorts($value); |
---|
243 | break; |
---|
244 | case 'fields': |
---|
245 | $this->addFields($value); |
---|
246 | break; |
---|
247 | case 'rows': |
---|
248 | $this->setRows((int)$value); |
---|
249 | break; |
---|
250 | case 'start': |
---|
251 | $this->setStart((int)$value); |
---|
252 | break; |
---|
253 | case 'component': |
---|
254 | $this->_createComponents($value); |
---|
255 | break; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * Set the query string |
---|
262 | * |
---|
263 | * Overwrites the current value. You are responsible for the correct |
---|
264 | * escaping of user input. |
---|
265 | * |
---|
266 | * @param string $query |
---|
267 | * @param array $bind Bind values for placeholders in the query string |
---|
268 | * @return Solarium_Query_Select Provides fluent interface |
---|
269 | */ |
---|
270 | public function setQuery($query, $bind = null) |
---|
271 | { |
---|
272 | if (!is_null($bind)) { |
---|
273 | $query = $this->getHelper()->assemble($query, $bind); |
---|
274 | } |
---|
275 | |
---|
276 | return $this->_setOption('query', trim($query)); |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * Get the query string |
---|
281 | * |
---|
282 | * @return string |
---|
283 | */ |
---|
284 | public function getQuery() |
---|
285 | { |
---|
286 | return $this->getOption('query'); |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * Set default query operator |
---|
291 | * |
---|
292 | * Use one of the constants as value |
---|
293 | * |
---|
294 | * @param string $operator |
---|
295 | * @return Solarium_Query_Select Provides fluent interface |
---|
296 | */ |
---|
297 | public function setQueryDefaultOperator($operator) |
---|
298 | { |
---|
299 | return $this->_setOption('querydefaultoperator', $operator); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Get the default query operator |
---|
304 | * |
---|
305 | * @return null|string |
---|
306 | */ |
---|
307 | public function getQueryDefaultOperator() |
---|
308 | { |
---|
309 | return $this->getOption('querydefaultoperator'); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * Set default query field |
---|
314 | * |
---|
315 | * @param string $field |
---|
316 | * @return Solarium_Query_Select Provides fluent interface |
---|
317 | */ |
---|
318 | public function setQueryDefaultField($field) |
---|
319 | { |
---|
320 | return $this->_setOption('querydefaultfield', $field); |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * Get the default query field |
---|
325 | * |
---|
326 | * @return null|string |
---|
327 | */ |
---|
328 | public function getQueryDefaultField() |
---|
329 | { |
---|
330 | return $this->getOption('querydefaultfield'); |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Set the start offset |
---|
335 | * |
---|
336 | * @param integer $start |
---|
337 | * @return Solarium_Query_Select Provides fluent interface |
---|
338 | */ |
---|
339 | public function setStart($start) |
---|
340 | { |
---|
341 | return $this->_setOption('start', $start); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Get the start offset |
---|
346 | * |
---|
347 | * @return integer |
---|
348 | */ |
---|
349 | public function getStart() |
---|
350 | { |
---|
351 | return $this->getOption('start'); |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * Set a custom resultclass |
---|
356 | * |
---|
357 | * @param string $value classname |
---|
358 | * @return Solarium_Query_Select Provides fluent interface |
---|
359 | */ |
---|
360 | public function setResultClass($value) |
---|
361 | { |
---|
362 | return $this->_setOption('resultclass', $value); |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Get the current resultclass option |
---|
367 | * |
---|
368 | * The value is a classname, not an instance |
---|
369 | * |
---|
370 | * @return string |
---|
371 | */ |
---|
372 | public function getResultClass() |
---|
373 | { |
---|
374 | return $this->getOption('resultclass'); |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | * Set a custom document class |
---|
379 | * |
---|
380 | * @param string $value classname |
---|
381 | * @return Solarium_Query |
---|
382 | */ |
---|
383 | public function setDocumentClass($value) |
---|
384 | { |
---|
385 | return $this->_setOption('documentclass', $value); |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * Get the current documentclass option |
---|
390 | * |
---|
391 | * The value is a classname, not an instance |
---|
392 | * |
---|
393 | * @return string |
---|
394 | */ |
---|
395 | public function getDocumentClass() |
---|
396 | { |
---|
397 | return $this->getOption('documentclass'); |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * Set the number of rows to fetch |
---|
402 | * |
---|
403 | * @param integer $rows |
---|
404 | * @return Solarium_Query_Select Provides fluent interface |
---|
405 | */ |
---|
406 | public function setRows($rows) |
---|
407 | { |
---|
408 | return $this->_setOption('rows', $rows); |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * Get the number of rows |
---|
413 | * |
---|
414 | * @return integer |
---|
415 | */ |
---|
416 | public function getRows() |
---|
417 | { |
---|
418 | return $this->getOption('rows'); |
---|
419 | } |
---|
420 | |
---|
421 | /** |
---|
422 | * Specify a field to return in the resultset |
---|
423 | * |
---|
424 | * @param string $field |
---|
425 | * @return Solarium_Query_Select Provides fluent interface |
---|
426 | */ |
---|
427 | public function addField($field) |
---|
428 | { |
---|
429 | $this->_fields[$field] = true; |
---|
430 | return $this; |
---|
431 | } |
---|
432 | |
---|
433 | /** |
---|
434 | * Specify multiple fields to return in the resultset |
---|
435 | * |
---|
436 | * @param string|array $fields can be an array or string with comma |
---|
437 | * separated fieldnames |
---|
438 | * |
---|
439 | * @return Solarium_Query_Select Provides fluent interface |
---|
440 | */ |
---|
441 | public function addFields($fields) |
---|
442 | { |
---|
443 | if (is_string($fields)) { |
---|
444 | $fields = explode(',', $fields); |
---|
445 | $fields = array_map('trim', $fields); |
---|
446 | } |
---|
447 | |
---|
448 | foreach ($fields AS $field) { |
---|
449 | $this->addField($field); |
---|
450 | } |
---|
451 | |
---|
452 | return $this; |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | * Remove a field from the field list |
---|
457 | * |
---|
458 | * @param string $field |
---|
459 | * @return Solarium_Query_Select Provides fluent interface |
---|
460 | */ |
---|
461 | public function removeField($field) |
---|
462 | { |
---|
463 | if (isset($this->_fields[$field])) { |
---|
464 | unset($this->_fields[$field]); |
---|
465 | } |
---|
466 | |
---|
467 | return $this; |
---|
468 | } |
---|
469 | |
---|
470 | /** |
---|
471 | * Remove all fields from the field list. |
---|
472 | * |
---|
473 | * @return Solarium_Query_Select Provides fluent interface |
---|
474 | */ |
---|
475 | public function clearFields() |
---|
476 | { |
---|
477 | $this->_fields = array(); |
---|
478 | return $this; |
---|
479 | } |
---|
480 | |
---|
481 | /** |
---|
482 | * Get the list of fields |
---|
483 | * |
---|
484 | * @return array |
---|
485 | */ |
---|
486 | public function getFields() |
---|
487 | { |
---|
488 | return array_keys($this->_fields); |
---|
489 | } |
---|
490 | |
---|
491 | /** |
---|
492 | * Set multiple fields |
---|
493 | * |
---|
494 | * This overwrites any existing fields |
---|
495 | * |
---|
496 | * @param array $fields |
---|
497 | * @return Solarium_Query_Select Provides fluent interface |
---|
498 | */ |
---|
499 | public function setFields($fields) |
---|
500 | { |
---|
501 | $this->clearFields(); |
---|
502 | $this->addFields($fields); |
---|
503 | |
---|
504 | return $this; |
---|
505 | } |
---|
506 | |
---|
507 | /** |
---|
508 | * Add a sort |
---|
509 | * |
---|
510 | * @param string $sort |
---|
511 | * @param string $order |
---|
512 | * @return Solarium_Query_Select Provides fluent interface |
---|
513 | */ |
---|
514 | public function addSort($sort, $order) |
---|
515 | { |
---|
516 | $this->_sorts[$sort] = $order; |
---|
517 | |
---|
518 | return $this; |
---|
519 | } |
---|
520 | |
---|
521 | /** |
---|
522 | * Add multiple sorts |
---|
523 | * |
---|
524 | * The input array must contain sort items as keys and the order as values. |
---|
525 | * |
---|
526 | * @param array $sorts |
---|
527 | * @return Solarium_Query_Select Provides fluent interface |
---|
528 | */ |
---|
529 | public function addSorts(array $sorts) |
---|
530 | { |
---|
531 | foreach ($sorts AS $sort => $order) { |
---|
532 | $this->addSort($sort, $order); |
---|
533 | } |
---|
534 | |
---|
535 | return $this; |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * Remove a sort |
---|
540 | * |
---|
541 | * @param string $sort |
---|
542 | * @return Solarium_Query_Select Provides fluent interface |
---|
543 | */ |
---|
544 | public function removeSort($sort) |
---|
545 | { |
---|
546 | if (isset($this->_sorts[$sort])) { |
---|
547 | unset($this->_sorts[$sort]); |
---|
548 | } |
---|
549 | |
---|
550 | return $this; |
---|
551 | } |
---|
552 | |
---|
553 | /** |
---|
554 | * Remove all sorts |
---|
555 | * |
---|
556 | * @return Solarium_Query_Select Provides fluent interface |
---|
557 | */ |
---|
558 | public function clearSorts() |
---|
559 | { |
---|
560 | $this->_sorts = array(); |
---|
561 | return $this; |
---|
562 | } |
---|
563 | |
---|
564 | /** |
---|
565 | * Get a list of the sorts |
---|
566 | * |
---|
567 | * @return array |
---|
568 | */ |
---|
569 | public function getSorts() |
---|
570 | { |
---|
571 | return $this->_sorts; |
---|
572 | } |
---|
573 | |
---|
574 | /** |
---|
575 | * Set multiple sorts |
---|
576 | * |
---|
577 | * This overwrites any existing sorts |
---|
578 | * |
---|
579 | * @param array $sorts |
---|
580 | * @return Solarium_Query_Select Provides fluent interface |
---|
581 | */ |
---|
582 | public function setSorts($sorts) |
---|
583 | { |
---|
584 | $this->clearSorts(); |
---|
585 | $this->addSorts($sorts); |
---|
586 | |
---|
587 | return $this; |
---|
588 | } |
---|
589 | |
---|
590 | /** |
---|
591 | * Create a filterquery instance |
---|
592 | * |
---|
593 | * If you supply a string as the first arguments ($options) it will be used as the key for the filterquery |
---|
594 | * and it will be added to this query. |
---|
595 | * If you supply an options array/object that contains a key the filterquery will also be added to the query. |
---|
596 | * |
---|
597 | * When no key is supplied the filterquery cannot be added, in that case you will need to add it manually |
---|
598 | * after setting the key, by using the addFilterQuery method. |
---|
599 | * |
---|
600 | * @param mixed $options |
---|
601 | * @return Solarium_Query_Select_FilterQuery |
---|
602 | */ |
---|
603 | public function createFilterQuery($options = null) |
---|
604 | { |
---|
605 | if (is_string($options)) { |
---|
606 | $fq = new Solarium_Query_Select_FilterQuery; |
---|
607 | $fq->setKey($options); |
---|
608 | } else { |
---|
609 | $fq = new Solarium_Query_Select_FilterQuery($options); |
---|
610 | } |
---|
611 | |
---|
612 | if ($fq->getKey() !== null) { |
---|
613 | $this->addFilterQuery($fq); |
---|
614 | } |
---|
615 | |
---|
616 | return $fq; |
---|
617 | } |
---|
618 | |
---|
619 | /** |
---|
620 | * Add a filter query |
---|
621 | * |
---|
622 | * Supports a filterquery instance or a config array, in that case a new |
---|
623 | * filterquery instance wil be created based on the options. |
---|
624 | * |
---|
625 | * @param Solarium_Query_Select_FilterQuery|array $filterQuery |
---|
626 | * @return Solarium_Query_Select Provides fluent interface |
---|
627 | */ |
---|
628 | public function addFilterQuery($filterQuery) |
---|
629 | { |
---|
630 | if (is_array($filterQuery)) { |
---|
631 | $filterQuery = new Solarium_Query_Select_FilterQuery($filterQuery); |
---|
632 | } |
---|
633 | |
---|
634 | $key = $filterQuery->getKey(); |
---|
635 | |
---|
636 | if (0 === strlen($key)) { |
---|
637 | throw new Solarium_Exception('A filterquery must have a key value'); |
---|
638 | } |
---|
639 | |
---|
640 | //double add calls for the same FQ are ignored, but non-unique keys cause an exception |
---|
641 | //@todo add trigger_error with a notice for double add calls? |
---|
642 | if (array_key_exists($key, $this->_filterQueries) && $this->_filterQueries[$key] !== $filterQuery) { |
---|
643 | throw new Solarium_Exception('A filterquery must have a unique key value within a query'); |
---|
644 | } else { |
---|
645 | $this->_filterQueries[$key] = $filterQuery; |
---|
646 | } |
---|
647 | |
---|
648 | return $this; |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | * Add multiple filterqueries |
---|
653 | * |
---|
654 | * @param array $filterQueries |
---|
655 | * @return Solarium_Query_Select Provides fluent interface |
---|
656 | */ |
---|
657 | public function addFilterQueries(array $filterQueries) |
---|
658 | { |
---|
659 | foreach ($filterQueries AS $key => $filterQuery) { |
---|
660 | |
---|
661 | // in case of a config array: add key to config |
---|
662 | if (is_array($filterQuery) && !isset($filterQuery['key'])) { |
---|
663 | $filterQuery['key'] = $key; |
---|
664 | } |
---|
665 | |
---|
666 | $this->addFilterQuery($filterQuery); |
---|
667 | } |
---|
668 | |
---|
669 | return $this; |
---|
670 | } |
---|
671 | |
---|
672 | /** |
---|
673 | * Get a filterquery |
---|
674 | * |
---|
675 | * @param string $key |
---|
676 | * @return string |
---|
677 | */ |
---|
678 | public function getFilterQuery($key) |
---|
679 | { |
---|
680 | if (isset($this->_filterQueries[$key])) { |
---|
681 | return $this->_filterQueries[$key]; |
---|
682 | } else { |
---|
683 | return null; |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | /** |
---|
688 | * Get all filterqueries |
---|
689 | * |
---|
690 | * @return array |
---|
691 | */ |
---|
692 | public function getFilterQueries() |
---|
693 | { |
---|
694 | return $this->_filterQueries; |
---|
695 | } |
---|
696 | |
---|
697 | /** |
---|
698 | * Remove a single filterquery |
---|
699 | * |
---|
700 | * You can remove a filterquery by passing it's key, or by passing the filterquery instance |
---|
701 | * |
---|
702 | * @param string|Solarium_Query_Select_FilterQuery $filterQuery |
---|
703 | * @return Solarium_Query_Select Provides fluent interface |
---|
704 | */ |
---|
705 | public function removeFilterQuery($filterQuery) |
---|
706 | { |
---|
707 | if (is_object($filterQuery)) { |
---|
708 | $filterQuery = $filterQuery->getKey(); |
---|
709 | } |
---|
710 | |
---|
711 | if (isset($this->_filterQueries[$filterQuery])) { |
---|
712 | unset($this->_filterQueries[$filterQuery]); |
---|
713 | } |
---|
714 | |
---|
715 | return $this; |
---|
716 | } |
---|
717 | |
---|
718 | /** |
---|
719 | * Remove all filterqueries |
---|
720 | * |
---|
721 | * @return Solarium_Query_Select Provides fluent interface |
---|
722 | */ |
---|
723 | public function clearFilterQueries() |
---|
724 | { |
---|
725 | $this->_filterQueries = array(); |
---|
726 | return $this; |
---|
727 | } |
---|
728 | |
---|
729 | /** |
---|
730 | * Set multiple filterqueries |
---|
731 | * |
---|
732 | * This overwrites any existing filterqueries |
---|
733 | * |
---|
734 | * @param array $filterQueries |
---|
735 | */ |
---|
736 | public function setFilterQueries($filterQueries) |
---|
737 | { |
---|
738 | $this->clearFilterQueries(); |
---|
739 | $this->addFilterQueries($filterQueries); |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * Get all registered component types |
---|
744 | * |
---|
745 | * @return array |
---|
746 | */ |
---|
747 | public function getComponentTypes() |
---|
748 | { |
---|
749 | return $this->_componentTypes; |
---|
750 | } |
---|
751 | |
---|
752 | /** |
---|
753 | * Register a component type |
---|
754 | * |
---|
755 | * @param string $key |
---|
756 | * @param string $component |
---|
757 | * @param string $requestBuilder |
---|
758 | * @param string $responseParser |
---|
759 | * @return Solarium_Query_Select Provides fluent interface |
---|
760 | */ |
---|
761 | public function registerComponentType($key, $component, $requestBuilder=null, $responseParser=null) |
---|
762 | { |
---|
763 | $this->_componentTypes[$key] = array( |
---|
764 | 'component' => $component, |
---|
765 | 'requestbuilder' => $requestBuilder, |
---|
766 | 'responseparser' => $responseParser, |
---|
767 | ); |
---|
768 | |
---|
769 | return $this; |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | * Get all registered components |
---|
774 | * |
---|
775 | * @return array |
---|
776 | */ |
---|
777 | public function getComponents() |
---|
778 | { |
---|
779 | return $this->_components; |
---|
780 | } |
---|
781 | |
---|
782 | /** |
---|
783 | * Get a component instance by key |
---|
784 | * |
---|
785 | * You can optionally supply an autoload class to create a new component |
---|
786 | * instance if there is no registered component for the given key yet. |
---|
787 | * |
---|
788 | * @param string $key Use one of the constants |
---|
789 | * @param string $autoload Class to autoload if component needs to be created |
---|
790 | * @param array $config Configuration to use for autoload |
---|
791 | * @return object|null |
---|
792 | */ |
---|
793 | public function getComponent($key, $autoload = false, $config = null) |
---|
794 | { |
---|
795 | if (isset($this->_components[$key])) { |
---|
796 | return $this->_components[$key]; |
---|
797 | } else { |
---|
798 | if ($autoload == true) { |
---|
799 | |
---|
800 | if (!isset($this->_componentTypes[$key])) { |
---|
801 | throw new Solarium_Exception('Cannot autoload unknown component: ' . $key); |
---|
802 | } |
---|
803 | |
---|
804 | $className = $this->_componentTypes[$key]['component']; |
---|
805 | $component = new $className($config); |
---|
806 | $this->setComponent($key, $component); |
---|
807 | return $component; |
---|
808 | } |
---|
809 | return null; |
---|
810 | } |
---|
811 | } |
---|
812 | |
---|
813 | /** |
---|
814 | * Set a component instance |
---|
815 | * |
---|
816 | * This overwrites any existing component registered with the same key. |
---|
817 | * |
---|
818 | * @param string $key |
---|
819 | * @param object|null $value |
---|
820 | * @return Solarium_Query_Select Provides fluent interface |
---|
821 | */ |
---|
822 | public function setComponent($key, $value) |
---|
823 | { |
---|
824 | $this->_components[$key] = $value; |
---|
825 | return $this; |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * Remove a component instance |
---|
830 | * |
---|
831 | * You can remove a component by passing it's key or the component instance |
---|
832 | * |
---|
833 | * @param string|Solarium_Query_Select_Component $component |
---|
834 | * @return Solarium_Query_Select Provides fluent interface |
---|
835 | */ |
---|
836 | public function removeComponent($component) |
---|
837 | { |
---|
838 | if (is_object($component)) { |
---|
839 | foreach ($this->_components as $key => $instance) { |
---|
840 | if ($instance === $component) { |
---|
841 | unset($this->_components[$key]); |
---|
842 | break; |
---|
843 | } |
---|
844 | } |
---|
845 | } else { |
---|
846 | if (isset($this->_components[$component])) { |
---|
847 | unset($this->_components[$component]); |
---|
848 | } |
---|
849 | } |
---|
850 | return $this; |
---|
851 | } |
---|
852 | |
---|
853 | |
---|
854 | /** |
---|
855 | * Build component instances based on config |
---|
856 | * |
---|
857 | * @param array $configs |
---|
858 | * @return void |
---|
859 | */ |
---|
860 | protected function _createComponents($configs) |
---|
861 | { |
---|
862 | foreach ($configs AS $type => $config) { |
---|
863 | $this->getComponent($type, true, $config); |
---|
864 | } |
---|
865 | } |
---|
866 | |
---|
867 | /** |
---|
868 | * Get a MoreLikeThis component instance |
---|
869 | * |
---|
870 | * This is a convenience method that maps presets to getComponent |
---|
871 | * |
---|
872 | * @return Solarium_Query_Select_Component_MoreLikeThis |
---|
873 | */ |
---|
874 | public function getMoreLikeThis() |
---|
875 | { |
---|
876 | return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS, true); |
---|
877 | } |
---|
878 | |
---|
879 | /** |
---|
880 | * Get a FacetSet component instance |
---|
881 | * |
---|
882 | * This is a convenience method that maps presets to getComponent |
---|
883 | * |
---|
884 | * @return Solarium_Query_Select_Component_FacetSet |
---|
885 | */ |
---|
886 | public function getFacetSet() |
---|
887 | { |
---|
888 | return $this->getComponent(Solarium_Query_Select::COMPONENT_FACETSET, true); |
---|
889 | } |
---|
890 | |
---|
891 | /** |
---|
892 | * Get a DisMax component instance |
---|
893 | * |
---|
894 | * This is a convenience method that maps presets to getComponent |
---|
895 | * |
---|
896 | * @return Solarium_Query_Select_Component_DisMax |
---|
897 | */ |
---|
898 | public function getDisMax() |
---|
899 | { |
---|
900 | return $this->getComponent(Solarium_Query_Select::COMPONENT_DISMAX, true); |
---|
901 | } |
---|
902 | |
---|
903 | /** |
---|
904 | * Get a highlighting component instance |
---|
905 | * |
---|
906 | * This is a convenience method that maps presets to getComponent |
---|
907 | * |
---|
908 | * @return Solarium_Query_Select_Component_Highlighting |
---|
909 | */ |
---|
910 | public function getHighlighting() |
---|
911 | { |
---|
912 | return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, true); |
---|
913 | } |
---|
914 | |
---|
915 | /** |
---|
916 | * Get a grouping component instance |
---|
917 | * |
---|
918 | * This is a convenience method that maps presets to getComponent |
---|
919 | * |
---|
920 | * @return Solarium_Query_Select_Component_Grouping |
---|
921 | */ |
---|
922 | public function getGrouping() |
---|
923 | { |
---|
924 | return $this->getComponent(Solarium_Query_Select::COMPONENT_GROUPING, true); |
---|
925 | } |
---|
926 | |
---|
927 | /** |
---|
928 | * Get a spellcheck component instance |
---|
929 | * |
---|
930 | * This is a convenience method that maps presets to getComponent |
---|
931 | * |
---|
932 | * @return Solarium_Query_Select_Component_Spellcheck |
---|
933 | */ |
---|
934 | public function getSpellcheck() |
---|
935 | { |
---|
936 | return $this->getComponent(Solarium_Query_Select::COMPONENT_SPELLCHECK, true); |
---|
937 | } |
---|
938 | |
---|
939 | /** |
---|
940 | * Get a DistributedSearch component instance |
---|
941 | * |
---|
942 | * This is a convenience method that maps presets to getComponent |
---|
943 | * |
---|
944 | * @return Solarium_Query_Select_Component_DistributedSearch |
---|
945 | */ |
---|
946 | public function getDistributedSearch() |
---|
947 | { |
---|
948 | return $this->getComponent(Solarium_Query_Select::COMPONENT_DISTRIBUTEDSEARCH, true); |
---|
949 | } |
---|
950 | |
---|
951 | /** |
---|
952 | * Get a Stats component instance |
---|
953 | * |
---|
954 | * This is a convenience method that maps presets to getComponent |
---|
955 | * |
---|
956 | * @return Solarium_Query_Select_Component_Stats |
---|
957 | */ |
---|
958 | public function getStats() |
---|
959 | { |
---|
960 | return $this->getComponent(Solarium_Query_Select::COMPONENT_STATS, true); |
---|
961 | } |
---|
962 | |
---|
963 | /** |
---|
964 | * Get a Debug component instance |
---|
965 | * |
---|
966 | * This is a convenience method that maps presets to getComponent |
---|
967 | * |
---|
968 | * @return Solarium_Query_Select_Component_Debug |
---|
969 | */ |
---|
970 | public function getDebug() |
---|
971 | { |
---|
972 | return $this->getComponent(Solarium_Query_Select::COMPONENT_DEBUG, true); |
---|
973 | } |
---|
974 | |
---|
975 | } |
---|