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\Exception\CriteriaConfigurationException; |
9
|
|
|
|
10
|
|
|
final class SearchController |
11
|
|
|
{ |
12
|
|
|
const ACTION = 'findAction'; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $fqcn; |
16
|
|
|
/** @var CriteriaConfiguratorInterface */ |
17
|
|
|
private $filters = []; |
18
|
|
|
/** @var Selectable */ |
19
|
|
|
private $repository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ReadController constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $fqcn |
25
|
|
|
* @param Selectable $repository |
26
|
|
|
* @param CriteriaConfiguratorInterface[] $filters |
27
|
|
|
*/ |
28
|
|
|
public function __construct($fqcn, Selectable $repository, array $filters) |
29
|
|
|
{ |
30
|
|
|
$this->fqcn = $fqcn; |
31
|
|
|
$this->filters = $filters; |
|
|
|
|
32
|
2 |
|
$this->repository = $repository; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Performs search of entities and returns them |
37
|
2 |
|
* |
38
|
2 |
|
* @param array $criteria |
39
|
2 |
|
* @param array $order |
40
|
2 |
|
* @param int $limit |
41
|
|
|
* @param int $offset |
42
|
|
|
* |
43
|
|
|
* @return object[] |
44
|
|
|
* @throws CriteriaConfigurationException |
45
|
|
|
*/ |
46
|
|
|
public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0) |
47
|
|
|
{ |
48
|
|
|
$queryCriteria = new Criteria(null, $order, $offset, $limit); |
49
|
|
|
|
50
|
|
|
$unknown = array_diff_key($criteria, $this->filters); |
51
|
|
|
|
52
|
|
|
if (count($unknown) > 0) { |
53
|
2 |
|
throw CriteriaConfigurationException::unknown(array_keys($unknown)); |
54
|
|
|
} |
55
|
2 |
|
|
56
|
|
|
foreach ($criteria as $filter => $item) { |
57
|
2 |
|
$this->filters[$filter]->configure($this->fqcn, $queryCriteria, $item); |
58
|
|
|
} |
59
|
2 |
|
|
60
|
|
|
return $this->repository->matching($queryCriteria); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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..