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\Common\Field; |
14
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
15
|
|
|
use RDS\Hydrogen\Criteria\Join; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class JoinBuilder |
19
|
|
|
*/ |
20
|
|
|
class JoinBuilder extends WhereBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $relations = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param QueryBuilder $builder |
29
|
|
|
* @param CriterionInterface|Join $relation |
30
|
|
|
* @return \Generator |
31
|
|
|
*/ |
32
|
2 |
|
public function apply($builder, CriterionInterface $relation): \Generator |
33
|
|
|
{ |
34
|
2 |
|
$field = $relation->getField(); |
35
|
2 |
|
$alias = $relation->getAlias(); |
|
|
|
|
36
|
|
|
|
37
|
2 |
|
foreach ($field->getChunks() as $chunk) { |
38
|
2 |
|
if (isset($this->relations[$chunk])) { |
39
|
|
|
$alias = $this->relations[$chunk]; |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
$alias = $this->join($relation->getType(), $builder, $chunk, $alias); |
|
|
|
|
44
|
|
|
|
45
|
2 |
|
$this->relations[$chunk] = $alias; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
yield $relation->getQuery()->withAlias($alias); |
|
|
|
|
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $type |
53
|
|
|
* @param QueryBuilder $builder |
54
|
|
|
* @param string $child |
55
|
|
|
* @param string $parent |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
2 |
|
private function join(int $type, QueryBuilder $builder, string $child, string $parent): string |
59
|
|
|
{ |
60
|
2 |
|
$relation = Field::new($child)->withAlias($parent); |
61
|
2 |
|
$alias = \str_replace('.', '_', $relation->toString()); |
62
|
|
|
|
63
|
|
|
switch ($type) { |
64
|
2 |
|
case Join::TYPE_JOIN: |
65
|
|
|
$builder->join($relation->toString(), $alias); |
66
|
|
|
break; |
67
|
|
|
|
68
|
2 |
|
case Join::TYPE_LEFT_JOIN: |
69
|
2 |
|
$builder->leftJoin($relation->toString(), $alias); |
70
|
2 |
|
break; |
71
|
|
|
|
72
|
|
|
case Join::TYPE_INNER_JOIN: |
73
|
|
|
$builder->innerJoin($relation->toString(), $alias); |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
default: |
77
|
|
|
throw new \InvalidArgumentException('Unexpected join type'); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$builder->addSelect($alias); |
81
|
|
|
|
82
|
2 |
|
return $alias; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: