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\QueryBuilder; |
13
|
|
|
use RDS\Hydrogen\Criteria\Where; |
14
|
|
|
use Doctrine\ORM\Query\Expr\Andx; |
15
|
|
|
use RDS\Hydrogen\Criteria\Criterion; |
16
|
|
|
use RDS\Hydrogen\Criteria\WhereGroup; |
17
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
18
|
|
|
use RDS\Hydrogen\Processor\DatabaseProcessor\Common\Expression; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class GroupBuilder |
22
|
|
|
*/ |
23
|
|
|
class GroupBuilder extends Builder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string[]|Criterion[] |
27
|
|
|
*/ |
28
|
|
|
protected const ALLOWED_INNER_TYPES = [ |
29
|
|
|
Where::class => 'applyWhere', |
30
|
|
|
WhereGroup::class => 'applyGroup', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param QueryBuilder $builder |
35
|
|
|
* @param Andx $context |
36
|
|
|
* @param WhereGroup $group |
37
|
|
|
* @return \Generator |
38
|
|
|
*/ |
39
|
|
|
protected function applyGroup(QueryBuilder $builder, Andx $context, WhereGroup $group): \Generator |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
return $this->apply($builder, $group); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param QueryBuilder $builder |
46
|
|
|
* @param CriterionInterface|WhereGroup $group |
47
|
|
|
* @return iterable|null |
48
|
|
|
*/ |
49
|
2 |
|
public function apply($builder, CriterionInterface $group): ?iterable |
50
|
|
|
{ |
51
|
2 |
|
$expression = $builder->expr()->andX(); |
52
|
|
|
|
53
|
2 |
|
foreach ($this->getInnerSelections($group) as $criterion => $fn) { |
|
|
|
|
54
|
2 |
|
yield from $fn($builder, $expression, $criterion); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $group->isAnd() ? $builder->andWhere($expression) : $builder->orWhere($expression); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param WhereGroup $group |
62
|
|
|
* @return iterable|callable[] |
63
|
|
|
*/ |
64
|
2 |
|
protected function getInnerSelections(WhereGroup $group): iterable |
65
|
|
|
{ |
66
|
2 |
|
$query = $group->getQuery(); |
67
|
|
|
|
68
|
2 |
|
foreach ($query->getCriteria() as $criterion) { |
69
|
2 |
|
foreach (static::ALLOWED_INNER_TYPES as $typeOf => $fn) { |
70
|
2 |
|
if ($criterion instanceof $typeOf) { |
71
|
2 |
|
yield $criterion => [$this, $fn]; |
72
|
2 |
|
continue 2; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$error = 'Groups not allowed for %s criterion'; |
77
|
|
|
throw new \LogicException(\sprintf($error, \get_class($criterion))); |
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param QueryBuilder $builder |
83
|
|
|
* @param Andx $context |
84
|
|
|
* @param Where $where |
85
|
|
|
* @return \Generator |
86
|
|
|
*/ |
87
|
2 |
|
protected function applyWhere(QueryBuilder $builder, Andx $context, Where $where): \Generator |
88
|
|
|
{ |
89
|
2 |
|
$expression = new Expression($builder, $where->getOperator(), $where->getValue()); |
90
|
2 |
|
yield from $result = $expression->create($where->getField()); |
91
|
|
|
|
92
|
2 |
|
if ($where->isAnd()) { |
93
|
2 |
|
$context->add($result->getReturn()); |
94
|
|
|
} else { |
95
|
2 |
|
$builder->orWhere($result->getReturn()); |
96
|
|
|
} |
97
|
2 |
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.