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 Client |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Add select component FacetSet to the request |
---|
41 | * |
---|
42 | * @package Solarium |
---|
43 | * @subpackage Client |
---|
44 | */ |
---|
45 | class Solarium_Client_RequestBuilder_Select_Component_FacetSet extends Solarium_Client_RequestBuilder |
---|
46 | { |
---|
47 | |
---|
48 | /** |
---|
49 | * Add request settings for FacetSet |
---|
50 | * |
---|
51 | * @param Solarium_Query_Select_Component_FacetSet $component |
---|
52 | * @param Solarium_Client_Request $request |
---|
53 | * @return Solarium_Client_Request |
---|
54 | */ |
---|
55 | public function buildComponent($component, $request) |
---|
56 | { |
---|
57 | $facets = $component->getFacets(); |
---|
58 | if (count($facets) !== 0) { |
---|
59 | |
---|
60 | // enable faceting |
---|
61 | $request->addParam('facet', 'true'); |
---|
62 | |
---|
63 | // global facet params |
---|
64 | $request->addParam('facet.sort', $component->getSort()); |
---|
65 | $request->addParam('facet.prefix', $component->getPrefix()); |
---|
66 | $request->addParam('facet.missing', $component->getMissing()); |
---|
67 | $request->addParam('facet.mincount', $component->getMinCount()); |
---|
68 | $request->addParam('facet.limit', $component->getLimit()); |
---|
69 | |
---|
70 | foreach ($facets AS $facet) { |
---|
71 | switch ($facet->getType()) |
---|
72 | { |
---|
73 | case Solarium_Query_Select_Component_FacetSet::FACET_FIELD: |
---|
74 | $this->addFacetField($request, $facet); |
---|
75 | break; |
---|
76 | case Solarium_Query_Select_Component_FacetSet::FACET_QUERY: |
---|
77 | $this->addFacetQuery($request, $facet); |
---|
78 | break; |
---|
79 | case Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY: |
---|
80 | $this->addFacetMultiQuery($request, $facet); |
---|
81 | break; |
---|
82 | case Solarium_Query_Select_Component_FacetSet::FACET_RANGE: |
---|
83 | $this->addFacetRange($request, $facet); |
---|
84 | break; |
---|
85 | default: |
---|
86 | throw new Solarium_Exception('Unknown facet type'); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | return $request; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Add params for a field facet to request |
---|
96 | * |
---|
97 | * @param Solarium_Client_Request $request |
---|
98 | * @param Solarium_Query_Select_Component_Facet_Field $facet |
---|
99 | * @return void |
---|
100 | */ |
---|
101 | public function addFacetField($request, $facet) |
---|
102 | { |
---|
103 | $field = $facet->getField(); |
---|
104 | |
---|
105 | $request->addParam( |
---|
106 | 'facet.field', |
---|
107 | $this->renderLocalParams( |
---|
108 | $field, |
---|
109 | array('key' => $facet->getKey(), 'ex' => $facet->getExcludes()) |
---|
110 | ) |
---|
111 | ); |
---|
112 | |
---|
113 | $request->addParam("f.$field.facet.limit", $facet->getLimit()); |
---|
114 | $request->addParam("f.$field.facet.sort", $facet->getSort()); |
---|
115 | $request->addParam("f.$field.facet.prefix", $facet->getPrefix()); |
---|
116 | $request->addParam("f.$field.facet.offset", $facet->getOffset()); |
---|
117 | $request->addParam("f.$field.facet.mincount", $facet->getMinCount()); |
---|
118 | $request->addParam("f.$field.facet.missing", $facet->getMissing()); |
---|
119 | $request->addParam("f.$field.facet.method", $facet->getMethod()); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Add params for a facet query to request |
---|
124 | * |
---|
125 | * @param Solarium_Client_Request $request |
---|
126 | * @param Solarium_Query_Select_Component_Facet_Query $facet |
---|
127 | * @return void |
---|
128 | */ |
---|
129 | public function addFacetQuery($request, $facet) |
---|
130 | { |
---|
131 | $request->addParam( |
---|
132 | 'facet.query', |
---|
133 | $this->renderLocalParams( |
---|
134 | $facet->getQuery(), |
---|
135 | array('key' => $facet->getKey(), 'ex' => $facet->getExcludes()) |
---|
136 | ) |
---|
137 | ); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Add params for a multiquery facet to request |
---|
142 | * |
---|
143 | * @param Solarium_Client_Request $request |
---|
144 | * @param Solarium_Query_Select_Component_Facet_MultiQuery $facet |
---|
145 | * @return void |
---|
146 | */ |
---|
147 | public function addFacetMultiQuery($request, $facet) |
---|
148 | { |
---|
149 | foreach ($facet->getQueries() AS $facetQuery) { |
---|
150 | $this->addFacetQuery($request, $facetQuery); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Add params for a range facet to request |
---|
156 | * |
---|
157 | * @param Solarium_Client_Request $request |
---|
158 | * @param Solarium_Query_Select_Component_Facet_Range $facet |
---|
159 | * @return void |
---|
160 | */ |
---|
161 | public function addFacetRange($request, $facet) |
---|
162 | { |
---|
163 | $field = $facet->getField(); |
---|
164 | |
---|
165 | $request->addParam( |
---|
166 | 'facet.range', |
---|
167 | $this->renderLocalParams( |
---|
168 | $field, |
---|
169 | array('key' => $facet->getKey(), 'ex' => $facet->getExcludes()) |
---|
170 | ) |
---|
171 | ); |
---|
172 | |
---|
173 | $request->addParam("f.$field.facet.range.start", $facet->getStart()); |
---|
174 | $request->addParam("f.$field.facet.range.end", $facet->getEnd()); |
---|
175 | $request->addParam("f.$field.facet.range.gap", $facet->getGap()); |
---|
176 | $request->addParam("f.$field.facet.range.hardend", $facet->getHardend()); |
---|
177 | |
---|
178 | if (null !== $facet->getOther()) { |
---|
179 | $other = explode(',', $facet->getOther()); |
---|
180 | foreach ($other AS $otherValue) { |
---|
181 | $request->addParam("f.$field.facet.range.other", trim($otherValue)); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | if (null !== $facet->getOther()) { |
---|
186 | $include = explode(',', $facet->getInclude()); |
---|
187 | foreach ($include AS $includeValue) { |
---|
188 | $request->addParam("f.$field.facet.range.include", trim($includeValue)); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|