|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Controller\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\Plugin\AbstractPlugin; |
|
6
|
|
|
use Zend\Mvc\Controller\PluginManager as ControllerManager; |
|
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ListQuery |
|
11
|
|
|
* @package Core\Controller\Plugin |
|
12
|
|
|
* @todo: document |
|
13
|
|
|
*/ |
|
14
|
|
|
class ListQuery extends AbstractPlugin |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ServiceLocatorInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $serviceManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $propertiesMap = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $pageParamName = 'page'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $itemsPerPage = 25; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $queryKeysLowercased = true; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $sortParamName = 'sort'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param ServiceLocatorInterface $serviceManager |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ServiceLocatorInterface $serviceManager) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->serviceManager = $serviceManager; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $options |
|
|
|
|
|
|
57
|
|
|
* @return \Core\Controller\Plugin\ListQuery|\Doctrine\MongoDB\Query\Query |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __invoke($options = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if (null === $options) { |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (is_array($options)) { |
|
66
|
|
|
if (isset($options['properties_map'])) { |
|
67
|
|
|
$this->setPropertiesMap($options['properties_map']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (isset($options['page_param'])) { |
|
71
|
|
|
$this->setPageParamName($options['page_param']); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (isset($options['items_per_page'])) { |
|
75
|
|
|
$this->setItemsPerPage($options['items_per_page']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (isset($options['query_keys_lowercased'])) { |
|
79
|
|
|
$this->setQueryKeysLowercased($options['query_key_lowercased']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (isset($options['sort_param'])) { |
|
83
|
|
|
$this->setSortParamName($options['sort_param']); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->getQuery(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return Array $propertiesMap |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPropertiesMap() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->propertiesMap; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Array $propertiesMap |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setPropertiesMap($propertiesMap) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->propertiesMap = $propertiesMap; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return String $pageParamName |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getPageParamName() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->pageParamName; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $pageParamName |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setPageParamName($pageParamName) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->pageParamName = $pageParamName; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return String $sortParam |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getSortParamName() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->sortParamName; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param String $sortParam |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setSortParamName($sortParam) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->sortParamName = $sortParam; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getQueryKeysLowercased() |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
return $this->queryKeysLowercased; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setQueryKeysLowercased($flag) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->queryKeysLowercased = (bool) $flag; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return number $itemsPerPage |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getItemsPerPage() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->itemsPerPage; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param number $itemsPerPage |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setItemsPerPage($itemsPerPage) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->itemsPerPage = $itemsPerPage; |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return \Doctrine\MongoDB\Query\Query |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getQuery() |
|
171
|
|
|
{ |
|
172
|
|
|
$dbQuery = $this->serviceManager->get('query'); |
|
173
|
|
|
$criteria = $dbQuery->criteria(); |
|
174
|
|
|
|
|
175
|
|
|
/** @var \Zend\Http\Request $request */ |
|
176
|
|
|
$request = $this->getController()->getRequest(); |
|
177
|
|
|
$query = $request->getQuery()->toArray(); |
|
178
|
|
|
|
|
179
|
|
|
foreach ($this->propertiesMap as $name => $criterion) { |
|
180
|
|
|
if (is_numeric($name)) { |
|
181
|
|
|
$name = $criterion; |
|
182
|
|
|
$criterion = "startsWith"; |
|
183
|
|
|
} |
|
184
|
|
|
$queryName = $this->queryKeysLowercased ? strtolower($name) : $name; |
|
185
|
|
|
|
|
186
|
|
|
if (isset($query[$queryName])) { |
|
187
|
|
|
$criteria->$criterion($name, $query[$queryName]); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
$dbQuery->condition($criteria); |
|
191
|
|
|
$pageParamName = $this->queryKeysLowercased ? strtolower($this->getPageParamName()) : $this->getPageParamName(); |
|
192
|
|
|
$page = $request->getQuery($pageParamName, 1); |
|
193
|
|
|
$itemsPerPage = $this->getItemsPerPage(); |
|
194
|
|
|
$dbQuery->page($page, $itemsPerPage); |
|
195
|
|
|
|
|
196
|
|
|
$sortParamName = $this->queryKeysLowercased ? strtolower($this->getSortParamName()) : $this->getSortParamName(); |
|
197
|
|
|
if (isset($query[$sortParamName])) { |
|
198
|
|
|
$sort = $query[$sortParamName]; |
|
199
|
|
|
} else { |
|
200
|
|
|
reset($this->propertiesMap); |
|
201
|
|
|
list ($key, $val) = each($this->propertiesMap); |
|
202
|
|
|
$sort = is_numeric($key) ? $val : $key; |
|
203
|
|
|
} |
|
204
|
|
|
foreach (explode(',', $sort) as $s) { |
|
205
|
|
|
if ("-" == $s{0}) { |
|
206
|
|
|
$dbQuery->sort(substr($s, 1), false); |
|
207
|
|
|
} else { |
|
208
|
|
|
$dbQuery->sort($s); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
return $dbQuery; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param ControllerManager $controllerManager |
|
216
|
|
|
* @return ListQuery |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function factory(ControllerManager $controllerManager) |
|
219
|
|
|
{ |
|
220
|
|
|
return new static($controllerManager->getServiceLocator()); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.