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
|
1468 |
|
public function __construct($type, array $parts = []) |
46
|
|
|
{ |
47
|
1468 |
|
$this->type = $type; |
48
|
|
|
|
49
|
1468 |
|
$this->addMultiple($parts); |
50
|
1468 |
|
} |
51
|
|
|
|
52
|
1210 |
|
public static function and($part, ...$parts) : self |
53
|
|
|
{ |
54
|
1210 |
|
return new self(self::TYPE_AND, array_merge([$part], $parts)); |
55
|
|
|
} |
56
|
|
|
|
57
|
1380 |
|
public static function or($part, ...$parts) : self |
58
|
|
|
{ |
59
|
1380 |
|
return new self(self::TYPE_OR, array_merge([$part], $parts)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds multiple parts to composite expression. |
64
|
|
|
* |
65
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
66
|
|
|
* |
67
|
|
|
* @param self[]|string[] $parts |
68
|
|
|
* |
69
|
|
|
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression |
70
|
|
|
*/ |
71
|
1468 |
|
public function addMultiple(array $parts = []) |
72
|
|
|
{ |
73
|
1468 |
|
foreach ($parts as $part) { |
74
|
1444 |
|
$this->add($part); |
75
|
|
|
} |
76
|
|
|
|
77
|
1468 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds an expression to composite expression. |
82
|
|
|
* |
83
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
84
|
|
|
* |
85
|
|
|
* @param mixed $part |
86
|
|
|
* |
87
|
|
|
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression |
88
|
|
|
*/ |
89
|
1468 |
|
public function add($part) |
90
|
|
|
{ |
91
|
1468 |
|
if (empty($part)) { |
92
|
1333 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
1468 |
|
if ($part instanceof self && count($part) === 0) { |
96
|
1327 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
1468 |
|
$this->parts[] = $part; |
100
|
|
|
|
101
|
1468 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a new CompositeExpression with the given parts added. |
106
|
|
|
* |
107
|
|
|
* @param self|string $part |
108
|
|
|
* @param self|string ...$parts |
109
|
|
|
*/ |
110
|
1372 |
|
public function with($part, ...$parts) : self |
111
|
|
|
{ |
112
|
1372 |
|
$that = clone $this; |
113
|
|
|
|
114
|
1372 |
|
$that->parts[] = $part; |
115
|
|
|
|
116
|
1372 |
|
foreach ($parts as $part) { |
|
|
|
|
117
|
|
|
$that->parts[] = $part; |
118
|
|
|
} |
119
|
|
|
|
120
|
1372 |
|
return $that; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieves the amount of expressions on composite expression. |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
1462 |
|
public function count() |
129
|
|
|
{ |
130
|
1462 |
|
return count($this->parts); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Retrieves the string representation of this composite expression. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
1381 |
|
public function __toString() |
139
|
|
|
{ |
140
|
1381 |
|
if ($this->count() === 1) { |
141
|
1325 |
|
return (string) $this->parts[0]; |
142
|
|
|
} |
143
|
|
|
|
144
|
1312 |
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the type of this composite expression (AND/OR). |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
649 |
|
public function getType() |
153
|
|
|
{ |
154
|
649 |
|
return $this->type; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|