1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Query\Expression; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use function count; |
9
|
|
|
use function implode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Composite expression is responsible to build a group of similar expression. |
13
|
|
|
*/ |
14
|
|
|
class CompositeExpression implements Countable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Constant that represents an AND composite expression. |
18
|
|
|
*/ |
19
|
|
|
public const TYPE_AND = 'AND'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constant that represents an OR composite expression. |
23
|
|
|
*/ |
24
|
|
|
public const TYPE_OR = 'OR'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The instance type of composite expression. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $type; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Each expression part of the composite expression. |
35
|
|
|
* |
36
|
|
|
* @var self[]|string[] |
37
|
|
|
*/ |
38
|
|
|
private $parts = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $type Instance type of composite expression. |
42
|
|
|
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression. |
43
|
|
|
*/ |
44
|
891 |
|
public function __construct(string $type, array $parts = []) |
45
|
|
|
{ |
46
|
891 |
|
$this->type = $type; |
47
|
|
|
|
48
|
891 |
|
$this->addMultiple($parts); |
|
|
|
|
49
|
891 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds multiple parts to composite expression. |
53
|
|
|
* |
54
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
55
|
|
|
* |
56
|
|
|
* @param array<int, self|string> $parts |
57
|
|
|
* |
58
|
891 |
|
* @return $this |
59
|
|
|
*/ |
60
|
891 |
|
public function addMultiple(array $parts = []) : self |
61
|
567 |
|
{ |
62
|
|
|
foreach ($parts as $part) { |
63
|
|
|
$this->add($part); |
|
|
|
|
64
|
891 |
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Adds an expression to composite expression. |
71
|
|
|
* |
72
|
|
|
* @deprecated This class will be made immutable. Use with() instead. |
73
|
|
|
* |
74
|
891 |
|
* @param self|string $part |
75
|
|
|
* |
76
|
891 |
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function add($part) : self |
79
|
|
|
{ |
80
|
891 |
|
if (empty($part)) { |
81
|
27 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
891 |
|
if ($part instanceof self && count($part) === 0) { |
85
|
|
|
return $this; |
86
|
891 |
|
} |
87
|
|
|
|
88
|
|
|
$this->parts[] = $part; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
864 |
|
|
93
|
|
|
/** |
94
|
864 |
|
* Returns a new CompositeExpression with the given parts added. |
95
|
|
|
* |
96
|
|
|
* @param self|string $part |
97
|
|
|
* @param self|string ...$parts |
98
|
|
|
*/ |
99
|
|
|
public function with($part, ...$parts) : self |
100
|
810 |
|
{ |
101
|
|
|
$that = clone $this; |
102
|
810 |
|
|
103
|
189 |
|
$that->parts[] = $part; |
104
|
|
|
|
105
|
|
|
foreach ($parts as $part) { |
|
|
|
|
106
|
621 |
|
$that->parts[] = $part; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $that; |
110
|
|
|
} |
111
|
|
|
|
112
|
54 |
|
/** |
113
|
|
|
* Retrieves the amount of expressions on composite expression. |
114
|
54 |
|
*/ |
115
|
|
|
public function count() : int |
116
|
|
|
{ |
117
|
|
|
return count($this->parts); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Retrieves the string representation of this composite expression. |
122
|
|
|
*/ |
123
|
|
|
public function __toString() : string |
124
|
|
|
{ |
125
|
|
|
if ($this->count() === 1) { |
126
|
|
|
return (string) $this->parts[0]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the type of this composite expression (AND/OR). |
134
|
|
|
*/ |
135
|
|
|
public function getType() : string |
136
|
|
|
{ |
137
|
|
|
return $this->type; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.