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 OtherBindingInterface $other */ |
22
|
|
|
protected $other; |
23
|
|
|
|
24
|
|
|
/** @var StringBindingInterface $string */ |
25
|
|
|
protected $string; |
26
|
|
|
|
27
|
52 |
|
public function __construct( |
28
|
|
|
DateTimeBindingInterface $dateTime, |
29
|
|
|
LogicBindingInterface $logic, |
30
|
|
|
NumericBindingInterface $numeric, |
31
|
|
|
OtherBindingInterface $other, |
32
|
|
|
StringBindingInterface $string |
33
|
|
|
) { |
34
|
52 |
|
$this->dateTime = $dateTime; |
35
|
52 |
|
$this->logic = $logic; |
36
|
52 |
|
$this->numeric = $numeric; |
37
|
52 |
|
$this->other = $other; |
38
|
52 |
|
$this->string = $string; |
39
|
52 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Bind a boolean value as bool or optional null. |
43
|
|
|
* |
44
|
|
|
* @param int|bool|null $value |
45
|
|
|
* @param array $options ['nullable' => (bool)] |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
* @throws TypeError |
49
|
1 |
|
*/ |
50
|
|
|
public function bBool($value, array $options = []): array |
51
|
1 |
|
{ |
52
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
53
|
|
|
return $this->logic->bBoolNullable($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->logic->bBool($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Bind a boolean value as int or optional null. |
61
|
|
|
* |
62
|
3 |
|
* @param int|bool|null $value |
63
|
|
|
* @param array $options ['nullable' => (bool)] |
64
|
3 |
|
* @return array |
65
|
|
|
* @throws TypeError |
66
|
|
|
*/ |
67
|
|
|
public function bBoolInt($value, array $options = []): array |
68
|
|
|
{ |
69
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
70
|
|
|
return $this->logic->bBoolIntNullable($value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->logic->bBoolInt($value); |
74
|
|
|
} |
75
|
1 |
|
|
76
|
|
|
/** |
77
|
1 |
|
* Bind a date value as date or optional null. |
78
|
|
|
* YYYY-MM-DD is the proper date format. |
79
|
|
|
* |
80
|
|
|
* @param string|null $value |
81
|
|
|
* @param array $options ['nullable' => (bool)] |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* @throws TypeError |
85
|
|
|
*/ |
86
|
|
|
public function bDate(?string $value, array $options = []): array |
87
|
|
|
{ |
88
|
3 |
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
89
|
|
|
return $this->dateTime->bDateNullable($value); |
90
|
3 |
|
} |
91
|
|
|
|
92
|
|
|
return $this->dateTime->bDate($value); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Bind a date value as date time or optional null. |
97
|
|
|
* YYYY-MM-DD HH:MM:SS is the proper date format. |
98
|
|
|
* |
99
|
|
|
* @param string|null $value |
100
|
|
|
* @param array $options ['nullable' => (bool)] |
101
|
|
|
* |
102
|
1 |
|
* @return array |
103
|
|
|
* @throws TypeError |
104
|
1 |
|
*/ |
105
|
|
|
public function bDateTime(?string $value, array $options = []): array |
106
|
|
|
{ |
107
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
108
|
|
|
return $this->dateTime->bDateTimeNullable($value); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->dateTime->bDateTime($value); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Bind a float value as float or optional null. |
116
|
13 |
|
* |
117
|
|
|
* @param string|int|float|null $value |
118
|
13 |
|
* @param array $options ['decimals' => (int), 'nullable' => (bool)] |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
* @throws TypeError |
122
|
|
|
*/ |
123
|
|
|
public function bFloat($value, array $options = ['decimals' => 2]): array |
124
|
|
|
{ |
125
|
|
|
if (!isset($options['decimals'])) { |
126
|
|
|
$options['decimals'] = 2; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
130
|
1 |
|
return $this->numeric->bFloatNullable($value, $options['decimals']); |
131
|
|
|
} |
132
|
1 |
|
|
133
|
|
|
return $this->numeric->bFloat($value, $options['decimals']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Bind an integer value as int or optional null. |
138
|
|
|
* |
139
|
|
|
* @param string|int|float|bool|null $value |
140
|
|
|
* @param array $options ['nullable' => (bool)] |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
* @throws TypeError |
144
|
13 |
|
*/ |
145
|
|
|
public function bInt($value, array $options = []): array |
146
|
13 |
|
{ |
147
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
148
|
|
|
return $this->numeric->bIntNullable($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->numeric->bInt($value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Convert array of integers to comma separated values. Uses %% |
156
|
|
|
* Great for IN() statements. |
157
|
|
|
* |
158
|
1 |
|
* @param array $data |
159
|
|
|
* @param int $default |
160
|
1 |
|
* |
161
|
|
|
* @return array |
162
|
|
|
* @throws TypeError |
163
|
|
|
*/ |
164
|
|
|
public function bIntArray(array $data, int $default = 0): array |
165
|
|
|
{ |
166
|
|
|
return $this->numeric->bIntArray($data, $default); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Bind JSON to string or optional null. |
171
|
|
|
* |
172
|
15 |
|
* @param string|object|null $value |
173
|
|
|
* @param array $options ['nullable' => (bool)] |
174
|
15 |
|
* |
175
|
|
|
* @return array |
176
|
|
|
* @throws JsonException |
177
|
|
|
* @throws TypeError |
178
|
|
|
*/ |
179
|
|
|
public function bJson($value, array $options = []): array |
180
|
|
|
{ |
181
|
|
|
if (isset($options['nullable']) && $options['nullable'] === \true) { |
182
|
|
|
return $this->string->bJsonNullable($value); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $this->string->bJson($value); |
186
|
|
|
} |
187
|
1 |
|
|
188
|
|
|
/** |
189
|
|
|
* Create and bind string for LIKE() statements. |
190
|
|
|
* |
191
|
|
|
* @param string $value |
192
|
|
|
* @param bool $ends Ends with? |
193
|
|
|
* @param bool $starts Starts with? |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function bLike(string $value, bool $ends = false, bool $starts = false): array |
198
|
3 |
|
{ |
199
|
|
|
return $this->string->bLike($value, $ends, $starts); |
200
|
3 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* !!!DANGER!!! |
204
|
|
|
* Bind a raw value. |
205
|
|
|
* |
206
|
|
|
* @param string|int|float|bool $value |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function bRaw($value): array |
211
|
|
|
{ |
212
|
|
|
return $this->other->bRaw($value); |
213
|
2 |
|
} |
214
|
|
|
|
215
|
2 |
|
/** |
216
|
|
|
* Bind a string value as string or optional null. |
217
|
|
|
* |
218
|
|
|
* @param string|int|float|bool|null $value |
219
|
|
|
* @param array $options ['nullable' => (bool)] |
220
|
|
|
* @return array |
221
|
|
|
* @throws Exception |
222
|
|
|
*/ |
223
|
|
|
public function bStr($value, array $options = []): array |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
return $this->string->bStr($value); |
226
|
|
|
} |
227
|
1 |
|
|
228
|
|
|
/** |
229
|
1 |
|
* Convert an array into a string and bind it. |
230
|
|
|
* Great for IN() statements. |
231
|
|
|
* |
232
|
|
|
* @param array $values |
233
|
|
|
* @param string|int|float|bool $default |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function bStrArr(array $values, $default = ''): array |
238
|
|
|
{ |
239
|
|
|
return $this->string->bStrArr($values, $default); |
240
|
|
|
} |
241
|
|
|
|
242
|
4 |
|
/** |
243
|
|
|
* Bind a string to the PDO data type. |
244
|
4 |
|
* |
245
|
|
|
* @param string|int|float|bool|null $value |
246
|
|
|
* @param int $type |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function bValueType($value, int $type = \PDO::PARAM_STR): array |
251
|
|
|
{ |
252
|
|
|
return $this->other->bValueType($value, $type); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|