1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Processor\DatabaseProcessor; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\Query\Expr\Andx; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use phpDocumentor\Reflection\Types\Static_; |
15
|
|
|
use RDS\Hydrogen\Criteria\Criterion; |
16
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
17
|
|
|
use RDS\Hydrogen\Criteria\WhereGroup; |
18
|
|
|
use RDS\Hydrogen\Criteria\Where; |
19
|
|
|
use RDS\Hydrogen\Processor\DatabaseProcessor\Common\Expression; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class GroupBuilder |
23
|
|
|
*/ |
24
|
|
|
class GroupBuilder extends Builder |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string[]|Criterion[] |
28
|
|
|
*/ |
29
|
|
|
protected const ALLOWED_INNER_TYPES = [ |
30
|
|
|
Where::class => 'applyWhere', |
31
|
|
|
WhereGroup::class => 'applyGroup' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param QueryBuilder $builder |
36
|
|
|
* @param CriterionInterface|WhereGroup $group |
37
|
|
|
* @return iterable|null |
38
|
|
|
*/ |
39
|
2 |
|
public function apply($builder, CriterionInterface $group): ?iterable |
40
|
|
|
{ |
41
|
2 |
|
$expression = $builder->expr()->andX(); |
42
|
|
|
|
43
|
2 |
|
foreach ($this->getInnerSelections($group) as $criterion => $fn) { |
|
|
|
|
44
|
2 |
|
yield from $fn($builder, $expression, $criterion); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
return $group->isAnd() |
|
|
|
|
48
|
|
|
? $builder->andWhere($expression) |
49
|
2 |
|
: $builder->orWhere($expression); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param QueryBuilder $builder |
54
|
|
|
* @param Andx $context |
55
|
|
|
* @param WhereGroup $group |
56
|
|
|
* @return \Generator |
57
|
|
|
*/ |
58
|
|
|
protected function applyGroup(QueryBuilder $builder, Andx $context, WhereGroup $group): \Generator |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return $this->apply($builder, $group); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param QueryBuilder $builder |
65
|
|
|
* @param Andx $context |
66
|
|
|
* @param Where $where |
67
|
|
|
* @return \Generator |
68
|
|
|
*/ |
69
|
2 |
|
protected function applyWhere(QueryBuilder $builder, Andx $context, Where $where): \Generator |
70
|
|
|
{ |
71
|
2 |
|
$expression = new Expression($builder, $where->getOperator(), $where->getValue()); |
72
|
2 |
|
yield from $result = $expression->create($where->getField()); |
73
|
|
|
|
74
|
2 |
|
if ($where->isAnd()) { |
75
|
2 |
|
$context->add($result->getReturn()); |
76
|
|
|
} else { |
77
|
2 |
|
$builder->orWhere($result->getReturn()); |
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param WhereGroup $group |
83
|
|
|
* @return iterable|callable[] |
84
|
|
|
*/ |
85
|
2 |
|
protected function getInnerSelections(WhereGroup $group): iterable |
86
|
|
|
{ |
87
|
2 |
|
$query = $group->getQuery(); |
88
|
|
|
|
89
|
2 |
|
foreach ($query->getCriteria() as $criterion) { |
90
|
2 |
|
foreach (static::ALLOWED_INNER_TYPES as $typeOf => $fn) { |
91
|
2 |
|
if ($criterion instanceof $typeOf) { |
92
|
2 |
|
yield $criterion => [$this, $fn]; |
93
|
2 |
|
continue 2; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$error = 'Groups not allowed for %s criterion'; |
98
|
|
|
throw new \LogicException(\sprintf($error, \get_class($criterion))); |
99
|
|
|
} |
100
|
2 |
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.