1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AardsGerds\Game\Shared; |
||
6 | |||
7 | class IntegerValue implements \Stringable |
||
8 | { |
||
9 | 52 | final public function __construct( |
|
10 | protected int $value, |
||
11 | ) { |
||
12 | 52 | $this->validate(); |
|
13 | 49 | } |
|
14 | |||
15 | 49 | public function get(): int |
|
16 | { |
||
17 | 49 | return $this->value; |
|
18 | } |
||
19 | |||
20 | 10 | public function equals(self $value): bool |
|
21 | { |
||
22 | 10 | return $this->value === $value->get(); |
|
23 | } |
||
24 | |||
25 | 9 | public function isLowerThan(self $value): bool |
|
26 | { |
||
27 | 9 | return $this->value < $value->get(); |
|
28 | } |
||
29 | |||
30 | 14 | public function isGreaterThan(self $value): bool |
|
31 | { |
||
32 | 14 | return $this->value > $value->get(); |
|
33 | } |
||
34 | |||
35 | 11 | public function isGreaterThanOrEqual(self $value): bool |
|
36 | { |
||
37 | 11 | return $this->value >= $value->get(); |
|
38 | } |
||
39 | |||
40 | 7 | public function diff(self $value): static |
|
41 | { |
||
42 | 7 | return new static(abs($this->value - $value->get())); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
43 | } |
||
44 | |||
45 | 13 | public function increment(): static |
|
46 | { |
||
47 | 13 | $this->value += 1; |
|
48 | 13 | $this->validate(); |
|
49 | |||
50 | 12 | return $this; |
|
51 | } |
||
52 | |||
53 | 15 | public function increaseBy(self $value): static |
|
54 | { |
||
55 | 15 | $this->value += $value->get(); |
|
56 | 15 | $this->validate(); |
|
57 | |||
58 | 15 | return $this; |
|
59 | } |
||
60 | |||
61 | 6 | public function decreaseBy(self $value): static |
|
62 | { |
||
63 | 6 | $this->value -= $value->get(); |
|
64 | 6 | $this->validate(); |
|
65 | |||
66 | 4 | return $this; |
|
67 | } |
||
68 | |||
69 | 2 | public function replaceWith(self $value): static |
|
70 | { |
||
71 | 2 | $this->value = $value->get(); |
|
72 | 2 | $this->validate(); |
|
73 | |||
74 | 2 | return $this; |
|
75 | } |
||
76 | |||
77 | 10 | public function __toString(): string |
|
78 | { |
||
79 | 10 | return (string) $this->value; |
|
80 | } |
||
81 | |||
82 | 44 | protected function validate(): void |
|
83 | { |
||
84 | 44 | if ($this->value < 0) { |
|
85 | 1 | throw IntegerValueException::onlyPositive(); |
|
86 | } |
||
87 | 43 | } |
|
88 | } |
||
89 |