|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Criteria; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
6
|
|
|
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface; |
|
7
|
|
|
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException; |
|
8
|
|
|
use ScayTrase\Api\Cruds\Exception\MapperException; |
|
9
|
|
|
use ScayTrase\Api\Cruds\PropertyMapperInterface; |
|
10
|
|
|
use ScayTrase\Api\Cruds\ReferenceProviderInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class EntityCriteriaConfigurator implements CriteriaConfiguratorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const ALIAS_SEPARATOR = '.'; |
|
15
|
|
|
|
|
16
|
|
|
/** @var PropertyMapperInterface */ |
|
17
|
|
|
private $mapper; |
|
18
|
|
|
/** @var ReferenceProviderInterface */ |
|
19
|
|
|
private $provider; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* DoctrineCriteriaConfigurator constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param PropertyMapperInterface $mapper |
|
25
|
|
|
* @param ReferenceProviderInterface $provider |
|
26
|
|
|
*/ |
|
27
|
10 |
|
public function __construct(PropertyMapperInterface $mapper, ReferenceProviderInterface $provider) |
|
28
|
|
|
{ |
|
29
|
10 |
|
$this->mapper = $mapper; |
|
30
|
10 |
|
$this->provider = $provider; |
|
31
|
10 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** {@inheritdoc} */ |
|
34
|
10 |
|
public function configure($fqcn, Criteria $criteria, $arguments) |
|
35
|
|
|
{ |
|
36
|
10 |
|
if (null === $arguments) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
if (!is_array($arguments)) { |
|
41
|
|
|
throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
10 |
|
foreach ((array)$arguments as $apiProperty => $value) { |
|
45
|
|
|
try { |
|
46
|
8 |
|
$mappedProperty = $this->mapper->getEntityProperty($fqcn, $apiProperty); |
|
47
|
|
|
|
|
48
|
8 |
|
if (null === $mappedProperty) { |
|
49
|
|
|
throw CriteriaConfigurationException::invalidData($apiProperty); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
$value = $this->provider->getEntityReference($fqcn, $mappedProperty, $value); |
|
53
|
8 |
|
$this->filterDoctrineProperty($criteria, $mappedProperty, $value); |
|
54
|
8 |
|
unset($arguments[$apiProperty]); |
|
55
|
8 |
|
} catch (MapperException $e) { |
|
56
|
|
|
throw CriteriaConfigurationException::invalidProperty($apiProperty, $e); |
|
57
|
|
|
} |
|
58
|
10 |
|
} |
|
59
|
10 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Criteria $criteria |
|
63
|
|
|
* @param string $property |
|
64
|
|
|
* @param mixed $value |
|
65
|
|
|
* |
|
66
|
|
|
* @throws CriteriaConfigurationException |
|
67
|
|
|
*/ |
|
68
|
8 |
|
private function filterDoctrineProperty(Criteria $criteria, $property, $value) |
|
69
|
|
|
{ |
|
70
|
8 |
|
switch (true) { |
|
71
|
8 |
|
case is_array($value): |
|
72
|
2 |
|
$criteria->andWhere(Criteria::expr()->in($property, $value)); |
|
73
|
2 |
|
break; |
|
74
|
8 |
|
case null === $value: |
|
75
|
2 |
|
$criteria->andWhere(Criteria::expr()->isNull($property)); |
|
76
|
2 |
|
break; |
|
77
|
8 |
|
case !is_scalar($value) && !is_object($value): |
|
78
|
|
|
throw CriteriaConfigurationException::invalidPropertyType( |
|
79
|
|
|
$property, |
|
80
|
|
|
'scalar|array|null|identifier', |
|
81
|
|
|
gettype($value) |
|
82
|
|
|
); |
|
83
|
8 |
|
default: |
|
84
|
8 |
|
$criteria->andWhere(Criteria::expr()->eq($property, $value)); |
|
85
|
8 |
|
} |
|
86
|
8 |
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|