Passed
Push — master ( 396899...291a9b )
by Mr
06:02
created

Natural::isLessThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
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 Natural 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 9
    public function isEmpty(): bool
25
    {
26 9
        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 4
    public function subtract(self $amount): self
37
    {
38 4
        $this->assertNotEmpty();
39 3
        Assertion::false($amount->isEmpty(), 'Subtraction must not be empty.');
40 2
        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 2
    public static function zero(): self
77
    {
78 2
        return new self(0);
79
    }
80
81
    /** @param null|int|string $value  */
82 13
    public static function fromNative($value): self
83
    {
84 13
        $value = $value === '' ? null : $value;
85 13
        Assertion::nullOrIntegerish($value, 'Trying to create Natural VO from unsupported value type.');
86 13
        return $value === null ? new self : new self((int)$value);
87
    }
88
89 8
    public function toNative(): ?int
90
    {
91 8
        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 3
    public function __toString(): string
102
    {
103 3
        return (string)$this->value;
104
    }
105
106 8
    private function assertNotEmpty(): void
107
    {
108 8
        Assertion::false($this->isEmpty(), 'Natural is empty.');
109 6
    }
110
111 13
    private function __construct(int $value = null)
112
    {
113 13
        Assertion::nullOrGreaterOrEqualThan($value, 0, 'Must be at least 0.');
114 13
        $this->value = $value;
115 13
    }
116
}
117