1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Query\Expression; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use function array_merge; |
7
|
|
|
use function count; |
8
|
|
|
use function implode; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Composite expression is responsible to build a group of similar expression. |
12
|
|
|
*/ |
13
|
|
|
class CompositeExpression implements Countable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constant that represents an AND composite expression. |
17
|
|
|
*/ |
18
|
|
|
public const TYPE_AND = 'AND'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constant that represents an OR composite expression. |
22
|
|
|
*/ |
23
|
|
|
public const TYPE_OR = 'OR'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The instance type of composite expression. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Each expression part of the composite expression. |
34
|
|
|
* |
35
|
|
|
* @var self[]|string[] |
36
|
|
|
*/ |
37
|
|
|
private $parts = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @internal Use the and() / or() factory methods. |
41
|
|
|
* |
42
|
|
|
* @param string $type Instance type of composite expression. |
43
|
|
|
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression. |
44
|
|
|
*/ |
45
|
1301 |
|
public function __construct($type, array $parts = []) |
46
|
|
|
{ |
47
|
1301 |
|
$this->type = $type; |
48
|
|
|
|
49
|
1301 |
|
$this->addMultiple($parts); |
50
|
1301 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param self|string $part |
54
|
|
|
* @param self|string ...$parts |
55
|
|
|
*/ |
56
|
1088 |
|
public static function and($part, ...$parts) : self |
57
|
|
|
{ |
58
|
1088 |
|
return new self(self::TYPE_AND, array_merge([$part], $parts)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param self|string $part |
63
|
|
|
* @param self|string ...$parts |
64
|
|
|
*/ |
65
|
1257 |
|
public static function or($part, ...$parts) : self |
66
|
|
|
{ |
67
|
1257 |
|
return new self(self::TYPE_OR, array_merge([$part], $parts)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds multiple parts to composite expression. |
72
|
|
|
* |
73
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
74
|
|
|
* |
75
|
|
|
* @param self[]|string[] $parts |
76
|
|
|
* |
77
|
|
|
* @return CompositeExpression |
78
|
|
|
*/ |
79
|
1301 |
|
public function addMultiple(array $parts = []) |
80
|
|
|
{ |
81
|
1301 |
|
foreach ($parts as $part) { |
82
|
1289 |
|
$this->add($part); |
83
|
|
|
} |
84
|
|
|
|
85
|
1301 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds an expression to composite expression. |
90
|
|
|
* |
91
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
92
|
|
|
* |
93
|
|
|
* @param mixed $part |
94
|
|
|
* |
95
|
|
|
* @return CompositeExpression |
96
|
|
|
*/ |
97
|
1301 |
|
public function add($part) |
98
|
|
|
{ |
99
|
1301 |
|
if (empty($part)) { |
100
|
1223 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
1301 |
|
if ($part instanceof self && count($part) === 0) { |
104
|
1220 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
1301 |
|
$this->parts[] = $part; |
108
|
|
|
|
109
|
1301 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns a new CompositeExpression with the given parts added. |
114
|
|
|
* |
115
|
|
|
* @param self|string $part |
116
|
|
|
* @param self|string ...$parts |
117
|
|
|
*/ |
118
|
1253 |
|
public function with($part, ...$parts) : self |
119
|
|
|
{ |
120
|
1253 |
|
$that = clone $this; |
121
|
|
|
|
122
|
1253 |
|
$that->parts[] = $part; |
123
|
|
|
|
124
|
1253 |
|
foreach ($parts as $part) { |
|
|
|
|
125
|
|
|
$that->parts[] = $part; |
126
|
|
|
} |
127
|
|
|
|
128
|
1253 |
|
return $that; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieves the amount of expressions on composite expression. |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
1298 |
|
public function count() |
137
|
|
|
{ |
138
|
1298 |
|
return count($this->parts); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Retrieves the string representation of this composite expression. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1226 |
|
public function __toString() |
147
|
|
|
{ |
148
|
1226 |
|
if ($this->count() === 1) { |
149
|
1198 |
|
return (string) $this->parts[0]; |
150
|
|
|
} |
151
|
|
|
|
152
|
1181 |
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns the type of this composite expression (AND/OR). |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
587 |
|
public function getType() |
161
|
|
|
{ |
162
|
587 |
|
return $this->type; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|