1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Criteria; |
6
|
|
|
use Doctrine\Common\Collections\Selectable; |
7
|
|
|
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface; |
8
|
|
|
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent; |
9
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
10
|
|
|
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
|
13
|
|
|
final class SearchController |
14
|
|
|
{ |
15
|
|
|
const ACTION = 'findAction'; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $fqcn; |
19
|
|
|
/** @var CriteriaConfiguratorInterface */ |
20
|
|
|
private $filters = []; |
21
|
|
|
/** @var Selectable */ |
22
|
|
|
private $repository; |
23
|
|
|
/** @var EventDispatcherInterface */ |
24
|
|
|
private $evm; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ReadController constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $fqcn |
30
|
|
|
* @param Selectable $repository |
31
|
|
|
* @param CriteriaConfiguratorInterface[] $filters |
32
|
|
|
* @param EventDispatcherInterface $evm |
33
|
|
|
*/ |
34
|
4 |
|
public function __construct($fqcn, Selectable $repository, array $filters, EventDispatcherInterface $evm) |
35
|
|
|
{ |
36
|
4 |
|
$this->fqcn = $fqcn; |
37
|
4 |
|
$this->filters = $filters; |
|
|
|
|
38
|
4 |
|
$this->repository = $repository; |
39
|
4 |
|
$this->evm = $evm; |
40
|
4 |
|
} |
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 CriteriaConfigurationException |
52
|
|
|
*/ |
53
|
4 |
|
public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0) |
54
|
|
|
{ |
55
|
4 |
|
$queryCriteria = new Criteria(null, $order, $offset, $limit); |
56
|
|
|
|
57
|
4 |
|
$unknown = array_diff_key($criteria, $this->filters); |
58
|
|
|
|
59
|
4 |
|
if (count($unknown) > 0) { |
60
|
|
|
throw CriteriaConfigurationException::unknown(array_keys($unknown)); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
foreach ($criteria as $filter => $item) { |
64
|
2 |
|
$this->filters[$filter]->configure($this->fqcn, $queryCriteria, $item); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
$entities = $this->repository->matching($queryCriteria); |
68
|
|
|
|
69
|
4 |
|
$this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent($entities)); |
|
|
|
|
70
|
|
|
|
71
|
4 |
|
return $entities; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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..