Passed
Push — develop ( abefc0...4cf285 )
by Kenneth
02:21
created

Bindings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings;
4
5
use \PDO;
6
use \Exception;
7
use \TypeError;
8
use \JsonException;
9
10
class Bindings
11
{
12
    /** @var DateTimeBindingInterface $dateTime */
13
    protected $dateTime;
14
15
    /** @var LogicBindingInterface $logic */
16
    protected $logic;
17
18
    /** @var NumericBindingInterface $numeric */
19
    protected $numeric;
20
21
    /** @var OtherBindingInterface $other */
22
    protected $other;
23
24
    /** @var StringBindingInterface $string */
25
    protected $string;
26
27 53
    public function __construct(
28
        DateTimeBindingInterface $dateTime,
29
        LogicBindingInterface $logic,
30
        NumericBindingInterface $numeric,
31
        OtherBindingInterface $other,
32
        StringBindingInterface $string
33
    ) {
34 53
        $this->dateTime = $dateTime;
35 53
        $this->logic = $logic;
36 53
        $this->numeric = $numeric;
37 53
        $this->other = $other;
38 53
        $this->string = $string;
39 53
    }
40
41
    /**
42
     * Bind a boolean value as bool or optional null.
43
     *
44
     * @param int|bool|null $value
45
     * @param array $options ['nullable' => (bool)]
46
     *
47
     * @return array
48
     * @throws TypeError
49
     */
50 4
    public function bBool($value, array $options = []): array
51
    {
52 4
        if (isset($options['nullable']) && $options['nullable'] === \true) {
53 1
            return $this->logic->bBoolNullable($value);
54
        }
55
56 3
        return $this->logic->bBool($value);
57
    }
58
59
    /**
60
     * Bind a boolean value as int or optional null.
61
     *
62
     * @param int|bool|null $value
63
     * @param array $options ['nullable' => (bool)]
64
     * @return array
65
     * @throws TypeError
66
     */
67 4
    public function bBoolInt($value, array $options = []): array
68
    {
69 4
        if (isset($options['nullable']) && $options['nullable'] === \true) {
70 1
            return $this->logic->bBoolIntNullable($value);
71
        }
72
73 3
        return $this->logic->bBoolInt($value);
74
    }
75
76
    /**
77
     * Bind a date value as date or optional null.
78
     * YYYY-MM-DD is the proper date format.
79
     *
80
     * @param string|null $value
81
     * @param array $options ['nullable' => (bool)]
82
     *
83
     * @return array
84
     * @throws TypeError
85
     */
86 16
    public function bDate(?string $value, array $options = []): array
87
    {
88 16
        if (isset($options['nullable']) && $options['nullable'] === \true) {
89 1
            return $this->dateTime->bDateNullable($value);
90
        }
91
92
        /** @var string $value */
93 15
        return $this->dateTime->bDate($value);
94
    }
95
96
    /**
97
     * Bind a date value as date time or optional null.
98
     * YYYY-MM-DD HH:MM:SS is the proper date format.
99
     *
100
     * @param string|null $value
101
     * @param array $options ['nullable' => (bool)]
102
     *
103
     * @return array
104
     * @throws TypeError
105
     */
106 15
    public function bDateTime(?string $value, array $options = []): array
107
    {
108 15
        if (isset($options['nullable']) && $options['nullable'] === \true) {
109 1
            return $this->dateTime->bDateTimeNullable($value);
110
        }
111
112
        /** @var string $value */
113 14
        return $this->dateTime->bDateTime($value);
114
    }
115
116
    /**
117
     * Bind a float value as float or optional null.
118
     *
119
     * @param string|int|float|null $value
120
     * @param array $options ['decimals' => (int), 'nullable' => (bool)]
121
     *
122
     * @return array
123
     * @throws TypeError
124
     */
125 16
    public function bFloat($value, array $options = ['decimals' => 2]): array
126
    {
127 16
        if (!isset($options['decimals'])) {
128 1
            $options['decimals'] = 2;
129
        }
130
131 16
        if (isset($options['nullable']) && $options['nullable'] === \true) {
132 1
            return $this->numeric->bFloatNullable($value, $options['decimals']);
133
        }
134
135
        /** @var float|int|string $value */
136 16
        return $this->numeric->bFloat($value, $options['decimals']);
137
    }
138
139
    /**
140
     * Bind an integer value as int or optional null.
141
     *
142
     * @param string|int|float|bool|null $value
143
     * @param array $options ['nullable' => (bool)]
144
     *
145
     * @return array
146
     * @throws TypeError
147
     */
148 4
    public function bInt($value, array $options = []): array
149
    {
150 4
        if (isset($options['nullable']) && $options['nullable'] === \true) {
151 1
            return $this->numeric->bIntNullable($value);
152
        }
153
154
        /** @var bool|float|int|string $value */
155 3
        return $this->numeric->bInt($value);
156
    }
157
158
    /**
159
     * Convert array of integers to comma separated values. Uses %%
160
     * Great for IN() statements.
161
     *
162
     * @param array $data
163
     * @param int $default
164
     *
165
     * @return array
166
     * @throws TypeError
167
     */
168 2
    public function bIntArray(array $data, int $default = 0): array
169
    {
170 2
        return $this->numeric->bIntArray($data, $default);
171
    }
172
173
    /**
174
     * Bind JSON to string or optional null.
175
     *
176
     * @param string|object|null $value
177
     * @param array $options ['nullable' => (bool)]
178
     *
179
     * @return array
180
     * @throws JsonException
181
     * @throws TypeError
182
     */
183 5
    public function bJson($value, array $options = []): array
184
    {
185 5
        if (isset($options['nullable']) && $options['nullable'] === \true) {
186 1
            return $this->string->bJsonNullable($value);
187
        }
188
189 4
        return $this->string->bJson($value);
190
    }
191
192
    /**
193
     * Create and bind string for LIKE() statements.
194
     *
195
     * @param string $value
196
     * @param bool $ends Ends with?
197
     * @param bool $starts Starts with?
198
     *
199
     * @return array
200
     */
201 4
    public function bLike(string $value, bool $ends = false, bool $starts = false): array
202
    {
203 4
        return $this->string->bLike($value, $ends, $starts);
204
    }
205
206
    /**
207
     * !!!DANGER!!!
208
     * Bind a raw value.
209
     *
210
     * @param string|int|float|bool $value
211
     *
212
     * @return array
213
     */
214 17
    public function bRaw($value): array
215
    {
216 17
        return $this->other->bRaw($value);
217
    }
218
219
    /**
220
     * Bind a string value as string or optional null.
221
     *
222
     * @param string|int|float|null $value
223
     * @param array $options ['nullable' => (bool)]
224
     * @return array
225
     * @throws Exception
226
     */
227 24
    public function bStr($value, array $options = []): array
228
    {
229 24
        if (isset($options['nullable']) && $options['nullable'] === \true) {
230 1
            return $this->string->bStrNullable($value);
231
        }
232
233
        /** @var string $value */
234 24
        return $this->string->bStr($value);
235
    }
236
237
    /**
238
     * Convert an array into a string and bind it.
239
     * Great for IN() statements.
240
     *
241
     * @param array $values
242
     * @param string|int|float|bool $default
243
     *
244
     * @return array
245
     */
246 1
    public function bStrArr(array $values, $default = ''): array
247
    {
248 1
        return $this->string->bStrArr($values, $default);
249
    }
250
251
    /**
252
     * Bind a string to the PDO data type.
253
     *
254
     * @param string|int|float|bool|null $value
255
     * @param int $type
256
     *
257
     * @return array
258
     */
259 1
    public function bValueType($value, int $type = \PDO::PARAM_STR): array
260
    {
261 1
        return $this->other->bValueType($value, $type);
262
    }
263
}
264