1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action; |
2
|
|
|
|
3
|
|
|
use Doctrine\Common\Collections\Criteria; |
4
|
|
|
use Doctrine\ORM\Query; |
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Pz\Doctrine\Rest\Action\Index\ResponseData; |
7
|
|
|
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain; |
8
|
|
|
use Pz\Doctrine\Rest\QueryParser\FilterableQueryParser; |
9
|
|
|
use Pz\Doctrine\Rest\QueryParser\PropertyQueryParser; |
10
|
|
|
use Pz\Doctrine\Rest\Request\IndexRequestInterface; |
11
|
|
|
use Pz\Doctrine\Rest\RestException; |
12
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
13
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
14
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
15
|
|
|
|
16
|
|
|
trait IndexAction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Entity alias. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $rootAlias; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Doctrine repository from where get data. |
27
|
|
|
* |
28
|
|
|
* @return RestRepository |
29
|
|
|
*/ |
30
|
|
|
abstract protected function repository(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return RestResponseFactory |
34
|
|
|
*/ |
35
|
|
|
abstract protected function response(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param IndexRequestInterface $request |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
1 |
|
public function index(IndexRequestInterface $request) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
|
46
|
1 |
|
$request->authorize($this->repository()->getClassName()); |
47
|
1 |
|
$chain = CriteriaChain::create($this->criteriaBuilders($request)); |
48
|
|
|
|
49
|
1 |
|
$criteria = new Criteria(null, |
50
|
1 |
|
$request->getOrderBy(), |
51
|
1 |
|
$request->getStart(), |
52
|
1 |
|
$request->getLimit() |
53
|
|
|
); |
54
|
|
|
|
55
|
1 |
|
$qb = $this->repository() |
56
|
1 |
|
->createQueryBuilder($this->alias()) |
57
|
1 |
|
->addCriteria($chain->process($criteria)); |
58
|
|
|
|
59
|
1 |
|
return $this->response()->index($request, $this->buildResponseData($qb)); |
|
|
|
|
60
|
|
|
|
61
|
|
|
} catch (RestException $e) { |
62
|
|
|
return $this->response()->error($e->getCode(), $e->getMessage(), $e->errors()); |
|
|
|
|
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
return $this->response()->error( |
|
|
|
|
65
|
|
|
RestResponse::HTTP_INTERNAL_SERVER_ERROR, |
66
|
|
|
$e->getMessage(), |
67
|
|
|
$e->getTrace() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param QueryBuilder $qb |
74
|
|
|
* |
75
|
|
|
* @return ResponseData |
76
|
|
|
*/ |
77
|
1 |
|
protected function buildResponseData(QueryBuilder $qb) |
78
|
|
|
{ |
79
|
1 |
|
return new ResponseData($qb); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
protected function alias() |
86
|
|
|
{ |
87
|
1 |
|
if ($this->rootAlias === null) { |
88
|
1 |
|
$reflectionClass = new \ReflectionClass($this->repository()->getClassName()); |
89
|
1 |
|
$this->rootAlias = strtolower($reflectionClass->getShortName()[0]); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->rootAlias; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param IndexRequestInterface $request |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
1 |
|
protected function criteriaBuilders(IndexRequestInterface $request) |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
1 |
|
new PropertyQueryParser($request, $this->getQueryProperty()), |
|
|
|
|
104
|
1 |
|
new FilterableQueryParser($request, $this->getFilterable()), |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Param that can be filtered if query is string. |
110
|
|
|
* |
111
|
|
|
* @return null|string |
112
|
|
|
*/ |
113
|
1 |
|
protected function getQueryProperty() |
114
|
|
|
{ |
115
|
1 |
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get list of filterable entity properties. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
1 |
|
protected function getFilterable() |
124
|
|
|
{ |
125
|
1 |
|
return []; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|