Total Complexity | 63 |
Total Lines | 471 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like SearchContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class SearchContext |
||
45 | { |
||
46 | use Injectable; |
||
47 | |||
48 | /** |
||
49 | * DataObject subclass to which search parameters relate to. |
||
50 | * Also determines as which object each result is provided. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $modelClass; |
||
55 | |||
56 | /** |
||
57 | * FormFields mapping to {@link DataObject::$db} properties |
||
58 | * which are supposed to be searchable. |
||
59 | * |
||
60 | * @var FieldList |
||
61 | */ |
||
62 | protected $fields; |
||
63 | |||
64 | /** |
||
65 | * Array of {@link SearchFilter} subclasses. |
||
66 | * |
||
67 | * @var SearchFilter[] |
||
68 | */ |
||
69 | protected $filters; |
||
70 | |||
71 | /** |
||
72 | * Key/value pairs of search fields to search terms |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $searchParams = []; |
||
77 | |||
78 | /** |
||
79 | * The logical connective used to join WHERE clauses. Must be "AND". |
||
80 | * @deprecated 5.0 |
||
81 | * @var string |
||
82 | */ |
||
83 | public $connective = 'AND'; |
||
84 | |||
85 | /** |
||
86 | * A key value pair of values that should be searched for. |
||
87 | * The keys should match the field names specified in {@link self::$fields}. |
||
88 | * Usually these values come from a submitted searchform |
||
89 | * in the form of a $_REQUEST object. |
||
90 | * CAUTION: All values should be treated as insecure client input. |
||
91 | * |
||
92 | * @param string $modelClass The base {@link DataObject} class that search properties related to. |
||
93 | * Also used to generate a set of result objects based on this class. |
||
94 | * @param FieldList $fields Optional. FormFields mapping to {@link DataObject::$db} properties |
||
95 | * which are to be searched. Derived from modelclass using |
||
96 | * {@link DataObject::scaffoldSearchFields()} if left blank. |
||
97 | * @param array $filters Optional. Derived from modelclass if left blank |
||
98 | */ |
||
99 | public function __construct($modelClass, $fields = null, $filters = null) |
||
100 | { |
||
101 | $this->modelClass = $modelClass; |
||
102 | $this->fields = ($fields) ? $fields : new FieldList(); |
||
103 | $this->filters = ($filters) ? $filters : []; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns scaffolded search fields for UI. |
||
108 | * |
||
109 | * @return FieldList |
||
110 | */ |
||
111 | public function getSearchFields() |
||
112 | { |
||
113 | return ($this->fields) ? $this->fields : singleton($this->modelClass)->scaffoldSearchFields(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @todo move to SQLSelect |
||
118 | * @todo fix hack |
||
119 | */ |
||
120 | protected function applyBaseTableFields() |
||
121 | { |
||
122 | $classes = ClassInfo::dataClassesFor($this->modelClass); |
||
123 | $baseTable = DataObject::getSchema()->baseDataTable($this->modelClass); |
||
124 | $fields = ["\"{$baseTable}\".*"]; |
||
125 | if ($this->modelClass != $classes[0]) { |
||
126 | $fields[] = '"' . $classes[0] . '".*'; |
||
127 | } |
||
128 | //$fields = array_keys($model->db()); |
||
129 | $fields[] = '"' . $classes[0] . '".\"ClassName\" AS "RecordClassName"'; |
||
130 | return $fields; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns a SQL object representing the search context for the given |
||
135 | * list of query parameters. |
||
136 | * |
||
137 | * @param array $searchParams Map of search criteria, mostly taken from $_REQUEST. |
||
138 | * If a filter is applied to a relationship in dot notation, |
||
139 | * the parameter name should have the dots replaced with double underscores, |
||
140 | * for example "Comments__Name" instead of the filter name "Comments.Name". |
||
141 | * @param array|bool|string $sort Database column to sort on. |
||
142 | * Falls back to {@link DataObject::$default_sort} if not provided. |
||
143 | * @param array|bool|string $limit |
||
144 | * @param DataList $existingQuery |
||
145 | * @return DataList |
||
146 | * @throws Exception |
||
147 | */ |
||
148 | public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) |
||
149 | { |
||
150 | if ($this->connective != "AND") { |
||
|
|||
151 | throw new Exception("SearchContext connective '$this->connective' not supported after ORM-rewrite."); |
||
152 | } |
||
153 | $this->setSearchParams($searchParams); |
||
154 | $query = $this->prepareQuery($sort, $limit, $existingQuery); |
||
155 | return $this->search($query); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Perform a search on the passed DataList based on $this->searchParams. |
||
160 | */ |
||
161 | protected function search(DataList $query): DataList |
||
162 | { |
||
163 | /** @var DataObject $modelObj */ |
||
164 | $modelObj = Injector::inst()->create($this->modelClass); |
||
165 | $searchableFields = $modelObj->searchableFields(); |
||
166 | foreach ($this->searchParams as $searchField => $searchPhrase) { |
||
167 | $searchField = str_replace('__', '.', $searchField ?? ''); |
||
168 | if ($searchField !== '' && $searchField === $modelObj->primarySearchFieldName()) { |
||
169 | $query = $this->primaryFieldSearch($query, $searchableFields, $searchPhrase); |
||
170 | } else { |
||
171 | $query = $this->individualFieldSearch($query, $searchableFields, $searchField, $searchPhrase); |
||
172 | } |
||
173 | } |
||
174 | return $query; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Prepare the query to begin searching |
||
179 | * |
||
180 | * @param array|bool|string $sort Database column to sort on. |
||
181 | * @param array|bool|string $limit |
||
182 | */ |
||
183 | protected function prepareQuery($sort, $limit, ?DataList $existingQuery): DataList |
||
184 | { |
||
185 | $query = null; |
||
186 | if ($existingQuery) { |
||
187 | if (!($existingQuery instanceof DataList)) { |
||
188 | throw new InvalidArgumentException("existingQuery must be DataList"); |
||
189 | } |
||
190 | if ($existingQuery->dataClass() != $this->modelClass) { |
||
191 | throw new InvalidArgumentException("existingQuery's dataClass is " . $existingQuery->dataClass() |
||
192 | . ", $this->modelClass expected."); |
||
193 | } |
||
194 | $query = $existingQuery; |
||
195 | } else { |
||
196 | $query = DataList::create($this->modelClass); |
||
197 | } |
||
198 | |||
199 | if (is_array($limit)) { |
||
200 | $query = $query->limit( |
||
201 | isset($limit['limit']) ? $limit['limit'] : null, |
||
202 | isset($limit['start']) ? $limit['start'] : null |
||
203 | ); |
||
204 | } else { |
||
205 | $query = $query->limit($limit); |
||
206 | } |
||
207 | |||
208 | return $query->sort($sort); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Use the global primary search for searching across multiple fields |
||
213 | * |
||
214 | * @param string|array $searchPhrase |
||
215 | */ |
||
216 | protected function primaryFieldSearch(DataList $query, array $searchableFields, $searchPhrase): DataList |
||
263 | } |
||
264 | }); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get the search filter for the given fieldname when searched from the primary search field. |
||
269 | */ |
||
270 | public function getPrimarySearchFilter(string $modelClass, string $fieldName): ?SearchFilter |
||
271 | { |
||
272 | if ($filterClass = Config::inst()->get($modelClass, 'primary_search_field_filter')) { |
||
273 | return Injector::inst()->create($filterClass, $fieldName); |
||
274 | } |
||
275 | return $this->getFilter($fieldName); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Search against a single field |
||
280 | * |
||
281 | * @param string|array $searchPhrase |
||
282 | */ |
||
283 | protected function individualFieldSearch(DataList $query, array $searchableFields, string $searchField, $searchPhrase): DataList |
||
284 | { |
||
285 | if ($filter = $this->getFilter($searchField)) { |
||
286 | $filter->setModel($this->modelClass); |
||
287 | $filter->setValue($searchPhrase); |
||
288 | $searchableFieldSpec = $searchableFields[$searchField] ?? []; |
||
289 | return $query->alterDataQuery(function ($dataQuery) use ($filter, $searchableFieldSpec) { |
||
290 | $this->applyFilter($filter, $dataQuery, $searchableFieldSpec); |
||
291 | }); |
||
292 | } |
||
293 | return $query; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Apply a SearchFilter to a DataQuery for a given field's specifications |
||
298 | */ |
||
299 | public function applyFilter(SearchFilter $filter, DataQuery $dataQuery, array $searchableFieldSpec): void |
||
300 | { |
||
301 | if (!$filter->isEmpty()) { |
||
302 | if (isset($searchableFieldSpec['match_any'])) { |
||
303 | $searchFields = $searchableFieldSpec['match_any']; |
||
304 | $filterClass = get_class($filter); |
||
305 | $value = $filter->getValue(); |
||
306 | $modifiers = $filter->getModifiers(); |
||
307 | $subGroup = $dataQuery->disjunctiveGroup(); |
||
308 | foreach ($searchFields as $matchField) { |
||
309 | /** @var SearchFilter $filterClass */ |
||
310 | $filter = Injector::inst()->create($filterClass, $matchField, $value, $modifiers); |
||
311 | $filter->apply($subGroup); |
||
312 | } |
||
313 | } else { |
||
314 | $filter->apply($dataQuery); |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Returns a result set from the given search parameters. |
||
321 | * |
||
322 | * @todo rearrange start and limit params to reflect DataObject |
||
323 | * |
||
324 | * @param array $searchParams |
||
325 | * @param array|bool|string $sort |
||
326 | * @param array|bool|string $limit |
||
327 | * @return DataList |
||
328 | * @throws Exception |
||
329 | */ |
||
330 | public function getResults($searchParams, $sort = false, $limit = false) |
||
331 | { |
||
332 | $searchParams = array_filter((array)$searchParams, [$this, 'clearEmptySearchFields']); |
||
333 | |||
334 | // getQuery actually returns a DataList |
||
335 | return $this->getQuery($searchParams, $sort, $limit); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Callback map function to filter fields with empty values from |
||
340 | * being included in the search expression. |
||
341 | * |
||
342 | * @param mixed $value |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function clearEmptySearchFields($value) |
||
346 | { |
||
347 | return ($value != ''); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Accessor for the filter attached to a named field. |
||
352 | * |
||
353 | * @param string $name |
||
354 | * @return SearchFilter |
||
355 | */ |
||
356 | public function getFilter($name) |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Get the map of filters in the current search context. |
||
367 | * |
||
368 | * @return SearchFilter[] |
||
369 | */ |
||
370 | public function getFilters() |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Overwrite the current search context filter map. |
||
377 | * |
||
378 | * @param array $filters |
||
379 | */ |
||
380 | public function setFilters($filters) |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Adds a instance of {@link SearchFilter}. |
||
387 | * |
||
388 | * @param SearchFilter $filter |
||
389 | */ |
||
390 | public function addFilter($filter) |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Removes a filter by name. |
||
397 | * |
||
398 | * @param string $name |
||
399 | */ |
||
400 | public function removeFilterByName($name) |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Get the list of searchable fields in the current search context. |
||
407 | * |
||
408 | * @return FieldList |
||
409 | */ |
||
410 | public function getFields() |
||
411 | { |
||
412 | return $this->fields; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Apply a list of searchable fields to the current search context. |
||
417 | * |
||
418 | * @param FieldList $fields |
||
419 | */ |
||
420 | public function setFields($fields) |
||
421 | { |
||
422 | $this->fields = $fields; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Adds a new {@link FormField} instance. |
||
427 | * |
||
428 | * @param FormField $field |
||
429 | */ |
||
430 | public function addField($field) |
||
431 | { |
||
432 | $this->fields->push($field); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Removes an existing formfield instance by its name. |
||
437 | * |
||
438 | * @param string $fieldName |
||
439 | */ |
||
440 | public function removeFieldByName($fieldName) |
||
441 | { |
||
442 | $this->fields->removeByName($fieldName); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Set search param values |
||
447 | * |
||
448 | * @param array|HTTPRequest $searchParams |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function setSearchParams($searchParams) |
||
452 | { |
||
453 | // hack to work with $searchParams when it's an Object |
||
454 | if ($searchParams instanceof HTTPRequest) { |
||
455 | $this->searchParams = $searchParams->getVars(); |
||
456 | } else { |
||
457 | $this->searchParams = $searchParams; |
||
458 | } |
||
459 | return $this; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @return array |
||
464 | */ |
||
465 | public function getSearchParams() |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Gets a list of what fields were searched and the values provided |
||
472 | * for each field. Returns an ArrayList of ArrayData, suitable for |
||
473 | * rendering on a template. |
||
474 | * |
||
475 | * @return ArrayList |
||
476 | */ |
||
477 | public function getSummary() |
||
515 | } |
||
516 | } |
||
517 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.