BooleanType   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 111
ccs 31
cts 31
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isTrue() 0 4 1
A isFalse() 0 4 1
B asBool() 0 19 8
A getFromStringMap() 0 15 2
A __invoke() 0 11 4
A valueOf() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tdn\PhpTypes\Type;
6
7
use Tdn\PhpTypes\Exception\InvalidTypeCastException;
8
use Tdn\PhpTypes\Type\Traits\ValueType;
9
use Tdn\PhpTypes\Type\Traits\Boxable;
10
use Tdn\PhpTypes\Type\Traits\Transmutable;
11
use Tdn\PhpTypes\Exception\InvalidTransformationException;
12
13
/**
14
 * Class BooleanType.
15
 *
16
 * A BooleanType is a TypeInterface implementation that wraps around a regular PHP bool.
17
 */
18
class BooleanType implements TransmutableTypeInterface, ValueTypeInterface
19
{
20
    use ValueType;
21
    use Transmutable;
22
    use Boxable;
23
24
    /**
25
     * @param bool $bool
26
     */
27 18
    public function __construct(bool $bool)
28
    {
29 18
        $this->value = $bool;
30 18
    }
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @return string|bool
36
     */
37 2
    public function __invoke(int $toType = Type::BOOL)
38
    {
39
        switch ($toType) {
40 2
            case Type::STRING:
41 1
                return ($this->value) ? 'true' : 'false';
42 2
            case Type::BOOL:
43 2
                return $this->value;
44
            default:
45 1
                throw new InvalidTypeCastException(static::class, $this->getTranslatedType($toType));
46
        }
47
    }
48
49
    /**
50
     * Returns true if boolean is true.
51
     *
52
     * @return bool
53
     */
54 8
    public function isTrue(): bool
55
    {
56 8
        return $this->value;
57
    }
58
59
    /**
60
     * Returns true if boolean is false.
61
     *
62
     * @return bool
63
     */
64 2
    public function isFalse(): bool
65
    {
66 2
        return !$this->value;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @return BooleanType
73
     */
74 12
    public static function valueOf($mixed): BooleanType
75
    {
76 12
        return new static(self::asBool($mixed));
77
    }
78
79
    /**
80
     * Returns a mixed variable as a bool.
81
     *
82
     * @param mixed $mixed
83
     *
84
     * @return bool
85
     */
86 12
    private static function asBool($mixed): bool
87
    {
88 12
        if ($mixed instanceof static || $mixed instanceof StringType) {
89 2
            $mixed = $mixed->get();
90
        }
91
92 12
        $type = strtolower(gettype($mixed));
93
        switch ($type) {
94 12
            case 'boolean':
95 1
                return (bool) $mixed;
96 12
            case 'string':
97 3
                return self::getFromStringMap($mixed);
98 10
            case 'resource':
99
                // Don't really care for this, and might go away soon unless someone actually ends up using it.
100 1
                return ($mixed === null || $mixed === false) ? false : true;
101
            default:
102 9
                throw new InvalidTransformationException($type, static::class);
103
        }
104
    }
105
106
    /**
107
     * Maps specific strings to a boolean value.
108
     *
109
     * @param $key
110
     *
111
     * @return bool
112
     */
113 3
    protected static function getFromStringMap($key): bool
114
    {
115
        $map = [
116 3
            'true' => true,
117
            'on' => true,
118
            'yes' => true,
119
        ];
120
121 3
        if (array_key_exists($key, $map)) {
122 3
            return $map[$key];
123
        }
124
125
        // All other strings will always be false.
126 3
        return false;
127
    }
128
}
129