|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\Query\Expr; |
|
7
|
|
|
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface; |
|
8
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
|
9
|
|
|
use ScayTrase\Api\Cruds\Event\EntityCrudEvent; |
|
10
|
|
|
use ScayTrase\Api\Cruds\Exception\FilterException; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class ReadController |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var CriteriaConfiguratorInterface */ |
|
17
|
|
|
private $filters = []; |
|
18
|
|
|
/** @var EntityRepository */ |
|
19
|
|
|
private $repository; |
|
20
|
|
|
/** @var EventDispatcherInterface */ |
|
21
|
|
|
private $evm; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ReadController constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param EntityRepository $repository |
|
27
|
|
|
* @param CriteriaConfiguratorInterface[] $filters |
|
28
|
|
|
* @param EventDispatcherInterface $evm |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
EntityRepository $repository, |
|
32
|
|
|
array $filters, |
|
33
|
|
|
EventDispatcherInterface $evm = null |
|
34
|
|
|
) { |
|
35
|
|
|
$this->filters = $filters; |
|
|
|
|
|
|
36
|
|
|
$this->repository = $repository; |
|
37
|
|
|
$this->evm = $evm ?: new EventDispatcher(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Performs search of entities and returns them |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $criteria |
|
44
|
|
|
* @param array $order |
|
45
|
|
|
* @param int $limit |
|
46
|
|
|
* @param int $offset |
|
47
|
|
|
* |
|
48
|
|
|
* @return object[] |
|
49
|
|
|
* @throws FilterException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0) |
|
52
|
|
|
{ |
|
53
|
|
|
$builder = $this->repository->createQueryBuilder('e'); |
|
54
|
|
|
|
|
55
|
|
|
$unknown = array_diff_key($criteria, $this->filters); |
|
56
|
|
|
|
|
57
|
|
|
if (count($unknown) > 0) { |
|
58
|
|
|
throw FilterException::unknown(array_keys($unknown)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach ($criteria as $filter => $item) { |
|
62
|
|
|
$this->filters[$filter]->configure($builder, $item); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($order as $key => $value) { |
|
66
|
|
|
$builder->addOrderBy(new Expr\Literal($key), strtolower($value) === 'asc' ? 'ASC' : 'DESC'); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$builder->setMaxResults($limit); |
|
70
|
|
|
$builder->setFirstResult($offset); |
|
71
|
|
|
|
|
72
|
|
|
$entities = $builder->getQuery()->getResult(); |
|
73
|
|
|
|
|
74
|
|
|
$this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent($entities)); |
|
75
|
|
|
|
|
76
|
|
|
return $entities; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the entity by given identifiers |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $identifier |
|
83
|
|
|
* |
|
84
|
|
|
* @return null|object |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getAction($identifier) |
|
87
|
|
|
{ |
|
88
|
|
|
$entity = $this->repository->find($identifier); |
|
89
|
|
|
|
|
90
|
|
|
$this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent([$entity])); |
|
91
|
|
|
|
|
92
|
|
|
return $entity; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..