-
Notifications
You must be signed in to change notification settings - Fork 1
/
Conditions.php
225 lines (199 loc) · 6.18 KB
/
Conditions.php
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
223
224
225
<?php
namespace Tivins\Database;
use Tivins\Database\Exceptions\ConditionException;
/**
* Base class of Query.
* Allow adding conditions on queries, like 'is null' et 'like %%', operators, ...
*/
class Conditions
{
public const MODE_AND = 'and';
public const MODE_OR = 'or';
protected static array $operators = ['<','<=','=','!=','>=','>','<=>','<>', 'between'];
protected array $conditions = [];
/**
* @var Conditions[]
*/
protected array $nestedConditions = [];
protected string $mode = self::MODE_AND;
/**
*
*/
public function __construct(string $mode = self::MODE_AND)
{
$this->mode = $mode;
}
/**
* @return string[]
*/
public static function getOperators(): array
{
return self::$operators;
}
/**
* @param string[] $operators
*/
public static function setOperators(array $operators): void
{
self::$operators = $operators;
}
/**
*
*/
public function whereIn(string $field, array $values): self
{
$stmt = "$field in (" . implode(',', array_fill(0, count($values), '?')) . ")";
$this->pushCondition($stmt, $values);
return $this;
}
public function between(string $field, $low, $high): self
{
$this->pushCondition("$field between ? and ?", [$low, $high]);
return $this;
}
/**
* Search on the given field's value for a value matching the given value.
*
* @param string $value A string using `%` character(s) as wildcard (ex. `"%search%"`).
*/
public function like(string $field, string $value): self
{
$this->pushCondition("$field like ?", [$value]);
return $this;
}
/**
* Search on the given field for a NULL value.
*/
public function isNull(string $field): self
{
$this->pushCondition("$field is null", []);
return $this;
}
/**
* Search on the given field for a non-NULL value.
*/
public function isNotNull(string $field): self
{
$this->pushCondition("$field is not null", []);
return $this;
}
public function isEqual(string $field, $value): self
{
$this->pushCondition("$field = ?", [$value]);
return $this;
}
public function isDifferent(string $field, $value): self
{
$this->pushCondition("$field != ?", [$value]);
return $this;
}
public function nest(Conditions $field): static
{
$this->nestedConditions[] = $field;
return $this;
}
/**
* Add a specific condition to the current query.
*
* @param Conditions|string $field
* @param null $value
* @param string $operator
* @return Conditions
* @throws ConditionException
* @see Query
* @see https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html
*/
public function condition(Conditions|string $field, $value = null, string $operator = '='): self
{
if ($field instanceof Conditions) {
$this->nestedConditions[] = $field;
return $this;
}
if ($operator == 'in') {
if (!is_array($value)) {
throw new ConditionException('Invalid value');
}
return $this->whereIn($field, $value);
}
if ($operator == 'like') {
return $this->like($field, $value);
}
if (!in_array($operator, self::$operators)) {
throw new ConditionException('Invalid operator');
}
$this->pushCondition("$field $operator ?", [$value]);
return $this;
}
/**
* Shortcut to `->condition($field,$value)` (and suppress ConditionException).
*
* @param string $field
* @param mixed $value
* @return $this
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function equalsTo(string $field, mixed $value = null): self
{
return $this->condition($field, $value);
}
/**
* Add an expression to the current conditions.
*
* ```php
* $query->conditionExpression('concat(field, ?) = another_field', $someValue);
* ```
*
* @param string $expression The SQL expression.
* @param mixed ...$args The parameters for the given expression.
* @return self The current request.
*
* @see SelectTest::testConditionExpression();
*/
public function conditionExpression(string $expression, ...$args): self
{
$this->pushCondition($expression, $args);
return $this;
}
/**
* Build the query string for the (nested) conditions with according parameters.
*
* Return_ an array with two values :
* 1. The SQL string for the condition,
* 2. An array containing the condition's parameters.
*
* This function can use a recursive call in case of nested conditions.
* In this case, the query strings are 'glued' using their own condition mode ('and','or'),
* and the parameters are concatenated.
*/
public function buildConditions(): QueryData
{
if (empty($this->conditions) && empty($this->nestedConditions)) {
return new QueryData();
}
$query = implode(' ' . $this->mode . ' ', array_column($this->conditions, 'cond'));
$query = $this->mode == self::MODE_OR ? "($query)" : $query;
$parameters = array_merge(...array_column($this->conditions, 'data'));
$qData = new QueryData($query, $parameters);
foreach ($this->nestedConditions as $k => $nestedConditions) {
$queryData = $nestedConditions->buildConditions();
$qData->merge($queryData, " $this->mode ");
}
// Empiric fix.
if (str_starts_with($qData->sql, ' and ')) {
$qData->sql = substr($qData->sql, 5);
};
return $qData;
}
/**
* Private function used by most of the conditions shortcuts.
* Push the given condition and parameters to the current query.
*/
private function pushCondition(string $condition, array $data): void
{
$this->conditions[] = [
'cond' => $condition,
'data' => $data,
];
}
}