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

BoolGuard::isPseudoFalse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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