Test Failed
Push — develop ( eabf9f )
by Kenneth
03:54
created

MySQLNumericBindings::bIntNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
    public function bFloatNullable($value = null, $decimals = 3): array
22
    {
23
        // Use NULL?
24
        if ($value === null) {
25
            return ['NULL'];
26
        }
27
28
        return $this->bFloat($value, $decimals);
29
30
    }
31
32
    /**
33
     * Bind a float.
34
     *
35
     * @param string|int|float $value
36
     * @param int $decimals
37
     *
38
     * @return array
39
     * @throws TypeError
40
     */
41
    public function bFloat($value, $decimals = 3): array
42
    {
43
        if (!is_numeric($value)) {
44
            throw new TypeError('Can not bind "' . $value . '" in float spot.');
45
        }
46
47
        $format = sprintf('%%0.%df', $decimals);
48
49
        // Apparently using PDO::PARAM_STR makes this fail!
50
        return [sprintf($format, $value)];
51
    }
52
53
    /**
54
     * Bind an integer or null.
55
     *
56
     * @param string|int|float|bool|null $value
57
     *
58
     * @return array
59
     * @throws TypeError
60
     */
61
    public function bIntNullable($value = null): array
62
    {
63
        // Use NULL?
64
        if ($value === null) {
65
            return [null, PDO::PARAM_NULL];
66
        }
67
68
        return $this->bInt($value);
69
    }
70
    /**
71
     * Bind an integer.
72
     *
73
     * @param string|int|float|bool $value
74
     *
75
     * @return array
76
     * @throws InvalidArgumentException
77
     */
78
    public function bInt($value): array
79
    {
80
        if (!is_numeric($value)) {
81
            throw new TypeError('Can not bind "' . $value . '" in integer spot.');
82
        }
83
84
        return [(int) sprintf('%u', $value), PDO::PARAM_INT];
85
    }
86
87
    /**
88
     * Convert array of integers to comma separated values. Uses %%
89
     * Great for IN() statements.
90
     *
91
     * @param array $data
92
     * @param int $default
93
     *
94
     * @return array
95
     * @throws InvalidArgumentException
96
     */
97
    public function bIntArray(array $data, int $default = 0): array
98
    {
99
        if (empty($data)) {
100
            throw new InvalidArgumentException('Can not bind an empty array.');
101
        }
102
103
        // Make unique integer array
104
        $numbers = array();
105
106
        foreach ($data as $value) {
107
            $numbers[(int) $value] = true;
108
        }
109
110
        $numbers = array_keys($numbers);
111
112
        // turn into a string
113
        $result = implode(', ', $numbers);
114
115
        return [$result ?: $default];
116
    }
117
}
118