|
9 | 9 | use PhpTypedValues\Contract\IntTypeInterface; |
10 | 10 | use PhpTypedValues\Exception\IntegerTypeException; |
11 | 11 |
|
| 12 | +use function sprintf; |
| 13 | + |
12 | 14 | /** |
13 | 15 | * @psalm-immutable |
14 | 16 | */ |
@@ -52,4 +54,42 @@ public function toString(): string |
52 | 54 | { |
53 | 55 | return (string) $this->value; |
54 | 56 | } |
| 57 | + |
| 58 | + /** |
| 59 | + * @throws IntegerTypeException |
| 60 | + */ |
| 61 | + #[Override] |
| 62 | + public function assertLessThan(int $value, int $limit, bool $inclusive = false): void |
| 63 | + { |
| 64 | + if ($inclusive && $value > $limit) { |
| 65 | + throw new IntegerTypeException(sprintf('Value "%d" is greater than maximum "%d"', $value, $limit)); |
| 66 | + } |
| 67 | + |
| 68 | + if (!$inclusive && $value >= $limit) { |
| 69 | + throw new IntegerTypeException(sprintf('Value "%d" is greater than or equal to maximum "%d"', $value, $limit)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @throws IntegerTypeException |
| 75 | + */ |
| 76 | + #[Override] |
| 77 | + public function assertGreaterThan(int $value, int $limit, bool $inclusive = false): void |
| 78 | + { |
| 79 | + if ($inclusive && $value < $limit) { |
| 80 | + throw new IntegerTypeException(sprintf('Value "%d" is less than minimum "%d"', $value, $limit)); |
| 81 | + } |
| 82 | + |
| 83 | + if (!$inclusive && $value <= $limit) { |
| 84 | + throw new IntegerTypeException(sprintf('Value "%d" is less than or equal to minimum "%d"', $value, $limit)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Domain-specific assertion to validate provided integer value. |
| 90 | + * |
| 91 | + * @throws IntegerTypeException |
| 92 | + */ |
| 93 | + #[Override] |
| 94 | + abstract public function assert(int $value): void; |
55 | 95 | } |
0 commit comments