Lines
100.00%
11 / 11
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% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| normalize | n/a | 0 / 0 | n/a | 0 / 0 | 0 | ||
| [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 | ||
| 19 | abstract class NormalizeConstraint extends Constraint | |
| 20 | { | |
| 21 | /** | |
| 22 | * Normalizers never produce an error message. | |
| 23 | * | |
| 24 | * A normalizer always passes, so its message is never requested in normal | |
| 25 | * flow. Returning null signals that no error should be recorded, which is | |
| 26 | * safe even if {@see Constraint::message()} is called on a normalizer. | |
| 27 | * | |
| 28 | * @return string|\Closure(FieldContext): ?string|null Always null. | |
| 29 | */ | |
| 30 | #[Override] | |
| 31 | protected function defaultMessage(): string|\Closure|null | |
| 32 | { | |
| 33 | return null; | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Normalizers always pass. | |
| 38 | * | |
| 39 | * Applies {@see normalize()} to the current value and stores the result | |
| 40 | * on the context, then returns true. | |
| 41 | * | |
| 42 | * @param FieldContext $ctx The context of the field being normalized. | |
| 43 | * @return bool Always true. | |
| 44 | */ | |
| 45 | #[Override] | |
| 46 | public function validate(FieldContext $ctx): bool | |
| 47 | { | |
| 48 | $ctx->normalize($this->normalize($ctx, $ctx->value)); | |
| 49 | return true; | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * Transform a raw value. | |
| 54 | * | |
| 55 | * @param FieldContext $ctx The context of the field being normalized. | |
| 56 | * @param mixed $value The raw value to transform. | |
| 57 | * @return mixed The transformed value. | |
| 58 | */ | |
| 59 | abstract public function normalize(FieldContext $ctx, mixed $value): mixed; | |
| 60 | } |
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 | } |