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 RDS\Hydrogen\Criteria\Criterion; |
15
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
16
|
|
|
use RDS\Hydrogen\Criteria\WhereGroup; |
17
|
|
|
use RDS\Hydrogen\Criteria\Having; |
18
|
|
|
use RDS\Hydrogen\Criteria\Where; |
19
|
|
|
use RDS\Hydrogen\Processor\DatabaseProcessor\Common\Expression; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class HavingGroupBuilder |
23
|
|
|
*/ |
24
|
|
|
class HavingGroupBuilder extends GroupBuilder |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string[]|Criterion[] |
28
|
|
|
*/ |
29
|
|
|
protected const ALLOWED_INNER_TYPES = [ |
30
|
|
|
Where::class => 'applyWhere', |
31
|
|
|
Having::class => 'applyWhere', |
32
|
|
|
WhereGroup::class => 'applyGroup', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param QueryBuilder $builder |
37
|
|
|
* @param CriterionInterface|WhereGroup $group |
38
|
|
|
* @return iterable|null |
39
|
|
|
*/ |
40
|
|
|
public function apply($builder, CriterionInterface $group): ?iterable |
41
|
|
|
{ |
42
|
|
|
$expression = $builder->expr()->andX(); |
43
|
|
|
|
44
|
|
|
foreach ($this->getInnerSelections($group) as $criterion => $fn) { |
|
|
|
|
45
|
|
|
yield from $fn($builder, $expression, $criterion); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $group->isAnd() ? $builder->andHaving($expression) : $builder->orHaving($expression); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param QueryBuilder $builder |
53
|
|
|
* @param Andx $context |
54
|
|
|
* @param Where $where |
55
|
|
|
* @return \Generator |
56
|
|
|
*/ |
57
|
|
|
final protected function applyWhere(QueryBuilder $builder, Andx $context, Where $where): \Generator |
58
|
|
|
{ |
59
|
|
|
$expression = new Expression($builder, $where->getOperator(), $where->getValue()); |
60
|
|
|
yield from $result = $expression->create($where->getField()); |
61
|
|
|
|
62
|
|
|
if ($where->isAnd()) { |
63
|
|
|
$context->add($result->getReturn()); |
64
|
|
|
} else { |
65
|
|
|
$builder->orHaving($result->getReturn()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.