1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Handler; |
4
|
|
|
|
5
|
|
|
use App\Entity\Product; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
use FOS\RestBundle\View\View; |
9
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
10
|
|
|
use Pagerfanta\Pagerfanta; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ApiHandler |
17
|
|
|
* |
18
|
|
|
* @package App\Handler |
19
|
|
|
*/ |
20
|
|
|
class ApiHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
private const MAX_RESULTS = 100; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ViewHandlerInterface |
29
|
|
|
*/ |
30
|
|
|
public $viewhandler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EntityManagerInterface |
34
|
|
|
*/ |
35
|
|
|
public $em; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Request |
39
|
|
|
*/ |
40
|
|
|
public $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
public $repository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ApiHandler constructor. |
49
|
|
|
* |
50
|
|
|
* @param RequestStack $requestStack |
51
|
|
|
* @param EntityManagerInterface $em |
52
|
|
|
* @param ViewHandlerInterface $viewHandler |
53
|
|
|
*/ |
54
|
|
|
public function __construct(RequestStack $requestStack, EntityManagerInterface $em, ViewHandlerInterface $viewHandler) |
55
|
|
|
{ |
56
|
|
|
$this->viewhandler = $viewHandler; |
57
|
|
|
$this->em = $em; |
58
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $entity |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function init(string $entity) |
67
|
|
|
{ |
68
|
|
|
$this->qb = $this->em->getRepository($entity)->createQueryBuilder('c'); |
|
|
|
|
69
|
|
|
$this->repository = $this->em->getRepository($entity); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $groups |
76
|
|
|
* @param array $routeParams |
77
|
|
|
* |
78
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
79
|
|
|
*/ |
80
|
|
|
public function collect(array $groups, array $routeParams = array()) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$view = $this->createView( |
83
|
|
|
$this->transformIterator($this->getPaginatedResult()), |
84
|
|
|
$groups |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $this->viewhandler->createResponse($view, $this->request,'json'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $data |
92
|
|
|
* @param array $groups |
93
|
|
|
* |
94
|
|
|
* @return static |
95
|
|
|
*/ |
96
|
|
|
public function createView($data, array $groups) |
97
|
|
|
{ |
98
|
|
|
$view = View::create($data); |
99
|
|
|
|
100
|
|
|
if ($groups) { |
|
|
|
|
101
|
|
|
$context = $view->getContext(); |
102
|
|
|
$context->setGroups($groups); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $view; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Pagerfanta |
111
|
|
|
*/ |
112
|
|
|
private function getPaginatedResult(): Pagerfanta |
113
|
|
|
{ |
114
|
|
|
$adapter = new DoctrineORMAdapter($this->getQueryBuilder()); |
115
|
|
|
|
116
|
|
|
$pagerfanta = new Pagerfanta($adapter); |
117
|
|
|
|
118
|
|
|
$pagerfanta |
119
|
|
|
->setMaxPerPage($this->request->query->get('limit', self::MAX_RESULTS)) |
120
|
|
|
->setCurrentPage($this->request->query->get('page', 1)); |
121
|
|
|
|
122
|
|
|
if ($this->request->query->get('noLimit') !== null) { |
123
|
|
|
$pagerfanta->setMaxPerPage(999999); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $pagerfanta; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Create final return array with data needed + items per page etc. |
132
|
|
|
* |
133
|
|
|
* @param Pagerfanta $pagerfanta |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function transformIterator(Pagerfanta $pagerfanta): array |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$pagerfanta = $this->getPaginatedResult(); |
140
|
|
|
|
141
|
|
|
return [ |
142
|
|
|
'data' => iterator_to_array($pagerfanta->getCurrentPageResults()), |
143
|
|
|
'page' => $pagerfanta->getCurrentPage(), |
144
|
|
|
'pages' => $pagerfanta->getNbPages(), |
145
|
|
|
'max' => $pagerfanta->getNbResults(), |
146
|
|
|
'offes' => $pagerfanta->getMaxPerPage(), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return QueryBuilder |
152
|
|
|
*/ |
153
|
|
|
public function convertRequest(): QueryBuilder |
154
|
|
|
{ |
155
|
|
|
return $this->qb; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return QueryBuilder |
160
|
|
|
*/ |
161
|
|
|
public function getQueryBuilder(): QueryBuilder |
162
|
|
|
{ |
163
|
|
|
$qb = $this->convertRequest(); |
164
|
|
|
|
165
|
|
|
return $qb; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: