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\Str; |
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 |
37
|
|
|
*/ |
38
|
|
|
protected bool $value; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Values that represent `true` boolean. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected array $trueValues = ['1', 'true', 'on', 'yes']; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Values that represent `false` boolean. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected array $falseValues = ['0', 'false', 'off', 'no']; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a new instance of the value object. |
56
|
|
|
* |
57
|
|
|
* @param bool|int|string $value |
58
|
|
|
*/ |
59
|
14 |
|
public function __construct(bool|int|string $value) |
60
|
|
|
{ |
61
|
14 |
|
if (isset($this->value)) { |
62
|
1 |
|
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE); |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
! is_bool($value) ? $this->handleNonBoolean($value) : $this->value = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the object value. |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
9 |
|
public function value(): bool |
74
|
|
|
{ |
75
|
9 |
|
return $this->value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine if the passed boolean is true. |
80
|
|
|
* |
81
|
|
|
* @param int|string $value |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
14 |
|
protected function handleNonBoolean(int|string $value): void |
85
|
|
|
{ |
86
|
14 |
|
$string = is_string($value) ? $value : (string) $value; |
87
|
|
|
|
88
|
14 |
|
$this->value = match (true) { |
89
|
14 |
|
Str::contains($string, $this->trueValues, ignoreCase: true) => true, |
90
|
14 |
|
Str::contains($string, $this->falseValues, ignoreCase: true) => false, |
91
|
14 |
|
default => throw new InvalidArgumentException('Invalid boolean.'), |
92
|
14 |
|
}; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get string representation of the value object. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public function toString(): string |
101
|
|
|
{ |
102
|
1 |
|
return $this->value() ? 'true' : 'false'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get string representation of the value object. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
1 |
|
public function __toString(): string |
111
|
|
|
{ |
112
|
1 |
|
return $this->toString(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|