Test Failed
Push — develop ( aa34ee...bf5ed8 )
by Kenneth
02:49
created

MySQLStringBindings::bJsonNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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