|
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
|
1414 |
|
public function __construct($type, array $parts = []) |
|
46
|
|
|
{ |
|
47
|
1414 |
|
$this->type = $type; |
|
48
|
|
|
|
|
49
|
1414 |
|
$this->addMultiple($parts); |
|
50
|
1414 |
|
} |
|
51
|
|
|
|
|
52
|
1164 |
|
public static function and($part, ...$parts) : self |
|
53
|
|
|
{ |
|
54
|
1164 |
|
return new self(self::TYPE_AND, array_merge([$part], $parts)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1326 |
|
public static function or($part, ...$parts) : self |
|
58
|
|
|
{ |
|
59
|
1326 |
|
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
|
1414 |
|
public function addMultiple(array $parts = []) |
|
72
|
|
|
{ |
|
73
|
1414 |
|
foreach ($parts as $part) { |
|
74
|
1390 |
|
$this->add($part); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1414 |
|
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
|
1414 |
|
public function add($part) |
|
90
|
|
|
{ |
|
91
|
1414 |
|
if (empty($part)) { |
|
92
|
1280 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1414 |
|
if ($part instanceof self && count($part) === 0) { |
|
96
|
1274 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1414 |
|
$this->parts[] = $part; |
|
100
|
|
|
|
|
101
|
1414 |
|
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
|
1318 |
|
public function with($part, ...$parts) : self |
|
111
|
|
|
{ |
|
112
|
1318 |
|
$that = clone $this; |
|
113
|
|
|
|
|
114
|
1318 |
|
$that->parts[] = $part; |
|
115
|
|
|
|
|
116
|
1318 |
|
foreach ($parts as $part) { |
|
|
|
|
|
|
117
|
|
|
$that->parts[] = $part; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1318 |
|
return $that; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Retrieves the amount of expressions on composite expression. |
|
125
|
|
|
* |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
1408 |
|
public function count() |
|
129
|
|
|
{ |
|
130
|
1408 |
|
return count($this->parts); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Retrieves the string representation of this composite expression. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1330 |
|
public function __toString() |
|
139
|
|
|
{ |
|
140
|
1330 |
|
if ($this->count() === 1) { |
|
141
|
1274 |
|
return (string) $this->parts[0]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1262 |
|
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
|
624 |
|
public function getType() |
|
153
|
|
|
{ |
|
154
|
624 |
|
return $this->type; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|