1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action; |
2
|
|
|
|
3
|
|
|
use Doctrine\Common\Collections\Criteria; |
4
|
|
|
use Doctrine\ORM\QueryBuilder; |
5
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
6
|
|
|
use League\Fractal\Pagination\DoctrinePaginatorAdapter; |
7
|
|
|
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain; |
8
|
|
|
use Pz\Doctrine\Rest\QueryParser\ArrayFilterParser; |
9
|
|
|
use Pz\Doctrine\Rest\QueryParser\FilterParserAbstract; |
10
|
|
|
use Pz\Doctrine\Rest\QueryParser\SearchFilterParser; |
11
|
|
|
use Pz\Doctrine\Rest\Resource\Collection; |
12
|
|
|
use Pz\Doctrine\Rest\RestAction; |
13
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
14
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
15
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Action for providing collection (list or array) of data with API. |
19
|
|
|
*/ |
20
|
|
|
class CollectionAction extends RestAction |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Field that can be filtered if filter is string. |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $filterProperty; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Either to apply a loose or strict search. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $filterPropertyStrict = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get list of filterable entity fields. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
1 |
|
protected $filterable = []; |
42
|
|
|
|
43
|
1 |
|
/** |
44
|
1 |
|
* @param string|null $property |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setFilterProperty(?string $property): self |
49
|
|
|
{ |
50
|
|
|
$this->filterProperty = $property; |
51
|
|
|
return $this; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
/** |
55
|
1 |
|
* @param bool $filterPropertyStrict |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function setFilterPropertyStrict(bool $filterPropertyStrict): self |
59
|
|
|
{ |
60
|
|
|
$this->filterPropertyStrict = $filterPropertyStrict; |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
12 |
|
|
64
|
|
|
/** |
65
|
12 |
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function getFilterPropertyStrict() |
68
|
|
|
{ |
69
|
|
|
return $this->filterPropertyStrict; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
12 |
|
* @param array $filterable |
74
|
|
|
* |
75
|
12 |
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setFilterable(array $filterable): self |
78
|
|
|
{ |
79
|
|
|
$this->filterable = $filterable; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
12 |
|
/** |
84
|
|
|
* Param that can be filtered if query is string. |
85
|
12 |
|
* |
86
|
|
|
* @return null|string |
87
|
12 |
|
*/ |
88
|
12 |
|
public function getStringFilterField() |
89
|
12 |
|
{ |
90
|
|
|
return $this->filterProperty; |
91
|
12 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get list of filterable entity properties. |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getArrayFilterFields() |
99
|
12 |
|
{ |
100
|
|
|
return $this->filterable; |
101
|
12 |
|
} |
102
|
12 |
|
|
103
|
|
|
/** |
104
|
12 |
|
* @param RestRequestContract $request |
105
|
2 |
|
* @return RestResponse |
106
|
2 |
|
* @throws \Doctrine\ORM\Query\QueryException |
107
|
2 |
|
*/ |
108
|
2 |
|
protected function handle($request) |
109
|
|
|
{ |
110
|
2 |
|
$this->authorize($request, $this->repository()->getClassName()); |
111
|
|
|
|
112
|
2 |
|
$qb = $this->repository()->sourceQueryBuilder($request); |
113
|
2 |
|
$this->applyPagination($request, $qb); |
114
|
|
|
$this->applyFilter($request, $qb); |
115
|
|
|
|
116
|
2 |
|
return RestResponseFactory::resource($request, $this->prepareCollection($request, $qb)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param RestRequestContract $request |
121
|
12 |
|
* @param QueryBuilder $qb |
122
|
|
|
* @return Collection |
123
|
|
|
*/ |
124
|
|
|
protected function prepareCollection($request, QueryBuilder $qb) |
125
|
|
|
{ |
126
|
|
|
$paginator = new Paginator($qb, false); |
127
|
|
|
$collection = new Collection($paginator, $this->transformer(), $this->repository()->getResourceKey()); |
128
|
|
|
|
129
|
|
|
if ($qb->getMaxResults()) { |
|
|
|
|
130
|
|
|
$collection->setPaginator( |
131
|
12 |
|
new DoctrinePaginatorAdapter( |
132
|
|
|
$paginator, |
133
|
12 |
|
function(int $page) use ($request) { |
134
|
12 |
|
// return !$resourceKey ? null : "{$request->getBaseUrl()}/$resourceKey?".http_build_query([ |
135
|
12 |
|
return $request->getBasePath().'?'.http_build_query([ |
|
|
|
|
136
|
12 |
|
'page' => [ |
137
|
12 |
|
'number' => $page, |
138
|
|
|
'size' => $request->getLimit() |
139
|
|
|
] |
140
|
|
|
]); |
141
|
12 |
|
} |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $collection; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param RestRequestContract $request |
151
|
12 |
|
* @param QueryBuilder $qb |
152
|
|
|
* |
153
|
12 |
|
* @throws \Doctrine\ORM\Query\QueryException |
154
|
12 |
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
protected function applyPagination(RestRequestContract $request, QueryBuilder $qb): self |
157
|
12 |
|
{ |
158
|
|
|
$qb->addCriteria( |
159
|
|
|
new Criteria(null, |
160
|
|
|
$request->getOrderBy(), |
161
|
|
|
$request->getStart(), |
162
|
|
|
$request->getLimit() |
163
|
|
|
) |
164
|
|
|
); |
165
|
12 |
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
12 |
|
|
169
|
12 |
|
/** |
170
|
|
|
* @param RestRequestContract $request |
171
|
|
|
* @param QueryBuilder $qb |
172
|
|
|
* |
173
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
protected function applyFilter(RestRequestContract $request, QueryBuilder $qb): self |
177
|
|
|
{ |
178
|
|
|
$qb->addCriteria( |
179
|
|
|
CriteriaChain::create($this->filterParsers($request))->process() |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param RestRequestContract $request |
187
|
|
|
* |
188
|
|
|
* @return array|FilterParserAbstract[] |
189
|
|
|
*/ |
190
|
|
|
protected function filterParsers(RestRequestContract $request) |
191
|
|
|
{ |
192
|
|
|
return [ |
193
|
|
|
new SearchFilterParser( |
194
|
|
|
$request, |
195
|
|
|
$this->getStringFilterField(), |
196
|
|
|
SearchFilterParser::SEARCH_KEY, |
197
|
|
|
$this->getFilterPropertyStrict() |
198
|
|
|
), |
199
|
|
|
new ArrayFilterParser($request, $this->getArrayFilterFields()), |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: