1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeekLab\GLPDO2\Bindings; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Exception; |
7
|
|
|
use JsonException; |
8
|
|
|
|
9
|
|
|
class Bindings |
10
|
|
|
{ |
11
|
|
|
protected DateTimeBindingInterface $dateTime; |
12
|
|
|
protected LogicBindingInterface $logic; |
13
|
|
|
protected NumericBindingInterface $numeric; |
14
|
|
|
protected RawBindingInterface $raw; |
15
|
|
|
protected StringBindingInterface $string; |
16
|
|
|
|
17
|
50 |
|
public function __construct( |
18
|
|
|
DateTimeBindingInterface $dateTime, |
19
|
|
|
LogicBindingInterface $logic, |
20
|
|
|
NumericBindingInterface $numeric, |
21
|
|
|
RawBindingInterface $raw, |
22
|
|
|
StringBindingInterface $string |
23
|
|
|
) { |
24
|
50 |
|
$this->dateTime = $dateTime; |
25
|
50 |
|
$this->logic = $logic; |
26
|
50 |
|
$this->numeric = $numeric; |
27
|
50 |
|
$this->raw = $raw; |
28
|
50 |
|
$this->string = $string; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Bind a boolean value as bool, with NULL option. |
33
|
|
|
* |
34
|
|
|
* @param bool | int | null $value |
35
|
|
|
* @param bool $null |
36
|
|
|
* |
37
|
|
|
* @return array{?bool, int} |
|
|
|
|
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
4 |
|
public function bBool(bool | int | null $value = null, bool $null = false): array |
41
|
|
|
{ |
42
|
4 |
|
return $this->logic->bBool($value, $null); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Bind a boolean value as int, with NULL option. |
47
|
|
|
* |
48
|
|
|
* @param bool | int | null $value |
49
|
|
|
* @param bool $null |
50
|
|
|
* |
51
|
|
|
* @return array{?int, int} |
|
|
|
|
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
4 |
|
public function bBoolInt(bool | int | null $value = null, bool $null = false): array |
55
|
|
|
{ |
56
|
4 |
|
return $this->logic->bBoolInt($value, $null); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Bind a date value as date or optional NULL. |
61
|
|
|
* YYYY-MM-DD is the proper date format. |
62
|
|
|
* |
63
|
|
|
* @param string | null $value |
64
|
|
|
* @param bool $null |
65
|
|
|
* |
66
|
|
|
* @return array{?string, int} |
|
|
|
|
67
|
|
|
* @throws Exception |
68
|
|
|
*/ |
69
|
16 |
|
public function bDate(?string $value, bool $null = false): array |
70
|
|
|
{ |
71
|
16 |
|
return $this->dateTime->bDate($value, $null); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Bind a date value as date time or optional NULL. |
76
|
|
|
* YYYY-MM-DD HH:MM:SS is the proper date format. |
77
|
|
|
* |
78
|
|
|
* @param string | null $value |
79
|
|
|
* @param bool $null |
80
|
|
|
* |
81
|
|
|
* @return array{?string, int} |
|
|
|
|
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
15 |
|
public function bDateTime(?string $value = null, bool $null = false): array |
85
|
|
|
{ |
86
|
15 |
|
return $this->dateTime->bDateTime($value, $null); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Bind a float. |
91
|
|
|
* |
92
|
|
|
* @param float | int | string | null $value |
93
|
|
|
* @param int $decimals |
94
|
|
|
* @param bool $null |
95
|
|
|
* |
96
|
|
|
* @return array{?string} |
|
|
|
|
97
|
|
|
* @throws Exception |
98
|
|
|
*/ |
99
|
15 |
|
public function bFloat(float | int | string | null $value = null, int $decimals = 3, bool $null = false): array |
100
|
|
|
{ |
101
|
15 |
|
return $this->numeric->bFloat($value, $decimals, $null); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Bind an integer with optional NULL. |
106
|
|
|
* |
107
|
|
|
* @param float | bool | int | string | null $value |
108
|
|
|
* @param bool $null |
109
|
|
|
* |
110
|
|
|
* @return array{?int, int} |
|
|
|
|
111
|
|
|
* @throws Exception |
112
|
|
|
*/ |
113
|
3 |
|
public function bInt(float | bool | int | string | null $value = null, bool $null = false): array |
114
|
|
|
{ |
115
|
3 |
|
return $this->numeric->bInt($value, $null); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Convert array of integers to comma separated values. Uses %% |
120
|
|
|
* Great for IN() statements. |
121
|
|
|
* |
122
|
|
|
* @param array{} | array{mixed} $data |
|
|
|
|
123
|
|
|
* |
124
|
|
|
* @return array{int | string} |
|
|
|
|
125
|
|
|
* @throws Exception |
126
|
|
|
*/ |
127
|
2 |
|
public function bIntArray(array $data): array |
128
|
|
|
{ |
129
|
2 |
|
return $this->numeric->bIntArray($data); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Bind an object or JSON string to a string |
134
|
|
|
* |
135
|
|
|
* @param mixed $value |
136
|
|
|
* @param bool $null |
137
|
|
|
* |
138
|
|
|
* @return array{string, int} |
|
|
|
|
139
|
|
|
* @throws JsonException |
140
|
|
|
*/ |
141
|
5 |
|
public function bJSON(mixed $value, bool $null = false): array |
142
|
|
|
{ |
143
|
5 |
|
return $this->string->bJSON($value, $null); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Create and bind string for LIKE() statements. |
148
|
|
|
* |
149
|
|
|
* @param string $value |
150
|
|
|
* @param bool $ends Ends with? |
151
|
|
|
* @param bool $starts Starts with? |
152
|
|
|
* |
153
|
|
|
* @return array{string} |
|
|
|
|
154
|
|
|
*/ |
155
|
4 |
|
public function bLike(string $value, bool $ends = false, bool $starts = false): array |
156
|
|
|
{ |
157
|
4 |
|
return $this->string->bLike($value, $ends, $starts); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* !!!DANGER!!! |
162
|
|
|
* Bind a raw value. |
163
|
|
|
* |
164
|
|
|
* @param float | bool | int | string $value |
165
|
|
|
* |
166
|
|
|
* @return array{float | bool | int | string} |
|
|
|
|
167
|
|
|
*/ |
168
|
17 |
|
public function bRaw(float | bool | int | string $value): array |
169
|
|
|
{ |
170
|
17 |
|
return $this->raw->bRaw($value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Bind a string value. |
175
|
|
|
* |
176
|
|
|
* @param float | bool | int | string | null $value |
177
|
|
|
* @param bool $null |
178
|
|
|
* @param int $type |
179
|
|
|
* |
180
|
|
|
* @return array{string, int} |
|
|
|
|
181
|
|
|
* @throws Exception |
182
|
|
|
*/ |
183
|
24 |
|
public function bStr( |
184
|
|
|
float | bool | int | string | null $value, |
185
|
|
|
bool $null = false, |
186
|
|
|
int $type = PDO::PARAM_STR |
187
|
|
|
): array { |
188
|
24 |
|
return $this->string->bStr($value, $null, $type); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Convert an array into a string and bind it. |
193
|
|
|
* Great for IN() statements. |
194
|
|
|
* |
195
|
|
|
* @param array{} | array{mixed} $values |
|
|
|
|
196
|
|
|
* @param float | bool | int | string $default |
197
|
|
|
* |
198
|
|
|
* @return array{float | bool | int | string} |
|
|
|
|
199
|
|
|
*/ |
200
|
1 |
|
public function bStrArr(array $values, float | bool | int | string $default = ''): array |
201
|
|
|
{ |
202
|
1 |
|
return $this->string->bStrArr($values, $default); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|