1 | <?php |
||
18 | class BooleanType implements TransmutableTypeInterface, ValueTypeInterface |
||
19 | { |
||
20 | use ValueType; |
||
21 | use Transmutable; |
||
22 | use Boxable; |
||
23 | |||
24 | /** |
||
25 | * @param bool $bool |
||
26 | */ |
||
27 | 18 | public function __construct(bool $bool) |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | * |
||
35 | * @return string|bool |
||
36 | */ |
||
37 | 2 | public function __invoke(int $toType = Type::BOOL) |
|
38 | { |
||
39 | switch ($toType) { |
||
40 | 2 | case Type::STRING: |
|
41 | 1 | return ($this->value) ? 'true' : 'false'; |
|
42 | 2 | case Type::BOOL: |
|
43 | 2 | return $this->value; |
|
44 | default: |
||
45 | 1 | throw new InvalidTypeCastException(static::class, $this->getTranslatedType($toType)); |
|
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Returns true if boolean is true. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 8 | public function isTrue(): bool |
|
58 | |||
59 | /** |
||
60 | * Returns true if boolean is false. |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | 2 | public function isFalse(): bool |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * |
||
72 | * @return BooleanType |
||
73 | */ |
||
74 | 12 | public static function valueOf($mixed): BooleanType |
|
75 | { |
||
76 | 12 | return new static(self::asBool($mixed)); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Returns a mixed variable as a bool. |
||
81 | * |
||
82 | * @param mixed $mixed |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | 12 | private static function asBool($mixed): bool |
|
105 | |||
106 | /** |
||
107 | * Maps specific strings to a boolean value. |
||
108 | * |
||
109 | * @param $key |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | 3 | protected static function getFromStringMap($key): bool |
|
128 | } |
||
129 |