Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| AbstractIntValueObject | |
100.00% |
14 / 14 |
|
100.00% |
7 / 7 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromInt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| equals | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExceptionClass | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| validateRules | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Domain; |
| 11 | |
| 12 | use InvalidArgumentException; |
| 13 | use Override; |
| 14 | |
| 15 | abstract readonly class AbstractIntValueObject implements ValueObjectInterface |
| 16 | { |
| 17 | private int $value; |
| 18 | |
| 19 | private function __construct(int $value) |
| 20 | { |
| 21 | $this->validate($value); |
| 22 | $this->value = $value; |
| 23 | } |
| 24 | |
| 25 | #[Override] |
| 26 | final public function __toString(): string |
| 27 | { |
| 28 | return (string) $this->value; |
| 29 | } |
| 30 | |
| 31 | final public static function fromInt(int $value): static |
| 32 | { |
| 33 | return new static($value); |
| 34 | } |
| 35 | |
| 36 | final public function getValue(): int |
| 37 | { |
| 38 | return $this->value; |
| 39 | } |
| 40 | |
| 41 | #[Override] |
| 42 | final public function equals(ValueObjectInterface $other): bool |
| 43 | { |
| 44 | if (!$other instanceof static) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | return $this->value === $other->value; |
| 49 | } |
| 50 | |
| 51 | #[Override] |
| 52 | final public function jsonSerialize(): int |
| 53 | { |
| 54 | return $this->value; |
| 55 | } |
| 56 | |
| 57 | abstract protected static function getExceptionClass(): callable; |
| 58 | |
| 59 | abstract protected function validateRules(int $value): void; |
| 60 | |
| 61 | private function validate(int $value): void |
| 62 | { |
| 63 | try { |
| 64 | $this->validateRules($value); |
| 65 | } catch (InvalidArgumentException $invalidArgumentException) { |
| 66 | $callback = static::getExceptionClass(); |
| 67 | $arguments = $invalidArgumentException->getMessage(); |
| 68 | throw call_user_func($callback, $arguments); |
| 69 | } |
| 70 | } |
| 71 | } |