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

MySQLLogicBindings::bBool()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.6111
1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings\MySQL;
4
5
use \PDO;
6
use \DomainException;
7
use \Exception;
8
use GeekLab\GLPDO2\Bindings\LogicBindingInterface;
9
10
class MySQLLogicBindings implements LogicBindingInterface
11
{
12
    /**
13
     * Bind a boolean value as bool, with NULL option.
14
     *
15
     * @param int|bool|null $value
16
     * @param bool $null
17
     *
18
     * @return array
19
     * @throws Exception
20
     */
21
    public function bBool($value = null, bool $null = false): array
22
    {
23
        // use NULL
24
        if ($value === null && $null) {
25
            return [null, PDO::PARAM_NULL];
26
        }
27
28
        if ($value === null && $null === false) {
29
            throw new DomainException('Can not bind NULL in boolean spot.');
30
        }
31
32
        return [(bool) $value, PDO::PARAM_BOOL];
33
    }
34
35
    /**
36
     * Bind a boolean value as int, with NULL option.
37
     *
38
     * @param int|bool|null $value
39
     * @param bool $null
40
     *
41
     * @return array
42
     * @throws Exception
43
     */
44
    public function bBoolInt($value = null, bool $null = false): array
45
    {
46
        // use NULL
47
        if ($value === null && $null) {
48
            return [null, PDO::PARAM_NULL];
49
        }
50
51
        if ($value === null && $null === false) {
52
            throw new DomainException('Can not bind NULL in boolean spot.');
53
        }
54
55
        return [(int) $value, PDO::PARAM_INT];
56
    }
57
}
58