Lines
100.00%
10 / 10
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| defaultMessage | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| validate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lucent\Validation\Constraint] withMessage | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [Lucent\Validation\Constraint] message | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| 12 | final class Present extends Constraint | |
| 13 | { | |
| 14 | /** | |
| 15 | * @return string|Closure(FieldContext): string The default error message. | |
| 16 | */ | |
| 17 | #[Override] | |
| 18 | protected function defaultMessage(): string|Closure|null | |
| 19 | { | |
| 20 | return fn(FieldContext $ctx) => "The {$ctx->field} field must be present."; | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * Validate that the field key was present in the request body. | |
| 25 | * | |
| 26 | * Unlike {@see Required}, this only checks that the key exists — the | |
| 27 | * value may be null, an empty string, or false and still pass. This is | |
| 28 | * useful for booleans and checkboxes where an explicit `false` or `0` is | |
| 29 | * a valid submission but the key must still be supplied. | |
| 30 | * | |
| 31 | * @param FieldContext $ctx The context of the field being validated. | |
| 32 | * @return bool True if the field was present, false otherwise. | |
| 33 | */ | |
| 34 | #[Override] | |
| 35 | public function validate(FieldContext $ctx): bool | |
| 36 | { | |
| 37 | return $ctx->present; | |
| 38 | } | |
| 39 | } |
Inherited from Lucent\Validation\Constraint
| 38 | final public function withMessage(string|\Closure $message): static | |
| 39 | { | |
| 40 | $this->customMessage = $message; | |
| 41 | return $this; | |
| 42 | } |
| 56 | final public function message(FieldContext $ctx): ?string | |
| 57 | { | |
| 58 | $message = $this->customMessage ?? $this->defaultMessage(); | |
| 59 | ||
| 60 | if ($message === null) { | |
| 61 | return null; | |
| 62 | } | |
| 63 | ||
| 64 | if ($message instanceof \Closure) { | |
| 65 | return call_user_func($message, $ctx); | |
| 66 | } | |
| 67 | ||
| 68 | return $message; | |
| 69 | } |