Passed
Push — master ( bd6e6f...61c111 )
by Kenneth
01:39
created

MySQLStringBindings   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bStrArr() 0 3 2
A bStr() 0 9 5
B bJSON() 0 26 8
A bLike() 0 15 3
1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings\MySQL;
4
5
use \Exception;
6
use \JsonException;
7
use \DomainException;
8
9
use GeekLab\GLPDO2\Bindings\StringBindingInterface;
10
use PDO;
11
12
class MySQLStringBindings implements StringBindingInterface
13
{
14
15
    /**
16
     * @param object|string|null $value
17
     * @param bool $null
18
     *
19
     * @return array
20
     * @throws Exception
21
     */
22
    public function bJSON($value, bool $null = false): array
23
    {
24
        // Use NULL?
25
        if ($value === null && $null) {
26
            return $this->bStr(null, true);
27
        }
28
29
        if ($value === null && !$null) {
30
            throw new JsonException('Can not bind NULL in JSON spot.');
31
        }
32
33
        if (is_object($value)) {
34
            $value = json_encode($value);
35
        } elseif (is_string($value)) {
36
            $JSON = json_decode($value, false, 255);
37
38
            if (json_last_error()) {
39
                throw new JsonException('Can not bind invalid JSON in JSON spot. (' . json_last_error_msg() . ')');
40
            }
41
42
            $value = json_encode($JSON);
43
        } else {
44
            throw new JsonException('Can not bind invalid JSON in JSON spot. (' . $value . ')');
45
        }
46
47
        return $this->bStr($value);
48
    }
49
50
    /**
51
     * Create and bind string for LIKE() statements.
52
     *
53
     * @param string $value
54
     * @param bool $ends Ends with?
55
     * @param bool $starts Starts with?
56
     *
57
     * @return array
58
     */
59
    public function bLike(string $value, bool $ends = false, bool $starts = false): array
60
    {
61
        $arr = ['%', $value, '%'];
62
63
        if ($starts) {
64
            // Starts with.
65
            array_shift($arr);
66
        }
67
68
        if ($ends) {
69
            // Ends with.
70
            array_pop($arr);
71
        }
72
73
        return [implode('', $arr)];
74
    }
75
76
    /**
77
     * Bind a string value.
78
     *
79
     * @param string|int|float|bool|null $value
80
     * @param bool $null
81
     * @param int $type
82
     *
83
     * @return array
84
     * @throws Exception
85
     */
86
    public function bStr($value, bool $null = false, int $type = PDO::PARAM_STR): array
87
    {
88
        if ($value === null && $null) {
89
            $type = PDO::PARAM_NULL;
90
        } elseif ($value === null && !$null) {
91
            throw new DomainException('Can not bind NULL in string spot.');
92
        }
93
94
        return [(string) $value, $type];
95
    }
96
97
    /**
98
     * Convert an array into a string and bind it.
99
     * Great for IN() statements.
100
     *
101
     * @param array $values
102
     * @param string|int|float|bool $default
103
     *
104
     * @return array
105
     */
106
    public function bStrArr(array $values, $default = ''): array
107
    {
108
        return [empty($values) ? $default : '\'' . implode("', '", $values) . '\''];
109
    }
110
}
111