|
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 InvalidArgumentException; |
|
16
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* "Boolean" object presenting a boolean value. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Michael Rubél <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @template TKey of array-key |
|
24
|
|
|
* @template TValue |
|
25
|
|
|
* |
|
26
|
|
|
* @method static static make(bool|int|string $value) |
|
27
|
|
|
* @method static static from(bool|int|string $value) |
|
28
|
|
|
* @method static static makeOrNull(bool|int|string|null $value) |
|
29
|
|
|
* |
|
30
|
|
|
* @extends ValueObject<TKey, TValue> |
|
31
|
|
|
*/ |
|
32
|
|
|
class Boolean extends ValueObject |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool|int|string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected bool|int|string $value; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Allowed values that are treated as `true`. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected array $trueValues = ['1', 'true', 'True', 'TRUE', 1, true]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Allowed values that are treated as `false`. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected array $falseValues = ['0', 'false', 'False', 'FALSE', 0, false]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a new instance of the value object. |
|
55
|
|
|
* |
|
56
|
|
|
* @param bool|int|string $value |
|
57
|
|
|
*/ |
|
58
|
13 |
|
public function __construct(bool|int|string $value) |
|
59
|
|
|
{ |
|
60
|
13 |
|
if (isset($this->value)) { |
|
61
|
1 |
|
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
13 |
|
$this->value = $value; |
|
65
|
|
|
|
|
66
|
11 |
|
$this->value = match (true) { |
|
67
|
13 |
|
$this->isInTrueValues() => true, |
|
68
|
9 |
|
$this->isInFalseValues() => false, |
|
69
|
2 |
|
default => throw new InvalidArgumentException('Invalid boolean.'), |
|
70
|
|
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the object value. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
8 |
|
public function value(): bool |
|
79
|
|
|
{ |
|
80
|
8 |
|
return (bool) $this->value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Determine if the passed boolean is true. |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
13 |
|
protected function isInTrueValues(): bool |
|
89
|
|
|
{ |
|
90
|
13 |
|
return in_array($this->value, $this->trueValues, strict: true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Determine if the passed boolean is false. |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
9 |
|
protected function isInFalseValues(): bool |
|
99
|
|
|
{ |
|
100
|
9 |
|
return in_array($this->value, $this->falseValues, strict: true); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get string representation of the value object. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function toString(): string |
|
109
|
|
|
{ |
|
110
|
1 |
|
return ! empty($this->value()) ? 'true' : 'false'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get string representation of the value object. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function __toString(): string |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->toString(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|