1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | |||
6 | namespace DBUnt1tled\VO\VObjects\Scalar; |
||
7 | |||
8 | use DBUnt1tled\VO\Exception\InvalidVOArgumentException; |
||
9 | use DBUnt1tled\VO\VObjects\ValueObject; |
||
10 | |||
11 | class Boolean extends ValueObject |
||
12 | { |
||
13 | /** |
||
14 | * @param mixed $value |
||
15 | * @param mixed ...$other |
||
16 | * @throws \ReflectionException |
||
17 | */ |
||
18 | 3 | public function guard($value, ...$other): void |
|
19 | { |
||
20 | 3 | parent::guard($value); |
|
21 | 3 | if (!\is_bool($value)) { |
|
22 | 1 | throw new InvalidVOArgumentException('Value is invalid. Allowed type is boolean', $value); |
|
23 | } |
||
24 | 2 | } |
|
25 | |||
26 | /** |
||
27 | * @param int $int |
||
28 | * @return Boolean |
||
29 | * @throws \ReflectionException |
||
30 | */ |
||
31 | 1 | public static function createFromInt(int $int): self |
|
32 | { |
||
33 | 1 | return new static($int !== 0); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param string $string |
||
38 | * @return Boolean |
||
39 | * @throws \ReflectionException |
||
40 | */ |
||
41 | 1 | public static function createFromString(string $string): self |
|
42 | { |
||
43 | 1 | return new static(mb_strtolower($string) === 'true' || $string === '1'); |
|
0 ignored issues
–
show
|
|||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | 1 | public function __toString(): string |
|
50 | { |
||
51 | 1 | return $this->value ? 'true' : 'false'; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return int |
||
56 | */ |
||
57 | 1 | public function toInt(): int |
|
58 | { |
||
59 | 1 | return $this->value ? 1 : 0; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param self $boolean |
||
64 | * @return Boolean |
||
65 | * @throws \ReflectionException |
||
66 | */ |
||
67 | 1 | public function and(self $boolean): self |
|
68 | { |
||
69 | 1 | $andResult = $this->value && $boolean->value; |
|
70 | 1 | return new static($andResult); |
|
0 ignored issues
–
show
|
|||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param self $boolean |
||
75 | * @return Boolean |
||
76 | * @throws \ReflectionException |
||
77 | */ |
||
78 | 1 | public function or(self $boolean): self |
|
79 | { |
||
80 | 1 | $orResult = $this->value || $boolean->value; |
|
81 | 1 | return new static($orResult); |
|
0 ignored issues
–
show
|
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return Boolean |
||
86 | * @throws \ReflectionException |
||
87 | */ |
||
88 | 1 | public function not(): self |
|
89 | { |
||
90 | 1 | $notResult = !$this->value; |
|
91 | 1 | return new static($notResult); |
|
0 ignored issues
–
show
|
|||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param self $boolean |
||
96 | * @return Boolean |
||
97 | * @throws \ReflectionException |
||
98 | */ |
||
99 | 1 | public function xor(self $boolean): self |
|
100 | { |
||
101 | 1 | $xorResult = ($this->value xor $boolean->value); |
|
102 | 1 | return new static($xorResult); |
|
0 ignored issues
–
show
|
|||
103 | } |
||
104 | } |
||
105 |