1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
13
|
|
|
|
14
|
|
|
final class IntValue implements MakeEmptyInterface, ValueObjectInterface |
15
|
|
|
{ |
16
|
|
|
private ?int $value; |
17
|
|
|
|
18
|
1 |
|
public function isZero(): bool |
19
|
|
|
{ |
20
|
1 |
|
$this->assertNotEmpty(); |
21
|
1 |
|
return $this->value === 0; |
22
|
|
|
} |
23
|
|
|
|
24
|
8 |
|
public function isEmpty(): bool |
25
|
|
|
{ |
26
|
8 |
|
return $this->value === null; |
27
|
|
|
} |
28
|
|
|
|
29
|
3 |
|
public function add(self $amount): self |
30
|
|
|
{ |
31
|
3 |
|
$this->assertNotEmpty(); |
32
|
2 |
|
Assertion::false($amount->isEmpty(), 'Addition must not be empty.'); |
33
|
1 |
|
return self::fromNative((int)$this->toNative() + (int)$amount->toNative()); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function subtract(self $amount): self |
37
|
|
|
{ |
38
|
3 |
|
$this->assertNotEmpty(); |
39
|
2 |
|
Assertion::false($amount->isEmpty(), 'Subtraction must not be empty.'); |
40
|
1 |
|
return self::fromNative((int)$this->toNative() - (int)$amount->toNative()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function isGreaterThanOrEqualTo(self $comparator): bool |
44
|
|
|
{ |
45
|
|
|
$this->assertNotEmpty(); |
46
|
|
|
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.'); |
47
|
|
|
return $this->toNative() >= $comparator->toNative(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isLessThanOrEqualTo(self $comparator): bool |
51
|
|
|
{ |
52
|
|
|
$this->assertNotEmpty(); |
53
|
|
|
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.'); |
54
|
|
|
return $this->toNative() <= $comparator->toNative(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isGreaterThan(self $comparator): bool |
58
|
|
|
{ |
59
|
|
|
$this->assertNotEmpty(); |
60
|
|
|
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.'); |
61
|
|
|
return $this->toNative() > $comparator->toNative(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function isLessThan(self $comparator): bool |
65
|
|
|
{ |
66
|
|
|
$this->assertNotEmpty(); |
67
|
|
|
Assertion::false($comparator->isEmpty(), 'Comparator must not be empty.'); |
68
|
|
|
return $this->toNative() < $comparator->toNative(); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
public static function makeEmpty(): self |
72
|
|
|
{ |
73
|
6 |
|
return new self; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public static function zero(): self |
77
|
|
|
{ |
78
|
1 |
|
return new self(0); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @param null|int|string $value */ |
82
|
21 |
|
public static function fromNative($value): self |
83
|
|
|
{ |
84
|
21 |
|
$value = $value === '' ? null : $value; |
85
|
21 |
|
Assertion::nullOrIntegerish($value, 'Trying to create IntValue VO from unsupported value type.'); |
86
|
21 |
|
return $value === null ? new self : new self((int)$value); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
public function toNative(): ?int |
90
|
|
|
{ |
91
|
11 |
|
return $this->value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @param self $comparator */ |
95
|
1 |
|
public function equals($comparator): bool |
96
|
|
|
{ |
97
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
98
|
1 |
|
return $this->toNative() === $comparator->toNative(); |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
public function __toString(): string |
102
|
|
|
{ |
103
|
6 |
|
return (string)$this->value; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
private function assertNotEmpty(): void |
107
|
|
|
{ |
108
|
7 |
|
Assertion::false($this->isEmpty(), 'Int value is empty.'); |
109
|
5 |
|
} |
110
|
|
|
|
111
|
21 |
|
private function __construct(int $value = null) |
112
|
|
|
{ |
113
|
21 |
|
$this->value = $value; |
114
|
21 |
|
} |
115
|
|
|
} |
116
|
|
|
|