Repository navigation
Expand file tree
/
Copy pathPredicateSet.php
More file actions
222 lines (180 loc) · 6.04 KB
/
Copy pathPredicateSet.php
File metadata and controls
222 lines (180 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace PhpDb\Sql\Predicate;
use Closure;
use Countable;
use Override;
use PhpDb\Sql\Exception;
use PhpDb\Sql\Expression;
use PhpDb\Sql\Predicate\Expression as PredicateExpression;
use ReturnTypeWillChange;
use function count;
use function implode;
use function is_array;
use function is_string;
use function str_contains;
// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
class PredicateSet implements PredicateInterface, Countable
{
final public const OP_AND = 'AND';
final public const OP_OR = 'OR';
/** @deprecated Use OP_AND instead */
final public const COMBINED_BY_AND = self::OP_AND;
/** @deprecated Use OP_OR instead */
final public const COMBINED_BY_OR = self::OP_OR;
protected string $defaultCombination = self::OP_AND;
protected array $predicates = [];
/**
* Constructor
*/
public function __construct(?array $predicates = null, string $defaultCombination = self::OP_AND)
{
$this->defaultCombination = $defaultCombination;
if ($predicates !== null) {
foreach ($predicates as $predicate) {
$this->addPredicate($predicate);
}
}
}
/**
* Add predicate to set
*/
public function addPredicate(PredicateInterface $predicate, ?string $combination = null): static
{
$combination ??= $this->defaultCombination;
match ($combination) {
self::OP_AND => $this->andPredicate($predicate),
self::OP_OR => $this->orPredicate($predicate),
default => throw new Exception\InvalidArgumentException(
"Invalid combination: expected 'AND' or 'OR'"
),
};
return $this;
}
/**
* Add predicates to set
*
* @throws Exception\InvalidArgumentException
*/
public function addPredicates(
PredicateInterface|Closure|string|array $predicates,
string $combination = self::OP_AND
): static {
if ($predicates instanceof PredicateInterface) {
$this->addPredicate($predicates, $combination);
return $this;
}
if ($predicates instanceof Closure) {
$predicates($this);
return $this;
}
if (is_string($predicates)) {
$predicate = str_contains($predicates, Expression::PLACEHOLDER)
? new PredicateExpression($predicates) : new Literal($predicates);
$this->addPredicate($predicate, $combination);
return $this;
}
foreach ($predicates as $pkey => $pvalue) {
if (is_string($pkey)) {
if (str_contains($pkey, '?')) {
$predicate = new PredicateExpression($pkey, $pvalue);
} elseif ($pvalue === null) {
$predicate = new IsNull($pkey);
} elseif (is_array($pvalue)) {
$predicate = new In($pkey, $pvalue);
} elseif ($pvalue instanceof PredicateInterface) {
throw new Exception\InvalidArgumentException(
'Using Predicate must not use string keys'
);
} else {
$predicate = new Operator($pkey, Operator::OP_EQ, $pvalue);
}
} elseif ($pvalue instanceof PredicateInterface) {
$predicate = $pvalue;
} elseif ($pvalue instanceof Expression) {
$predicate = new PredicateExpression(
$pvalue->getExpression(),
$pvalue->getParameters()
);
} else {
$predicate = str_contains($pvalue, Expression::PLACEHOLDER)
? new Expression($pvalue) : new Literal($pvalue);
}
$this->addPredicate($predicate, $combination);
}
return $this;
}
/**
* Return the predicates
*/
public function getPredicates(): array
{
return $this->predicates;
}
/**
* Add predicate using OR operator
*/
public function orPredicate(PredicateInterface $predicate): static
{
$this->predicates[] = [self::OP_OR, $predicate];
return $this;
}
/**
* Add predicate using AND operator
*/
public function andPredicate(PredicateInterface $predicate): static
{
$this->predicates[] = [self::OP_AND, $predicate];
return $this;
}
/** @inheritDoc */
#[Override]
public function getExpressionData(): array
{
$predicateCount = count($this->predicates);
if ($predicateCount === 0) {
return ['spec' => '', 'values' => []];
}
if ($predicateCount === 1) {
[$operator, $predicate] = $this->predicates[0];
$expressionData = $predicate->getExpressionData();
if ($predicate instanceof self) {
return [
'spec' => "({$expressionData['spec']})",
'values' => $expressionData['values'],
];
}
return $expressionData;
}
$specParts = [];
$allValues = [];
$first = true;
foreach ($this->predicates as [$operator, $predicate]) {
$expressionData = $predicate->getExpressionData();
$spec = $predicate instanceof self
? "({$expressionData['spec']})"
: $expressionData['spec'];
$specParts[] = $first ? $spec : "{$operator} {$spec}";
$first = false;
$values = $expressionData['values'];
if ($values !== []) {
foreach ($values as $value) {
$allValues[] = $value;
}
}
}
return [
'spec' => implode(' ', $specParts),
'values' => $allValues,
];
}
/**
* Get count of attached predicates
*/
#[Override]
#[ReturnTypeWillChange]
public function count(): int
{
return count($this->predicates);
}
}