|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
4
|
|
|
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain; |
|
5
|
|
|
use Pz\Doctrine\Rest\QueryParser\FilterableQueryParser; |
|
6
|
|
|
use Pz\Doctrine\Rest\QueryParser\PropertyQueryParser; |
|
7
|
|
|
use Pz\Doctrine\Rest\RestActionAbstract; |
|
8
|
|
|
use Pz\Doctrine\Rest\RestRequest; |
|
9
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Action for providing collection (list or array) of data with API. |
|
13
|
|
|
*/ |
|
14
|
|
|
class CollectionAction extends RestActionAbstract |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Field that can be filtered if filter is string. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $filterProperty; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get list of filterable entity fields. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $filterable = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $property |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function setFilterProperty($property) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->filterProperty = $property; |
|
38
|
1 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $filterable |
|
43
|
|
|
* |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function setFilterable(array $filterable) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->filterable = $filterable; |
|
49
|
1 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RestRequest $request |
|
54
|
|
|
* |
|
55
|
|
|
* @return RestResponse |
|
56
|
|
|
*/ |
|
57
|
6 |
|
protected function handle(RestRequest $request) |
|
58
|
|
|
{ |
|
59
|
6 |
|
$request->authorize($this->repository()->getClassName()); |
|
60
|
6 |
|
$chain = CriteriaChain::create($this->criteriaBuilders($request)); |
|
61
|
|
|
|
|
62
|
6 |
|
$criteria = new Criteria(null, |
|
63
|
6 |
|
$request->getOrderBy(), |
|
64
|
6 |
|
$request->getStart(), |
|
65
|
6 |
|
$request->getLimit() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
6 |
|
$qb = $this->repository() |
|
69
|
6 |
|
->createQueryBuilder($this->repository()->alias()) |
|
70
|
6 |
|
->addCriteria($chain->process($criteria)); |
|
71
|
|
|
|
|
72
|
6 |
|
return $this->response()->collection($request, $qb); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param RestRequest $request |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
6 |
|
protected function criteriaBuilders(RestRequest $request) |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
6 |
|
new PropertyQueryParser($request, $this->getQueryProperty()), |
|
84
|
6 |
|
new FilterableQueryParser($request, $this->getFilterable()), |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Param that can be filtered if query is string. |
|
90
|
|
|
* |
|
91
|
|
|
* @return null|string |
|
92
|
|
|
*/ |
|
93
|
6 |
|
protected function getQueryProperty() |
|
94
|
|
|
{ |
|
95
|
6 |
|
return $this->filterProperty; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get list of filterable entity properties. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
6 |
|
protected function getFilterable() |
|
104
|
|
|
{ |
|
105
|
6 |
|
return $this->filterable; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|