1 | <?php |
||
21 | class PaginateElasticaQuerySubscriber implements EventSubscriberInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var RequestStack |
||
25 | */ |
||
26 | private $requestStack; |
||
27 | |||
28 | /** |
||
29 | * @param RequestStack $requestStack |
||
30 | */ |
||
31 | 10 | public function __construct(RequestStack $requestStack) |
|
35 | |||
36 | /** |
||
37 | * @param ItemsEvent $event |
||
38 | */ |
||
39 | 10 | public function items(ItemsEvent $event) |
|
58 | |||
59 | /** |
||
60 | * @return array |
||
61 | */ |
||
62 | public static function getSubscribedEvents() |
||
68 | |||
69 | /** |
||
70 | * Adds knp paging sort to query. |
||
71 | * |
||
72 | * @param ItemsEvent $event |
||
73 | */ |
||
74 | 10 | protected function setSorting(ItemsEvent $event) |
|
89 | |||
90 | 8 | protected function getSort($sortField, array $options = []) |
|
91 | { |
||
92 | $sort = [ |
||
93 | 8 | 'order' => $this->getSortDirection($sortField, $options), |
|
94 | ]; |
||
95 | |||
96 | 7 | if (isset($options['sortNestedPath'])) { |
|
97 | 4 | $path = is_callable($options['sortNestedPath']) ? |
|
98 | 4 | $options['sortNestedPath']($sortField) : $options['sortNestedPath']; |
|
99 | |||
100 | 4 | if (!empty($path)) { |
|
101 | 4 | $sort['nested_path'] = $path; |
|
102 | } |
||
103 | } |
||
104 | |||
105 | 7 | if (isset($options['sortNestedFilter'])) { |
|
106 | 2 | $filter = is_callable($options['sortNestedFilter']) ? |
|
107 | 2 | $options['sortNestedFilter']($sortField) : $options['sortNestedFilter']; |
|
108 | |||
109 | 2 | if (!empty($filter)) { |
|
110 | 2 | $sort['nested_filter'] = $filter; |
|
111 | } |
||
112 | } |
||
113 | |||
114 | 7 | return $sort; |
|
115 | } |
||
116 | |||
117 | 8 | protected function getSortDirection($sortField, array $options = []) |
|
137 | |||
138 | /** |
||
139 | * @return Request|null |
||
140 | */ |
||
141 | 10 | private function getRequest() |
|
145 | |||
146 | /** |
||
147 | * @param string|null $key |
||
148 | * @return mixed|null |
||
149 | */ |
||
150 | 10 | private function getFromRequest(?string $key) |
|
158 | } |
||
159 |