Completed
Push — master ( a86c7c...52e4fc )
by Rasmus
8s
created

BooleanType::convertToSQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace mindplay\sql\types;
4
5
use mindplay\sql\model\Type;
6
use UnexpectedValueException;
7
8
class BooleanType implements Type
9
{
10
    protected $boolean_literals = [
11
        't'     => true,
12
        'true'  => true,
13
        'y'     => true,
14
        'yes'   => true,
15
        'on'    => true,
16
        '1'     => true,
17
        'f'     => false,
18
        'false' => false,
19
        'n'     => false,
20
        'no'    => false,
21
        'off'   => false,
22
        '0'     => false,
23
    ];
24
25
    public function convertToSQL($value)
26
    {
27
        if ($value === null) {
28
            return null;
29
        }
30
31
        return (bool) $value;
32
    }
33
34
    public function convertToPHP($value)
35
    {
36
        if ($value === null) {
37
            return null;
38
        }
39
40
        if (is_bool($value)) {
41
            return $value;
42
        }
43
44
        if (is_int($value)) {
45
            return (bool) $value;
46
        }
47
48
        if (isset($this->boolean_literals[$value])) {
49
            return $this->boolean_literals[$value];
50
        }
51
52
        throw new UnexpectedValueException("Unexpected value given as boolean");
53
    }
54
}
55