Passed
Pull Request — main (#30)
by Michael
03:17
created

Boolean::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 3
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects\Collection\Primitive;
14
15
use Illuminate\Support\Stringable;
16
use InvalidArgumentException;
17
use MichaelRubel\ValueObjects\ValueObject;
18
19
/**
20
 * "Boolean" object presenting a boolean value.
21
 *
22
 * @author Michael Rubél <[email protected]>
23
 *
24
 * @template TKey of array-key
25
 * @template TValue
26
 *
27
 * @method static static make(bool|int|string $value)
28
 * @method static static from(bool|int|string $value)
29
 * @method static static makeOrNull(bool|int|string|null $value)
30
 *
31
 * @extends ValueObject<TKey, TValue>
32
 */
33
class Boolean extends ValueObject
34
{
35
    /**
36
     * @var bool|int|string
37
     */
38
    protected bool|int|string $value;
39
40
    /**
41
     * Values that represent `true` boolean.
42
     *
43
     * @var array
44
     */
45
    protected array $trueValues = ['1', 'true', 1, 'on', 'yes'];
46
47
    /**
48
     * Values that represent `false` boolean.
49
     *
50
     * @var array
51
     */
52
    protected array $falseValues = ['0', 'false', 0, 'off', 'no'];
53
54
    /**
55
     * Create a new instance of the value object.
56
     *
57
     * @param  bool|int|string  $value
58
     */
59 13
    public function __construct(bool|int|string $value)
60
    {
61 13
        if (isset($this->value)) {
62 1
            throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE);
63
        }
64
65 13
        $this->value = $value;
66
67 13
        if (! is_bool($this->value)) {
68 12
            $this->handleNonBooleanValue();
69
        }
70
    }
71
72
    /**
73
     * Get the object value.
74
     *
75
     * @return bool
76
     */
77 9
    public function value(): bool
78
    {
79 9
        return (bool) $this->value;
80
    }
81
82
    /**
83
     * Determine if the passed boolean is true.
84
     *
85
     * @return void
86
     */
87 13
    protected function handleNonBooleanValue(): void
88
    {
89 13
        $string = new Stringable($this->value);
90
91 11
        $this->value = match (true) {
92 13
            $string->contains($this->trueValues, true) => true,
93 8
            $string->contains($this->falseValues, true) => false,
94 2
            default => throw new InvalidArgumentException('Invalid boolean.'),
95
        };
96
    }
97
98
    /**
99
     * Get string representation of the value object.
100
     *
101
     * @return string
102
     */
103 1
    public function toString(): string
104
    {
105 1
        return ! empty($this->value()) ? 'true' : 'false';
106
    }
107
108
    /**
109
     * Get string representation of the value object.
110
     *
111
     * @return string
112
     */
113 1
    public function __toString(): string
114
    {
115 1
        return $this->toString();
116
    }
117
}
118