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