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

BooleanType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 47
ccs 0
cts 14
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToSQL() 0 8 2
B convertToPHP() 0 20 5
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