1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Part of SplTypes package. |
||
5 | * |
||
6 | * (c) Adrien Loyant <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Ducks\Component\SplTypes; |
||
15 | |||
16 | /** |
||
17 | * The SplBool class is used to enforce strong typing of the bool type. |
||
18 | * |
||
19 | * @extends SplEnum<bool> |
||
20 | * @implements SplBackedEnum<bool> |
||
21 | * |
||
22 | * @property-read mixed $value |
||
23 | * @property-read string $name |
||
24 | * |
||
25 | * @psalm-api |
||
26 | * @psalm-suppress MissingDependency |
||
27 | * @psalm-suppress UndefinedClass |
||
28 | */ |
||
29 | class SplBool extends SplEnum implements SplBackedEnum, SplEnumSingletonable |
||
30 | { |
||
31 | /** @use SplBackedEnumTrait<bool> */ |
||
32 | use SplBackedEnumTrait; |
||
33 | /** @use SplEnumBackedTrait<bool> */ |
||
34 | use SplEnumBackedTrait; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | * |
||
39 | * @psalm-suppress InvalidClassConstantType |
||
40 | */ |
||
41 | // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
||
42 | protected const __default = self::false; |
||
43 | |||
44 | /** |
||
45 | * Value of enum instance |
||
46 | * |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected bool $value; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
||
55 | public const false = false; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
||
61 | public const true = true; |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | * |
||
66 | * @param bool $initial_value |
||
67 | * |
||
68 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) |
||
69 | * @SuppressWarnings(PHPMD.CamelCaseVariableName) |
||
70 | */ |
||
71 | 5 | public function __construct(bool $initial_value = self::__default) |
|
72 | { |
||
73 | 5 | parent::__construct($initial_value); |
|
74 | |||
75 | /** @psalm-suppress UnsupportedPropertyReferenceUsage */ |
||
76 | 5 | $this->value = &$this->__default; |
|
77 | 5 | $this->name = $this->value ? 'true' : 'false'; |
|
78 | } |
||
79 | |||
80 | 3 | final public function &__invoke(): bool |
|
81 | { |
||
82 | 3 | return $this->__default; |
|
83 | } |
||
84 | } |
||
85 |