1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BenTools\Where\Helper; |
5
|
|
|
|
6
|
|
|
use BenTools\Where\Expression\Expression; |
7
|
|
|
use function BenTools\Where\placeholders; |
8
|
|
|
use function BenTools\Where\where; |
9
|
|
|
|
10
|
|
|
final class FieldHelper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $field; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Field constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $field) |
21
|
|
|
{ |
22
|
|
|
$this->field = $field; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return Expression |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
|
|
public function isNull(): Expression |
30
|
|
|
{ |
31
|
|
|
return where(sprintf('%s IS NULL', $this->field)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Expression |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function isNotNull(): Expression |
39
|
|
|
{ |
40
|
|
|
return where(sprintf('%s IS NOT NULL', $this->field)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Expression |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function isTrue(): Expression |
48
|
|
|
{ |
49
|
|
|
return where(sprintf('%s = TRUE', $this->field)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Expression |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
public function isFalse(): Expression |
57
|
|
|
{ |
58
|
|
|
return where(sprintf('%s = FALSE', $this->field)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $values |
63
|
|
|
* @param null|string $placeholder |
64
|
|
|
* @param string $glue |
65
|
|
|
* @return Expression |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
*/ |
68
|
|
|
public function in(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
69
|
|
|
{ |
70
|
|
|
$expression = '%s IN (%s)'; |
71
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)) : where(sprintf($expression, $this->field, implode(', ', $values))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $values |
76
|
|
|
* @param string $placeholder |
77
|
|
|
* @param string $glue |
78
|
|
|
* @return Expression |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
*/ |
81
|
|
|
public function notIn(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
82
|
|
|
{ |
83
|
|
|
$expression = '%s NOT IN (%s)'; |
84
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)) : where(sprintf($expression, $this->field, implode(', ', $values))); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $value |
89
|
|
|
* @param string $placeholder |
90
|
|
|
* @return Expression |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
public function equals($value, ?string $placeholder = '?'): Expression |
94
|
|
|
{ |
95
|
|
|
$expression = '%s = %s'; |
96
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $value |
101
|
|
|
* @param string $placeholder |
102
|
|
|
* @return Expression |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function notEquals($value, ?string $placeholder = '?'): Expression |
106
|
|
|
{ |
107
|
|
|
$expression = '%s <> %s'; |
108
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $value |
113
|
|
|
* @param string $placeholder |
114
|
|
|
* @return Expression |
115
|
|
|
* @throws \InvalidArgumentException |
116
|
|
|
*/ |
117
|
|
|
public function lt($value, ?string $placeholder = '?'): Expression |
118
|
|
|
{ |
119
|
|
|
$expression = '%s < %s'; |
120
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $value |
125
|
|
|
* @param string $placeholder |
126
|
|
|
* @return Expression |
127
|
|
|
* @throws \InvalidArgumentException |
128
|
|
|
*/ |
129
|
|
|
public function lte($value, ?string $placeholder = '?'): Expression |
130
|
|
|
{ |
131
|
|
|
$expression = '%s <= %s'; |
132
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $value |
137
|
|
|
* @param string $placeholder |
138
|
|
|
* @return Expression |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
*/ |
141
|
|
|
public function gt($value, ?string $placeholder = '?'): Expression |
142
|
|
|
{ |
143
|
|
|
$expression = '%s > %s'; |
144
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $value |
149
|
|
|
* @param string $placeholder |
150
|
|
|
* @return Expression |
151
|
|
|
* @throws \InvalidArgumentException |
152
|
|
|
*/ |
153
|
|
|
public function gte($value, ?string $placeholder = '?'): Expression |
154
|
|
|
{ |
155
|
|
|
$expression = '%s >= %s'; |
156
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $start |
161
|
|
|
* @param $end |
162
|
|
|
* @param string $placeholder |
163
|
|
|
* @return Expression |
164
|
|
|
* @throws \InvalidArgumentException |
165
|
|
|
*/ |
166
|
|
|
public function between($start, $end, ?string $placeholder = '?'): Expression |
167
|
|
|
{ |
168
|
|
|
$expression = '%s BETWEEN %s AND %s'; |
169
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end) : where(sprintf($expression, $this->field, $start, $end)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $start |
174
|
|
|
* @param $end |
175
|
|
|
* @param string $placeholder |
176
|
|
|
* @return Expression |
177
|
|
|
* @throws \InvalidArgumentException |
178
|
|
|
*/ |
179
|
|
|
public function notBetween($start, $end, ?string $placeholder = '?'): Expression |
180
|
|
|
{ |
181
|
|
|
$expression = '%s NOT BETWEEN %s AND %s'; |
182
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end) : where(sprintf($expression, $this->field, $start, $end)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $value |
187
|
|
|
* @param string $placeholder |
188
|
|
|
* @param string $surroundWith |
189
|
|
|
* @return Expression |
190
|
|
|
* @throws \InvalidArgumentException |
191
|
|
|
*/ |
192
|
|
|
public function like(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
193
|
|
|
{ |
194
|
|
|
$expression = '%s LIKE %s'; |
195
|
|
|
$value = $surroundWith . $value . $surroundWith; |
196
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $value |
201
|
|
|
* @param string $placeholder |
202
|
|
|
* @param string $surroundWith |
203
|
|
|
* @return Expression |
204
|
|
|
* @throws \InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
public function notLike(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
207
|
|
|
{ |
208
|
|
|
$expression = '%s NOT LIKE %s'; |
209
|
|
|
$value = $surroundWith . $value . $surroundWith; |
210
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $value |
215
|
|
|
* @param string $placeholder |
216
|
|
|
* @param string $surroundWith |
217
|
|
|
* @return Expression |
218
|
|
|
* @throws \InvalidArgumentException |
219
|
|
|
*/ |
220
|
|
|
public function startsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
221
|
|
|
{ |
222
|
|
|
$expression = '%s LIKE %s'; |
223
|
|
|
$value = $value . $surroundWith; |
224
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $value |
229
|
|
|
* @param string $placeholder |
230
|
|
|
* @param string $surroundWith |
231
|
|
|
* @return Expression |
232
|
|
|
* @throws \InvalidArgumentException |
233
|
|
|
*/ |
234
|
|
|
public function notStartsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
235
|
|
|
{ |
236
|
|
|
$expression = '%s NOT LIKE %s'; |
237
|
|
|
$value = $value . $surroundWith; |
238
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $value |
243
|
|
|
* @param string $placeholder |
244
|
|
|
* @param string $surroundWith |
245
|
|
|
* @return Expression |
246
|
|
|
* @throws \InvalidArgumentException |
247
|
|
|
*/ |
248
|
|
|
public function endsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
249
|
|
|
{ |
250
|
|
|
$expression = '%s LIKE %s'; |
251
|
|
|
$value = $surroundWith . $value; |
252
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $value |
257
|
|
|
* @param string $placeholder |
258
|
|
|
* @param string $surroundWith |
259
|
|
|
* @return Expression |
260
|
|
|
* @throws \InvalidArgumentException |
261
|
|
|
*/ |
262
|
|
|
public function notEndsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
263
|
|
|
{ |
264
|
|
|
$expression = '%s NOT LIKE %s'; |
265
|
|
|
$value = $surroundWith . $value; |
266
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|