Passed
Push — develop ( 44a7ff...abefc0 )
by Kenneth
02:20
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 = $value = json_encode($value);
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
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 2
    public function bStrNullable($value): array
108
    {
109 2
        if ($value === null) {
110 2
            return [null, PDO::PARAM_NULL];
111
        }
112
113
        return $this->bStr($value);
114
    }
115
116
117
    /**
118
     * Bind a string.
119
     *
120
     * @param string|int|float|null $value
121
     *
122
     * @return array
123
     * @throws TypeError
124
     */
125 24
    public function bStr($value): array
126
    {
127 24
        if ($value === null) {
128 1
            throw new TypeError('Can not bind NULL in string spot.');
129
        }
130
131 23
        return [(string) $value, PDO::PARAM_STR];
132
    }
133
134
    /**
135
     * Convert an array into a string and bind it.
136
     * Great for IN() statements.
137
     *
138
     * @param array $values
139
     * @param string|int|float|bool $default
140
     *
141
     * @return array
142
     */
143 1
    public function bStrArr(array $values, $default = ''): array
144
    {
145 1
        return [empty($values) ? $default : '\'' . implode("', '", $values) . '\''];
146
    }
147
}
148