Lines
100.00%
2 / 2
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Integer | 100.00% | 2 / 2 | 100.00% | 2 / 2 | 2 | 100.00% | 1 / 1 | |||
| defaultMessage | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| validate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lucent\Validation\Constraints; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | use Lucent\Validation\Constraint; | |
| 9 | use Lucent\Validation\FieldContext; | |
| 10 | use Override; | |
| 11 | ||
| 12 | /** | |
| 13 | * Validates that a value is an integer. | |
| 14 | * | |
| 15 | * An explicit type check that complements {@see Numeric}, which coerces | |
| 16 | * numeric strings to a number. This constraint rejects floats and numeric | |
| 17 | * strings, requiring a genuine `int`. | |
| 18 | */ | |
| 19 | final class Integer extends Constraint | |
| 20 | { | |
| 21 | /** | |
| 22 | * @return string|Closure(FieldContext): string The default error message. | |
| 23 | */ | |
| 24 | #[Override] | |
| 25 | protected function defaultMessage(): string|Closure|null | |
| 26 | { | |
| 27 | return fn(FieldContext $ctx) => "The {$ctx->field} must be an integer."; | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * Validate that the value is an integer. | |
| 32 | * | |
| 33 | * @param FieldContext $ctx The context of the field being validated. | |
| 34 | * @return bool True if the value is an integer, false otherwise. | |
| 35 | */ | |
| 36 | #[Override] | |
| 37 | public function validate(FieldContext $ctx): bool | |
| 38 | { | |
| 39 | return is_int($ctx->value); | |
| 40 | } | |
| 41 | } |