Passed
Push — main ( 4fb12f...fe1906 )
by Michael
03:24
created

Boolean::isFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects\Collection\Primitive;
6
7
use MichaelRubel\ValueObjects\ValueObject;
8
9
/**
10
 * @method static static make(bool|int|string $value)
11
 * @method static static from(bool|int|string $value)
12
 */
13
class Boolean extends ValueObject
14
{
15
    /**
16
     * Allowed values that are treated as `true`.
17
     *
18
     * @var array
19
     */
20
    protected array $trueValues = ['1', 'true', 'True', 'TRUE', 1, true];
21
22
    /**
23
     * Allowed values that are treated as `false`.
24
     *
25
     * @var array
26
     */
27
    protected array $falseValues = ['0', 'false', 'False', 'FALSE', 0, false];
28
29
    /**
30
     * Create a new instance of the value object.
31
     *
32
     * @param  bool|int|string  $value
33
     */
34 12
    public function __construct(protected bool|int|string $value)
35
    {
36 10
        $this->value = match (true) {
37 12
            $this->isInTrueValues()  => true,
38 9
            $this->isInFalseValues() => false,
39 2
            default => throw new \InvalidArgumentException('Invalid boolean'),
40
        };
41
    }
42
43
    /**
44
     * Get the object value.
45
     *
46
     * @return bool
47
     */
48 8
    public function value(): bool
49
    {
50 8
        return (bool) $this->value;
51
    }
52
53
    /**
54
     * Determine if the passed boolean is a positive value.
55
     *
56
     * @return bool
57
     */
58 12
    protected function isInTrueValues(): bool
59
    {
60 12
        return in_array($this->value, $this->trueValues, strict: true);
61
    }
62
63
    /**
64
     * Determine if the passed boolean is a negative value.
65
     *
66
     * @return bool
67
     */
68 9
    protected function isInFalseValues(): bool
69
    {
70 9
        return in_array($this->value, $this->falseValues, strict: true);
71
    }
72
73
    /**
74
     * Get string representation of the value object.
75
     *
76
     * @return string
77
     */
78 1
    public function __toString(): string
79
    {
80 1
        return ! empty($this->value()) ? 'true' : 'false';
81
    }
82
}
83