Passed
Push — develop ( 4cf285...1f9eb4 )
by Kenneth
02:25
created

MySQLStringBindings::bJsonNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings\MySQL;
4
5
use \JsonException;
6
use \PDO;
7
use \stdClass;
8
use \TypeError;
9
use GeekLab\GLPDO2\Bindings\StringBindingInterface;
10
11
class MySQLStringBindings implements StringBindingInterface
12
{
13
    /**
14
     * Bind JSON to string or null.
15
     *
16
     * @param object|string|null $value
17
     *
18
     * @return array
19
     * @throws JsonException
20
     * @throws TypeError
21
     */
22 1
    public function bJsonNullable($value): array
23
    {
24
        // Use NULL?
25 1
        if ($value === null) {
26 1
            return $this->bStrNullable(null);
27
        }
28
29
        return $this->bJSON($value);
30
    }
31
32
    /**
33
     * Bind JSON to string.
34
     *
35
     * @param object|string|null $value
36
     *
37
     * @return array
38
     * @throws JsonException
39
     * @throws TypeError
40
     */
41 4
    public function bJson($value): array
42
    {
43 4
        if ($value === null) {
44 1
            throw new TypeError('Can not bind NULL in JSON spot.');
45
        }
46
47 3
        if (is_object($value)) {
48 1
            $json = json_encode($value);
49
50 1
            if (json_last_error()) {
51
                throw new JsonException(json_last_error_msg());
52
            }
53
54 1
            return [$json, PDO::PARAM_STR];
55
        }
56
57 3
        if (is_string($value)) {
0 ignored issues
show
introduced by
The condition is_string($value) is always true.
Loading history...
58
            /** @var stdClass $JSON */
59 2
            $json = json_decode($value, false, 255);
60
61 2
            if (json_last_error()) {
62 1
                throw new JsonException(json_last_error_msg());
63
            }
64
65 1
            $json = json_encode($json);
66
67 1
            return [$json, PDO::PARAM_STR];
68
        }
69
70 1
        throw new TypeError('Can not bind ' . gettype($value) . ': ( ' . $value . ') in JSON spot.');
71
    }
72
73
    /**
74
     * Create and bind string for LIKE() statements.
75
     *
76
     * @param string $value
77
     * @param bool $ends Ends with?
78
     * @param bool $starts Starts with?
79
     *
80
     * @return array
81
     */
82 4
    public function bLike(string $value, bool $ends = false, bool $starts = false): array
83
    {
84 4
        $arr = ['%', $value, '%'];
85
86 4
        if ($starts) {
87
            // Starts with.
88 2
            array_shift($arr);
89
        }
90
91 4
        if ($ends) {
92
            // Ends with.
93 2
            array_pop($arr);
94
        }
95
96 4
        return [implode('', $arr)];
97
    }
98
99
    /**
100
     * Bind a string value or null
101
     *
102
     * @param string|int|float|null $value
103
     *
104
     * @return array
105
     * @throws TypeError
106
     */
107 3
    public function bStrNullable($value): array
108
    {
109 3
        if ($value === null) {
110 2
            return [null, PDO::PARAM_NULL];
111
        }
112
113 1
        return $this->bStr($value);
114
    }
115
116
    /**
117
     * Bind a string.
118
     *
119
     * @param string|int|float|null $value
120
     *
121
     * @return array
122
     * @throws TypeError
123
     */
124 24
    public function bStr($value): array
125
    {
126 24
        if ($value === null) {
127 1
            throw new TypeError('Can not bind NULL in string spot.');
128
        }
129
130 23
        return [(string) $value, PDO::PARAM_STR];
131
    }
132
133
    /**
134
     * Convert an array into a string and bind it.
135
     * Great for IN() statements.
136
     *
137
     * @param array $values
138
     * @param string|int|float|bool $default
139
     *
140
     * @return array
141
     */
142 1
    public function bStrArr(array $values, $default = ''): array
143
    {
144 1
        return [empty($values) ? $default : '\'' . implode("', '", $values) . '\''];
145
    }
146
}
147