1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeekLab\GLPDO2\Bindings; |
4
|
|
|
|
5
|
|
|
use \PDO; |
6
|
|
|
use \Exception; |
7
|
|
|
use \TypeError; |
8
|
|
|
use \JsonException; |
9
|
|
|
|
10
|
|
|
class Bindings |
11
|
|
|
{ |
12
|
|
|
/** @var DateTimeBindingInterface $dateTime */ |
13
|
|
|
protected $dateTime; |
14
|
|
|
|
15
|
|
|
/** @var LogicBindingInterface $logic */ |
16
|
|
|
protected $logic; |
17
|
|
|
|
18
|
|
|
/** @var NumericBindingInterface $numeric */ |
19
|
|
|
protected $numeric; |
20
|
|
|
|
21
|
|
|
/** @var RawBindingInterface $raw */ |
22
|
|
|
protected $raw; |
23
|
|
|
|
24
|
|
|
/** @var StringBindingInterface $string */ |
25
|
|
|
protected $string; |
26
|
|
|
|
27
|
52 |
|
public function __construct( |
28
|
|
|
DateTimeBindingInterface $dateTime, |
29
|
|
|
LogicBindingInterface $logic, |
30
|
|
|
NumericBindingInterface $numeric, |
31
|
|
|
RawBindingInterface $raw, |
32
|
|
|
StringBindingInterface $string |
33
|
|
|
) { |
34
|
52 |
|
$this->dateTime = $dateTime; |
35
|
52 |
|
$this->logic = $logic; |
36
|
52 |
|
$this->numeric = $numeric; |
37
|
52 |
|
$this->raw = $raw; |
38
|
52 |
|
$this->string = $string; |
39
|
52 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Bind a boolean value as bool or null. |
43
|
|
|
* |
44
|
|
|
* @param int|bool|null $value |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
* @throws TypeError |
48
|
|
|
*/ |
49
|
1 |
|
public function bBoolNullable($value = null): array |
50
|
|
|
{ |
51
|
1 |
|
return $this->logic->bBoolNullable($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Bind a boolean value as bool. |
56
|
|
|
* |
57
|
|
|
* @param int|bool|null $value |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
* @throws TypeError |
61
|
|
|
*/ |
62
|
3 |
|
public function bBool($value): array |
63
|
|
|
{ |
64
|
3 |
|
return $this->logic->bBool($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Bind a boolean value as int or null. |
69
|
|
|
* |
70
|
|
|
* @param int|bool|null $value |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
* @throws TypeError |
74
|
|
|
*/ |
75
|
1 |
|
public function bBoolIntNullable($value = null): array |
76
|
|
|
{ |
77
|
1 |
|
return $this->logic->bBoolIntNullable($value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Bind a boolean value as int. |
82
|
|
|
* |
83
|
|
|
* @param int|bool|null $value |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
* @throws TypeError |
87
|
|
|
*/ |
88
|
3 |
|
public function bBoolInt($value): array |
89
|
|
|
{ |
90
|
3 |
|
return $this->logic->bBoolInt($value); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Bind a date value as date or null. |
95
|
|
|
* YYYY-MM-DD is the proper date format. |
96
|
|
|
* |
97
|
|
|
* @param string|null $value |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
* @throws TypeError |
101
|
|
|
*/ |
102
|
1 |
|
public function bDateNullable(?string $value = null): array |
103
|
|
|
{ |
104
|
1 |
|
return $this->dateTime->bDateNullable($value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Bind a date value as date. |
109
|
|
|
* YYYY-MM-DD is the proper date format. |
110
|
|
|
* |
111
|
|
|
* @param string $value |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
* @throws TypeError |
115
|
|
|
*/ |
116
|
13 |
|
public function bDate(string $value): array |
117
|
|
|
{ |
118
|
13 |
|
return $this->dateTime->bDate($value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Bind a date time value as date time or null. |
123
|
|
|
* YYYY-MM-DD HH:MM:SS is the proper date format. |
124
|
|
|
* |
125
|
|
|
* @param string|null $value |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
* @throws TypeError |
129
|
|
|
*/ |
130
|
1 |
|
public function bDateTimeNullable(?string $value = null): array |
131
|
|
|
{ |
132
|
1 |
|
return $this->dateTime->bDateTimeNullable($value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Bind a date value as date time. |
137
|
|
|
* YYYY-MM-DD HH:MM:SS is the proper date format. |
138
|
|
|
* |
139
|
|
|
* @param string $value |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
* @throws TypeError |
143
|
|
|
*/ |
144
|
13 |
|
public function bDateTime(string $value): array |
145
|
|
|
{ |
146
|
13 |
|
return $this->dateTime->bDateTime($value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Bind a float or null. |
151
|
|
|
* |
152
|
|
|
* @param string|int|float|null $value |
153
|
|
|
* @param int $decimals |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
* @throws TypeError |
157
|
|
|
*/ |
158
|
1 |
|
public function bFloatNullable($value = null, $decimals = 3): array |
159
|
|
|
{ |
160
|
1 |
|
return $this->numeric->bFloatNullable($value, $decimals); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Bind a float. |
165
|
|
|
* |
166
|
|
|
* @param string|int|float $value |
167
|
|
|
* @param int $decimals |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
* @throws TypeError |
171
|
|
|
*/ |
172
|
15 |
|
public function bFloat($value, $decimals = 3): array |
173
|
|
|
{ |
174
|
15 |
|
return $this->numeric->bFloat($value, $decimals); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Bind an integer or null. |
179
|
|
|
* |
180
|
|
|
* @param string|int|float|bool|null $value |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* @throws TypeError |
184
|
|
|
*/ |
185
|
1 |
|
public function bIntNullable($value = null): array |
186
|
|
|
{ |
187
|
1 |
|
return $this->numeric->bIntNullable($value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Bind an integer. |
192
|
|
|
* |
193
|
|
|
* @param string|int|float|bool $value |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
* @throws TypeError |
197
|
|
|
*/ |
198
|
3 |
|
public function bInt($value): array |
199
|
|
|
{ |
200
|
3 |
|
return $this->numeric->bInt($value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Convert array of integers to comma separated values. Uses %% |
205
|
|
|
* Great for IN() statements. |
206
|
|
|
* |
207
|
|
|
* @param array $data |
208
|
|
|
* @param int $default |
209
|
|
|
* |
210
|
|
|
* @return array |
211
|
|
|
* @throws TypeError |
212
|
|
|
*/ |
213
|
2 |
|
public function bIntArray(array $data, int $default = 0): array |
214
|
|
|
{ |
215
|
2 |
|
return $this->numeric->bIntArray($data, $default); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Bind JSON to string or null. |
220
|
|
|
* |
221
|
|
|
* @param string|object|null $value |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
* @throws JsonException |
225
|
|
|
* @throws TypeError |
226
|
|
|
*/ |
227
|
1 |
|
public function bJsonNullable($value): array |
228
|
|
|
{ |
229
|
1 |
|
return $this->string->bJsonNullable($value); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Bind JSON to string. |
235
|
|
|
* |
236
|
|
|
* @param string|object|null $value |
237
|
|
|
* |
238
|
|
|
* @return array |
239
|
|
|
* @throws JsonException |
240
|
|
|
* @throws TypeError |
241
|
|
|
*/ |
242
|
4 |
|
public function bJson($value): array |
243
|
|
|
{ |
244
|
4 |
|
return $this->string->bJson($value); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Create and bind string for LIKE() statements. |
249
|
|
|
* |
250
|
|
|
* @param string $value |
251
|
|
|
* @param bool $ends Ends with? |
252
|
|
|
* @param bool $starts Starts with? |
253
|
|
|
* |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
4 |
|
public function bLike(string $value, bool $ends = false, bool $starts = false): array |
257
|
|
|
{ |
258
|
4 |
|
return $this->string->bLike($value, $ends, $starts); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* !!!DANGER!!! |
263
|
|
|
* Bind a raw value. |
264
|
|
|
* |
265
|
|
|
* @param string|int|float|bool $value |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
17 |
|
public function bRaw($value): array |
270
|
|
|
{ |
271
|
17 |
|
return $this->raw->bRaw($value); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Bind a string or null. |
276
|
|
|
* |
277
|
|
|
* @param string|int|float|bool|null $value |
278
|
|
|
* @param int $type |
279
|
|
|
* |
280
|
|
|
* @return array |
281
|
|
|
* @throws Exception |
282
|
|
|
*/ |
283
|
|
|
public function bStrNullable($value, int $type = PDO::PARAM_STR): array |
284
|
|
|
{ |
285
|
|
|
return $this->string->bStrNullable($value, $type); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Bind a string. |
290
|
|
|
* |
291
|
|
|
* @param string|int|float|bool|null $value |
292
|
|
|
* @param int $type |
293
|
|
|
* |
294
|
|
|
* @return array |
295
|
|
|
* @throws Exception |
296
|
|
|
*/ |
297
|
24 |
|
public function bStr($value, int $type = PDO::PARAM_STR): array |
298
|
|
|
{ |
299
|
24 |
|
return $this->string->bStr($value, $type); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Convert an array into a string and bind it. |
304
|
|
|
* Great for IN() statements. |
305
|
|
|
* |
306
|
|
|
* @param array $values |
307
|
|
|
* @param string|int|float|bool $default |
308
|
|
|
* |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
1 |
|
public function bStrArr(array $values, $default = ''): array |
312
|
|
|
{ |
313
|
1 |
|
return $this->string->bStrArr($values, $default); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|