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 SearchController |
15
|
|
|
{ |
16
|
|
|
const ACTION = 'findAction'; |
17
|
|
|
|
18
|
|
|
/** @var CriteriaConfiguratorInterface */ |
19
|
|
|
private $filters = []; |
20
|
|
|
/** @var EntityRepository */ |
21
|
|
|
private $repository; |
22
|
|
|
/** @var EventDispatcherInterface */ |
23
|
|
|
private $evm; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ReadController constructor. |
27
|
|
|
* |
28
|
|
|
* @param EntityRepository $repository |
29
|
|
|
* @param CriteriaConfiguratorInterface[] $filters |
30
|
|
|
* @param EventDispatcherInterface $evm |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
EntityRepository $repository, |
34
|
|
|
array $filters, |
35
|
|
|
EventDispatcherInterface $evm = null |
36
|
|
|
) { |
37
|
|
|
$this->filters = $filters; |
|
|
|
|
38
|
|
|
$this->repository = $repository; |
39
|
|
|
$this->evm = $evm ?: new EventDispatcher(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Performs search of entities and returns them |
44
|
|
|
* |
45
|
|
|
* @param array $criteria |
46
|
|
|
* @param array $order |
47
|
|
|
* @param int $limit |
48
|
|
|
* @param int $offset |
49
|
|
|
* |
50
|
|
|
* @return object[] |
51
|
|
|
* @throws FilterException |
52
|
|
|
*/ |
53
|
|
|
public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0) |
54
|
|
|
{ |
55
|
|
|
$builder = $this->repository->createQueryBuilder('e'); |
56
|
|
|
|
57
|
|
|
$unknown = array_diff_key($criteria, $this->filters); |
58
|
|
|
|
59
|
|
|
if (count($unknown) > 0) { |
60
|
|
|
throw FilterException::unknown(array_keys($unknown)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($criteria as $filter => $item) { |
64
|
|
|
$this->filters[$filter]->configure($builder, $item); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($order as $key => $value) { |
68
|
|
|
$builder->addOrderBy(new Expr\Literal($key), strtolower($value) === 'asc' ? 'ASC' : 'DESC'); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$builder->setMaxResults($limit); |
72
|
|
|
$builder->setFirstResult($offset); |
73
|
|
|
|
74
|
|
|
$entities = $builder->getQuery()->getResult(); |
75
|
|
|
|
76
|
|
|
$this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent($entities)); |
77
|
|
|
|
78
|
|
|
return $entities; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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..