Passed
Push — develop ( a41828...be09b7 )
by Kenneth
02:35
created

MySQLStringBindings::bLike()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 3
rs 10
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 1
    public function bJsonNullable($value): array
22
    {
23
        // Use NULL?
24 1
        if ($value === null) {
25 1
            return $this->bStrNullable(null);
26
        }
27
28
        return $this->bJSON($value);
29
    }
30
31
    /**
32
     * Bind JSON to string.
33
     *
34
     * @param object|string|null $value
35
     *
36
     * @return array
37
     * @throws JsonException
38
     * @throws TypeError
39
     */
40 4
    public function bJson($value): array
41
    {
42 4
        if ($value === null) {
43 1
            throw new TypeError('Can not bind NULL in JSON spot.');
44
        }
45
46 3
        if (is_object($value)) {
47 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...
48
49 1
            if (json_last_error()) {
50
                throw new JsonException(json_last_error_msg());
51
            }
52
53 1
            return $this->bStr($json);
54
        }
55
56 3
        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 2
            $json = json_decode($value, false, 255);
59
60 2
            if (json_last_error()) {
61 1
                throw new JsonException(json_last_error_msg());
62
            }
63
64 1
            $json = json_encode($json);
65
66 1
            return $this->bStr($json);
67
        }
68
69 1
        throw new TypeError('Can not bind ' . gettype($value) . ': ( ' . $value . ') in JSON spot.');
70
    }
71
72
    /**
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 4
    public function bLike(string $value, bool $ends = false, bool $starts = false): array
82
    {
83 4
        $arr = ['%', $value, '%'];
84
85 4
        if ($starts) {
86
            // Starts with.
87 2
            array_shift($arr);
88
        }
89
90 4
        if ($ends) {
91
            // Ends with.
92 2
            array_pop($arr);
93
        }
94
95 4
        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
     * @throws TypeError
106
     */
107 1
    public function bStrNullable($value, int $type = PDO::PARAM_STR): array
108
    {
109 1
        if ($value === null) {
110 1
            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 24
    public function bStr($value, int $type = PDO::PARAM_STR): array
127
    {
128 24
        if ($value === null) {
129 2
            throw new TypeError('Can not bind NULL in string spot.');
130
        }
131
132 23
        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 1
    public function bStrArr(array $values, $default = ''): array
145
    {
146 1
        return [empty($values) ? $default : '\'' . implode("', '", $values) . '\''];
147
    }
148
}
149