Completed
Push — master ( f5e5fb...7d0a28 )
by Rasmus
02:24
created

BoolType::asInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace mindplay\sql\model\types;
4
5
use InvalidArgumentException;
6
use mindplay\sql\model\schema\Type;
7
8
class BoolType implements Type
9
{
10
    /**
11
     * @var mixed
12
     */
13
    private $true_value;
14
15
    /**
16
     * @var mixed
17
     */
18
    private $false_value;
19
20
    /**
21
     * @param mixed $true_value
22
     * @param mixed $false_value
23
     */
24 1
    protected function __construct($true_value = true, $false_value = false)
25
    {
26 1
        $this->true_value = $true_value;
27 1
        $this->false_value = $false_value;
28 1
    }
29
30
    /**
31
     * Flyweight factory method.
32
     *
33
     * @param int|string|bool $true_value  value to which boolean TRUE should be mapped
34
     * @param int|string|bool $false_value value to which boolean FALSE should be mapped
35
     *
36
     * @return self
37
     */
38 1
    public static function get($true_value = true, $false_value = false)
39
    {
40 1
        static $instances = [];
41
42 1
        $key = "{$true_value}_{$false_value}";
43
44 1
        if (!isset($instances[$key])) {
45 1
            $instances[$key] = new BoolType($true_value, $false_value);
46
        }
47
        
48 1
        return $instances[$key];
49
    }
50
51 1 View Code Duplication
    public function convertToSQL($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53 1
        if ($value === null) {
54
            return null;
55
        }
56
57 1
        if ($value === true) {
58 1
            return $this->true_value;
59
        }
60
61 1
        if ($value === false) {
62 1
            return $this->false_value;
63
        }
64
65
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
66
    }
67
68 1 View Code Duplication
    public function convertToPHP($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 1
        if ($value === null) {
71
            return null;
72
        }
73
74 1
        if ($value === $this->true_value) {
75 1
            return true;
76
        }
77
78 1
        if ($value === $this->false_value) {
79 1
            return false;
80
        }
81
82
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
83
    }
84
}
85