1 | <?php |
||
36 | class Faceting implements ParameterBuilder |
||
37 | { |
||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $isEnabled = false; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $sorting = ''; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $minCount = 1; |
||
52 | |||
53 | /** |
||
54 | * @var |
||
55 | */ |
||
56 | protected $limit = 10; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $fields = []; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $additionalParameters = []; |
||
67 | |||
68 | /** |
||
69 | * Faceting constructor. |
||
70 | * |
||
71 | * private constructor should only be created with the from* methods |
||
72 | * |
||
73 | * @param bool $isEnabled |
||
74 | * @param string $sorting |
||
75 | * @param int $minCount |
||
76 | * @param int $limit |
||
77 | * @param array $fields |
||
78 | * @param array $additionalParameters |
||
79 | */ |
||
80 | 135 | private function __construct($isEnabled, $sorting = '', $minCount = 1, $limit = 10, $fields = [], $additionalParameters = []) |
|
89 | |||
90 | /** |
||
91 | * @return boolean |
||
92 | */ |
||
93 | public function getIsEnabled() |
||
97 | |||
98 | /** |
||
99 | * @param boolean $isEnabled |
||
100 | */ |
||
101 | 44 | public function setIsEnabled($isEnabled) |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getSorting() |
||
113 | |||
114 | /** |
||
115 | * @param string $sorting |
||
116 | */ |
||
117 | public function setSorting($sorting) |
||
121 | |||
122 | /** |
||
123 | * @return int |
||
124 | */ |
||
125 | public function getMinCount() |
||
129 | |||
130 | /** |
||
131 | * @param int $minCount |
||
132 | */ |
||
133 | public function setMinCount($minCount) |
||
137 | |||
138 | /** |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function getLimit() |
||
145 | |||
146 | /** |
||
147 | * @param mixed $limit |
||
148 | */ |
||
149 | public function setLimit($limit) |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getFields() |
||
161 | |||
162 | /** |
||
163 | * @param array $fields |
||
164 | */ |
||
165 | 29 | public function setFields(array $fields) |
|
169 | |||
170 | /** |
||
171 | * @param string $fieldName |
||
172 | */ |
||
173 | 2 | public function addField($fieldName) |
|
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getAdditionalParameters(): array |
||
185 | |||
186 | /** |
||
187 | * @param array $additionalParameters |
||
188 | */ |
||
189 | public function setAdditionalParameters(array $additionalParameters) |
||
193 | |||
194 | /** |
||
195 | * @param array $value |
||
196 | */ |
||
197 | 38 | public function addAdditionalParameter($key, $value) |
|
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | 93 | public function build() |
|
206 | { |
||
207 | 93 | if (!$this->isEnabled) { |
|
208 | 49 | return []; |
|
209 | } |
||
210 | |||
211 | 46 | $facetParameters = []; |
|
212 | |||
213 | 46 | $facetParameters['facet'] = 'true'; |
|
214 | 46 | $facetParameters['facet.mincount'] = $this->minCount; |
|
215 | 46 | $facetParameters['facet.limit'] = $this->limit; |
|
216 | 46 | $facetParameters['facet.field'] = $this->fields; |
|
217 | |||
218 | 46 | foreach ($this->additionalParameters as $additionalParameterKey => $additionalParameterValue) { |
|
219 | 38 | $facetParameters[$additionalParameterKey] = $additionalParameterValue; |
|
220 | } |
||
221 | |||
222 | 46 | if ($facetParameters['json.facet']) { |
|
223 | 37 | $facetParameters['json.facet'] = json_encode($facetParameters['json.facet']); |
|
224 | } |
||
225 | |||
226 | 46 | $facetParameters = $this->applySorting($facetParameters); |
|
227 | |||
228 | 46 | return $facetParameters; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Reads the facet sorting configuration and applies it to the queryParameters. |
||
233 | * |
||
234 | * @param array $facetParameters |
||
235 | * @return array |
||
236 | */ |
||
237 | 46 | protected function applySorting(array $facetParameters) |
|
238 | { |
||
239 | 46 | $sortingExpression = new SortingExpression(); |
|
240 | 46 | $globalSortingExpression = $sortingExpression->getForFacet($this->sorting); |
|
241 | |||
242 | 46 | if (!empty($globalSortingExpression)) { |
|
243 | 32 | $facetParameters['facet.sort'] = $globalSortingExpression; |
|
244 | } |
||
245 | |||
246 | 46 | return $facetParameters; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param TypoScriptConfiguration $solrConfiguration |
||
251 | * @return Faceting |
||
252 | */ |
||
253 | 135 | public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration) |
|
266 | |||
267 | } |
||
268 |