Passed
Push — master ( 1cc59e...a8a804 )
by Travis
02:24
created

BoolGuard   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 61
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 5 1
A allowPseudoBools() 0 5 1
A __construct() 0 4 1
A validation() 0 11 3
A isFalse() 0 3 2
A isPseudoFalse() 0 3 2
A isTrue() 0 3 2
A isPseudoTrue() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards;
5
6
use InputGuard\Guards\Bases\SingleInput;
7
8
class BoolGuard implements Guard
9
{
10
    use ErrorMessagesBase;
11
    use SingleInput;
12
13
    /**
14
     * @var bool
15
     */
16
    private $pseudoBools = false;
17
18 19
    public function __construct($input, ?bool $default = null)
19
    {
20 19
        $this->input = $input;
21 19
        $this->value = $default;
22 19
    }
23
24 10
    public function allowPseudoBools(): self
25
    {
26 10
        $this->pseudoBools = true;
27
28 10
        return $this;
29
    }
30
31 17
    public function value(): ?bool
32
    {
33 17
        $this->success();
34
35 17
        return $this->value;
36
    }
37
38 18
    protected function validation($input, &$value): bool
39
    {
40 18
        if ($this->isTrue($input)) {
41 4
            $value  = true;
42 4
            $return = true;
43 15
        } elseif ($this->isFalse($input)) {
44 6
            $value  = false;
45 6
            $return = true;
46
        }
47
48 18
        return $return ?? false;
49
    }
50
51 18
    private function isTrue($input): bool
52
    {
53 18
        return $input === true || $this->isPseudoTrue($input);
54
    }
55
56 17
    private function isPseudoTrue($input): bool
57
    {
58 17
        return $this->pseudoBools && \in_array($input, [1, '1'], true);
59
    }
60
61 15
    private function isFalse($input): bool
62
    {
63 15
        return $input === false || $this->isPseudoFalse($input);
64
    }
65
66 12
    private function isPseudoFalse($input): bool
67
    {
68 12
        return $this->pseudoBools && \in_array($input, [0, '0', ''], true);
69
    }
70
}
71