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 | * Facet MultiQuery |
---|
41 | * |
---|
42 | * This is a 'virtual' querytype that combines multiple facet queries into a |
---|
43 | * single resultset |
---|
44 | * |
---|
45 | * @package Solarium |
---|
46 | * @subpackage Query |
---|
47 | */ |
---|
48 | class Solarium_Query_Select_Component_Facet_MultiQuery extends Solarium_Query_Select_Component_Facet |
---|
49 | { |
---|
50 | |
---|
51 | /** |
---|
52 | * Facet query objects |
---|
53 | * |
---|
54 | * @var array |
---|
55 | */ |
---|
56 | protected $_facetQueries = array(); |
---|
57 | |
---|
58 | /** |
---|
59 | * Initialize options |
---|
60 | * |
---|
61 | * Several options need some extra checks or setup work, for these options |
---|
62 | * the setters are called. |
---|
63 | * |
---|
64 | * @return void |
---|
65 | */ |
---|
66 | protected function _init() |
---|
67 | { |
---|
68 | parent::_init(); |
---|
69 | |
---|
70 | foreach ($this->_options AS $name => $value) { |
---|
71 | switch ($name) { |
---|
72 | case 'query': |
---|
73 | if(!is_array($value)) $value = array($value); |
---|
74 | $this->addQueries($value); |
---|
75 | break; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Get the facet type |
---|
82 | * |
---|
83 | * @return string |
---|
84 | */ |
---|
85 | public function getType() |
---|
86 | { |
---|
87 | return Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Create a new facetQuery |
---|
92 | * |
---|
93 | * Convenience method so you don't need to manually create facetquery |
---|
94 | * objects. |
---|
95 | * |
---|
96 | * @param string $key |
---|
97 | * @param string $query |
---|
98 | * @param array $excludes |
---|
99 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
100 | */ |
---|
101 | public function createQuery($key, $query, $excludes = array()) |
---|
102 | { |
---|
103 | // merge excludes with shared excludes |
---|
104 | $excludes = array_merge($this->getExcludes(), $excludes); |
---|
105 | |
---|
106 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
107 | $facetQuery->setKey($key); |
---|
108 | $facetQuery->setQuery($query); |
---|
109 | $facetQuery->setExcludes($excludes); |
---|
110 | |
---|
111 | return $this->addQuery($facetQuery); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Add a facetquery |
---|
116 | * |
---|
117 | * Supports a facetquery instance or a config array, in that case a new |
---|
118 | * facetquery instance wil be created based on the options. |
---|
119 | * |
---|
120 | * @param Solarium_Query_Select_Component_Facet_Query|array $facetQuery |
---|
121 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
122 | */ |
---|
123 | public function addQuery($facetQuery) |
---|
124 | { |
---|
125 | if (is_array($facetQuery)) { |
---|
126 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query($facetQuery); |
---|
127 | } |
---|
128 | |
---|
129 | $key = $facetQuery->getKey(); |
---|
130 | |
---|
131 | if (0 === strlen($key)) { |
---|
132 | throw new Solarium_Exception('A facetquery must have a key value'); |
---|
133 | } |
---|
134 | |
---|
135 | if (array_key_exists($key, $this->_facetQueries)) { |
---|
136 | throw new Solarium_Exception('A query must have a unique key value within a multiquery facet'); |
---|
137 | } |
---|
138 | |
---|
139 | // forward shared excludes |
---|
140 | $facetQuery->addExcludes($this->getExcludes()); |
---|
141 | |
---|
142 | $this->_facetQueries[$key] = $facetQuery; |
---|
143 | return $this; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Add multiple facetqueries |
---|
148 | * |
---|
149 | * @param array $facetQueries Instances or config array |
---|
150 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
151 | */ |
---|
152 | public function addQueries(array $facetQueries) |
---|
153 | { |
---|
154 | foreach ($facetQueries AS $key => $facetQuery) { |
---|
155 | |
---|
156 | // in case of a config array: add key to config |
---|
157 | if (is_array($facetQuery) && !isset($facetQuery['key'])) { |
---|
158 | $facetQuery['key'] = $key; |
---|
159 | } |
---|
160 | |
---|
161 | $this->addQuery($facetQuery); |
---|
162 | } |
---|
163 | |
---|
164 | return $this; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Get a facetquery |
---|
169 | * |
---|
170 | * @param string $key |
---|
171 | * @return string |
---|
172 | */ |
---|
173 | public function getQuery($key) |
---|
174 | { |
---|
175 | if (isset($this->_facetQueries[$key])) { |
---|
176 | return $this->_facetQueries[$key]; |
---|
177 | } else { |
---|
178 | return null; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Get all facetqueries |
---|
184 | * |
---|
185 | * @return array |
---|
186 | */ |
---|
187 | public function getQueries() |
---|
188 | { |
---|
189 | return $this->_facetQueries; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Remove a single facetquery |
---|
194 | * |
---|
195 | * You can remove a facetquery by passing it's key or the facetquery instance |
---|
196 | * |
---|
197 | * @param string|Solarium_Query_Select_Component_Facet_Query $query |
---|
198 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
199 | */ |
---|
200 | public function removeQuery($query) |
---|
201 | { |
---|
202 | if (is_object($query)) { |
---|
203 | $query = $query->getKey(); |
---|
204 | } |
---|
205 | |
---|
206 | if (isset($this->_facetQueries[$query])) { |
---|
207 | unset($this->_facetQueries[$query]); |
---|
208 | } |
---|
209 | |
---|
210 | return $this; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * Remove all facetqueries |
---|
215 | * |
---|
216 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
217 | */ |
---|
218 | public function clearQueries() |
---|
219 | { |
---|
220 | $this->_facetQueries = array(); |
---|
221 | return $this; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Set multiple facetqueries |
---|
226 | * |
---|
227 | * This overwrites any existing facetqueries |
---|
228 | * |
---|
229 | * @param array $facetQueries |
---|
230 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
231 | */ |
---|
232 | public function setQueries($facetQueries) |
---|
233 | { |
---|
234 | $this->clearQueries(); |
---|
235 | return $this->addQueries($facetQueries); |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * Add an exclude tag |
---|
240 | * |
---|
241 | * Excludes added to the MultiQuery facet a shared by all underlying |
---|
242 | * FacetQueries, so they must be forwarded to any existing instances. |
---|
243 | * |
---|
244 | * If you don't want to share an exclude use the addExclude method of a |
---|
245 | * specific FacetQuery instance instead. |
---|
246 | * |
---|
247 | * @param string $tag |
---|
248 | * @return Solarium_Query_Select_Component_Facet Provides fluent interface |
---|
249 | */ |
---|
250 | public function addExclude($tag) |
---|
251 | { |
---|
252 | foreach ($this->_facetQueries AS $facetQuery) { |
---|
253 | $facetQuery->addExclude($tag); |
---|
254 | } |
---|
255 | |
---|
256 | return parent::addExclude($tag); |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Remove a single exclude tag |
---|
261 | * |
---|
262 | * Excludes added to the MultiQuery facet a shared by all underlying |
---|
263 | * FacetQueries, so changes must be forwarded to any existing instances. |
---|
264 | * |
---|
265 | * If you don't want this use the removeExclude method of a |
---|
266 | * specific FacetQuery instance instead. |
---|
267 | * |
---|
268 | * @param string $exclude |
---|
269 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
270 | */ |
---|
271 | public function removeExclude($exclude) |
---|
272 | { |
---|
273 | foreach ($this->_facetQueries AS $facetQuery) { |
---|
274 | $facetQuery->removeExclude($exclude); |
---|
275 | } |
---|
276 | |
---|
277 | return parent::removeExclude($exclude); |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Remove all excludes |
---|
282 | * |
---|
283 | * Excludes added to the MultiQuery facet a shared by all underlying |
---|
284 | * FacetQueries, so changes must be forwarded to any existing instances. |
---|
285 | * |
---|
286 | * If you don't want this use the clearExcludes method of a |
---|
287 | * specific FacetQuery instance instead. |
---|
288 | * |
---|
289 | * @return Solarium_Query_Select_Component_Facet_MultiQuery Provides fluent interface |
---|
290 | */ |
---|
291 | public function clearExcludes() |
---|
292 | { |
---|
293 | foreach ($this->_facetQueries AS $facetQuery) { |
---|
294 | $facetQuery->clearExcludes(); |
---|
295 | } |
---|
296 | |
---|
297 | return parent::clearExcludes(); |
---|
298 | } |
---|
299 | |
---|
300 | } |
---|