Lines
100.00%
14 / 14
Methods
100.00%
5 / 5
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| normalize | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [Lucent\Validation\NormalizeConstraint] defaultMessage | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lucent\Validation\NormalizeConstraint] validate | 100.00% | 2 / 2 | 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 | ||
| 14 | final class Trim extends NormalizeConstraint | |
| 15 | { | |
| 16 | /** | |
| 17 | * Trim whitespace from a string value. | |
| 18 | * | |
| 19 | * Non-string values are returned unchanged. | |
| 20 | * | |
| 21 | * @param FieldContext $ctx The context of the field being normalized. | |
| 22 | * @param mixed $value The raw value to trim. | |
| 23 | * @return mixed The trimmed string, or the original value if not a string. | |
| 24 | */ | |
| 25 | #[Override] | |
| 26 | public function normalize(FieldContext $ctx, mixed $value): mixed | |
| 27 | { | |
| 28 | if (is_string($value)) { | |
| 29 | return trim($value); | |
| 30 | } | |
| 31 | ||
| 32 | return $value; | |
| 33 | } | |
| 34 | } |
Inherited from Lucent\Validation\NormalizeConstraint
| 31 | protected function defaultMessage(): string|\Closure|null | |
| 32 | { | |
| 33 | return null; | |
| 34 | } |
| 46 | public function validate(FieldContext $ctx): bool | |
| 47 | { | |
| 48 | $ctx->normalize($this->normalize($ctx, $ctx->value)); | |
| 49 | return true; | |
| 50 | } |
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 | } |