1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Client\CmsPageSearch\Plugin\Elasticsearch\QueryExpander; |
||
9 | |||
10 | use Elastica\Query; |
||
11 | use Spryker\Client\Kernel\AbstractPlugin; |
||
12 | use Spryker\Client\Search\Dependency\Plugin\QueryExpanderPluginInterface; |
||
13 | use Spryker\Client\Search\Dependency\Plugin\QueryInterface; |
||
14 | |||
15 | /** |
||
16 | * @method \Spryker\Client\CmsPageSearch\CmsPageSearchFactory getFactory() |
||
17 | */ |
||
18 | class SortedCmsPageQueryExpanderPlugin extends AbstractPlugin implements QueryExpanderPluginInterface |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritDoc} |
||
22 | * - Adds possibility to sort cms pages search result. Options are provided by cms page sort config builder. |
||
23 | * |
||
24 | * @api |
||
25 | * |
||
26 | * @param \Spryker\Client\Search\Dependency\Plugin\QueryInterface $searchQuery |
||
27 | * @param array<string, mixed> $requestParameters |
||
28 | * |
||
29 | * @return \Spryker\Client\Search\Dependency\Plugin\QueryInterface |
||
30 | */ |
||
31 | public function expandQuery(QueryInterface $searchQuery, array $requestParameters = []): QueryInterface |
||
32 | { |
||
33 | $this->addSortingToQuery( |
||
34 | $searchQuery->getSearchQuery(), |
||
35 | $requestParameters, |
||
36 | ); |
||
37 | |||
38 | return $searchQuery; |
||
0 ignored issues
–
show
|
|||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param \Elastica\Query $query |
||
43 | * @param array<string, mixed> $requestParameters |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | protected function addSortingToQuery(Query $query, array $requestParameters): void |
||
48 | { |
||
49 | $sortConfig = $this->getFactory()->createSortConfigBuilder(); |
||
50 | $sortParamName = $sortConfig->getActiveParamName($requestParameters); |
||
51 | $sortConfigTransfer = $sortConfig->getSortConfigTransfer($sortParamName); |
||
52 | |||
53 | if ($sortConfigTransfer === null) { |
||
54 | return; |
||
55 | } |
||
56 | |||
57 | $nestedSortField = $sortConfigTransfer->getFieldName() . '.' . $sortConfigTransfer->getName(); |
||
58 | $query->addSort( |
||
59 | [ |
||
60 | $nestedSortField => [ |
||
61 | 'order' => $sortConfig->getSortDirection($sortParamName), |
||
62 | ], |
||
63 | ], |
||
64 | ); |
||
65 | } |
||
66 | } |
||
67 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: