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\CriterionInterface; |
14
|
|
|
use RDS\Hydrogen\Criteria\Relation; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RelationProcessor |
18
|
|
|
*/ |
19
|
|
|
class RelationProcessor extends CriterionProcessor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private static $relationId = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array|string[] |
28
|
|
|
*/ |
29
|
|
|
private $relations = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param QueryBuilder $builder |
33
|
|
|
* @param CriterionInterface|Relation $with |
34
|
|
|
* @return QueryBuilder |
35
|
|
|
*/ |
36
|
|
|
public function apply(QueryBuilder $builder, CriterionInterface $with): QueryBuilder |
37
|
|
|
{ |
38
|
|
|
$alias = $this->alias; |
39
|
|
|
|
40
|
|
|
foreach ($with->getRelation()->split() as $relation) { |
|
|
|
|
41
|
|
|
$parent = $relation->withAlias($alias); |
42
|
|
|
$exists = $this->hasAlias($parent); |
43
|
|
|
$childAlias = $this->fetchAlias($parent, $relation->getName()); |
44
|
|
|
|
45
|
|
|
if (! $exists) { |
46
|
|
|
$builder->leftJoin($parent, $childAlias); |
47
|
|
|
$builder->addSelect($childAlias); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$alias = $childAlias; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->processor->apply($builder, $with->getQuery(), $alias); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $builder; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $parent |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
private function hasAlias(string $parent): bool |
63
|
|
|
{ |
64
|
|
|
return \array_key_exists($parent, $this->relations); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $parent |
69
|
|
|
* @param string $class |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function fetchAlias(string $parent, string $class): string |
73
|
|
|
{ |
74
|
|
|
if (! $this->hasAlias($parent)) { |
75
|
|
|
$this->relations[$parent] = $this->createAlias($class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->relations[$parent]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $class |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private function createAlias(string $class): string |
86
|
|
|
{ |
87
|
|
|
return 'ref_' . \snake_case(\class_basename($class)) . '_' . ++self::$relationId; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: