MySQLNumericBindings::bFloat()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings\MySQL;
4
5
use PDO;
6
use InvalidArgumentException;
7
use GeekLab\GLPDO2\Bindings\NumericBindingInterface;
8
9
class MySQLNumericBindings implements NumericBindingInterface
10
{
11
    /**
12
     * Bind a float.
13
     *
14
     * @param float | int | string | null $value
15
     * @param int                         $decimals
16
     * @param bool                        $null
17
     *
18
     * @return array{?string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{?string} at position 2 could not be parsed: Expected ':' at position 2, but found '?string'.
Loading history...
19
     * @throws InvalidArgumentException
20
     */
21 15
    public function bFloat(float | int | string | null $value = null, int $decimals = 3, bool $null = false): array
22
    {
23
        // Use NULL?
24 15
        if ($value === null) {
25 2
            if ($null) {
26 1
                return ['NULL'];
27
            }
28
29 1
            throw new InvalidArgumentException('Can not bind NULL in float spot.');
30
        }
31
32 14
        if (!is_numeric($value)) {
33 1
            throw new InvalidArgumentException('Can not bind "' . $value . '" in float spot.');
34
        }
35
36 13
        $format = sprintf('%%0.%df', $decimals);
37
38
        // Apparently using PDO::PARAM_STR makes this fail!
39 13
        return [sprintf($format, $value)];
40
    }
41
42
    /**
43
     * Bind an integer with optional NULL.
44
     *
45
     * @param float | bool | int | string | null $value
46
     * @param bool                               $null
47
     *
48
     * @return array{?int, int}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{?int, int} at position 2 could not be parsed: Expected ':' at position 2, but found '?int'.
Loading history...
49
     * @throws InvalidArgumentException
50
     */
51 3
    public function bInt(float | bool | int | string | null $value = null, bool $null = false): array
52
    {
53
        // Use NULL?
54 3
        if ($value === null) {
55 2
            if ($null) {
56 1
                return [null, PDO::PARAM_NULL];
57
            }
58
59 1
            throw new InvalidArgumentException('Can not bind NULL in integer spot.');
60
        }
61
62 2
        if (!is_numeric($value)) {
63 1
            throw new InvalidArgumentException('Can not bind "' . $value . '" in integer spot.');
64
        }
65
66 1
        return [(int)sprintf('%u', $value), PDO::PARAM_INT];
67
    }
68
69
    /**
70
     * Convert array of integers to comma separated values. Uses %%
71
     * Great for IN() statements.
72
     *
73
     * @param array{} | array{mixed} $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{} | array{mixed} at position 6 could not be parsed: Expected ':' at position 6, but found 'mixed'.
Loading history...
74
     *
75
     * @return array{string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
76
     * @throws InvalidArgumentException
77
     */
78 2
    public function bIntArray(array $data): array
79
    {
80 2
        if (empty($data)) {
81 1
            throw new InvalidArgumentException('Can not bind an empty array.');
82
        }
83
84
        // Make unique integer array
85 1
        $numbers = [];
86
87 1
        foreach ($data as $value) {
88 1
            $numbers[(int)$value] = true;
89
        }
90
91 1
        $numbers = array_keys($numbers);
92
93
        // Turn into a comma delimited string.
94 1
        return [implode(', ', $numbers)];
95
    }
96
}
97