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