1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rb\Specification\Doctrine\Query; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Rb\Specification\Doctrine\AbstractSpecification; |
7
|
|
|
use Rb\Specification\Doctrine\Exception\InvalidArgumentException; |
8
|
|
|
use Rb\Specification\Doctrine\SpecificationInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Join. |
12
|
|
|
*/ |
13
|
|
|
class Join extends AbstractSpecification |
14
|
|
|
{ |
15
|
|
|
const JOIN = 'join'; |
16
|
|
|
const LEFT_JOIN = 'leftJoin'; |
17
|
|
|
const INNER_JOIN = 'innerJoin'; |
18
|
|
|
|
19
|
|
|
protected static $types = [self::JOIN, self::LEFT_JOIN, self::INNER_JOIN]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $newAlias; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
private $conditionType = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|SpecificationInterface|null |
33
|
|
|
*/ |
34
|
|
|
private $condition = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
private $indexedBy = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $type = self::JOIN; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $field |
48
|
|
|
* @param string $newAlias |
49
|
|
|
* @param string|null $dqlAlias |
50
|
|
|
* |
51
|
|
|
* @throws InvalidArgumentException |
52
|
|
|
*/ |
53
|
|
|
public function __construct($field, $newAlias, $dqlAlias = null) |
54
|
|
|
{ |
55
|
|
|
$this->newAlias = $newAlias; |
56
|
|
|
|
57
|
|
|
parent::__construct($field, $dqlAlias); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $type |
62
|
|
|
* |
63
|
|
|
* @throws InvalidArgumentException |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function setType($type) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (! in_array($type, self::$types, true)) { |
70
|
|
|
throw new InvalidArgumentException(sprintf( |
71
|
|
|
'"%s" is not a valid type! Valid types: %s', |
72
|
|
|
$type, |
73
|
|
|
implode(', ', self::$types) |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->type = $type; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
86
|
|
|
{ |
87
|
|
|
if (! is_null($this->dqlAlias)) { |
88
|
|
|
$dqlAlias = $this->dqlAlias; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$property = $this->createPropertyWithAlias($dqlAlias); |
92
|
|
|
|
93
|
|
|
$condition = $this->condition; |
94
|
|
|
if ($condition instanceof SpecificationInterface) { |
95
|
|
|
$condition = $condition->modify($queryBuilder, $dqlAlias); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
call_user_func_array( |
99
|
|
|
[$queryBuilder, $this->type], |
100
|
|
|
[$property, $this->newAlias, $this->conditionType, $condition, $this->indexedBy] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the condition type to be used on the join (WITH/ON). |
106
|
|
|
* |
107
|
|
|
* @param string $conditionType |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setConditionType($conditionType) |
112
|
|
|
{ |
113
|
|
|
$this->conditionType = $conditionType; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the condition to be used for the join statement. |
120
|
|
|
* |
121
|
|
|
* @param string|SpecificationInterface $condition |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setCondition($condition) |
126
|
|
|
{ |
127
|
|
|
$this->condition = $condition; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the property which will be used as index for the returned collection. |
134
|
|
|
* |
135
|
|
|
* @param mixed $indexedBy |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function setIndexedBy($indexedBy) |
140
|
|
|
{ |
141
|
|
|
$this->indexedBy = $indexedBy; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.