1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeekLab\GLPDO2\Bindings\MySQL; |
4
|
|
|
|
5
|
|
|
use \PDO; |
6
|
|
|
use \InvalidArgumentException; |
7
|
|
|
use \TypeError; |
8
|
|
|
use GeekLab\GLPDO2\Bindings\NumericBindingInterface; |
9
|
|
|
|
10
|
|
|
class MySQLNumericBindings implements NumericBindingInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Bind a float or null |
14
|
|
|
* |
15
|
|
|
* @param string|int|float|null $value |
16
|
|
|
* @param int $decimals |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
* @throws TypeError |
20
|
|
|
*/ |
21
|
2 |
|
public function bFloatNullable($value = null, $decimals = 3): array |
22
|
|
|
{ |
23
|
|
|
// Use NULL? |
24
|
2 |
|
if ($value === null) { |
25
|
1 |
|
return ['NULL']; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
return $this->bFloat($value, $decimals); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Bind a float. |
33
|
|
|
* |
34
|
|
|
* @param string|int|float|null $value |
35
|
|
|
* @param int $decimals |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
* @throws TypeError |
39
|
|
|
*/ |
40
|
16 |
|
public function bFloat($value, $decimals = 3): array |
41
|
|
|
{ |
42
|
16 |
|
if ($value === null) { |
43
|
1 |
|
throw new TypeError('Can not bind NULL in float spot.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
if (!is_numeric($value)) { |
47
|
1 |
|
throw new TypeError('Can not bind "' . $value . '" in float spot.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
$format = sprintf('%%0.%df', $decimals); |
51
|
|
|
|
52
|
|
|
// Apparently using PDO::PARAM_STR makes this fail! |
53
|
14 |
|
return [sprintf($format, $value)]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Bind an integer or null. |
58
|
|
|
* |
59
|
|
|
* @param string|int|float|bool|null $value |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
* @throws TypeError |
63
|
|
|
*/ |
64
|
1 |
|
public function bIntNullable($value = null): array |
65
|
|
|
{ |
66
|
|
|
// Use NULL? |
67
|
1 |
|
if ($value === null) { |
68
|
1 |
|
return [null, PDO::PARAM_NULL]; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->bInt($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Bind an integer. |
76
|
|
|
* |
77
|
|
|
* @param string|int|float|bool|null $value |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
* @throws InvalidArgumentException |
81
|
|
|
*/ |
82
|
4 |
|
public function bInt($value): array |
83
|
|
|
{ |
84
|
4 |
|
if ($value === null) { |
85
|
1 |
|
throw new TypeError('Can not bind NULL in integer spot.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
if (!is_numeric($value)) { |
89
|
1 |
|
throw new TypeError('Can not bind "' . $value . '" in integer spot.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return [(int) sprintf('%u', $value), PDO::PARAM_INT]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Convert array of integers to comma separated values. Uses %% |
97
|
|
|
* Great for IN() statements. |
98
|
|
|
* |
99
|
|
|
* @param array $data |
100
|
|
|
* @param int $default |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
* @throws TypeError |
104
|
|
|
*/ |
105
|
2 |
|
public function bIntArray(array $data, int $default = 0): array |
106
|
|
|
{ |
107
|
2 |
|
if (empty($data)) { |
108
|
1 |
|
throw new TypeError('Can not bind an empty array.'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Make unique integer array |
112
|
1 |
|
$numbers = []; |
113
|
|
|
|
114
|
1 |
|
foreach ($data as $value) { |
115
|
1 |
|
$numbers[(int) $value] = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$numbers = array_keys($numbers); |
119
|
|
|
|
120
|
|
|
// turn into a string |
121
|
1 |
|
$result = implode(', ', $numbers); |
122
|
|
|
|
123
|
1 |
|
return [$result ?: $default]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|