Lines 93.33% 14 / 15
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 defaultMessage 100.00% 1 / 1 100.00% 1 / 1 1
 validate 83.33% 5 / 6 0.00% 0 / 1 5.12
 [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
17final class Numeric extends Constraint
18{
19    /**
20     * @return string|Closure(FieldContext): string The default error message.
21     */
22    #[Override]
23    protected function defaultMessage(): string|Closure|null
24    {
25        return fn(FieldContext $ctx) => "The {$ctx->field} must be a number";
26    }
27
28    /**
29     * Validate that the value is numeric.
30     *
31     * Integers and floats pass as-is. Numeric strings are normalized to a
32     * number via {@see FieldContext::normalize()} before passing.
33     *
34     * @param FieldContext $ctx The context of the field being validated.
35     * @return bool True if the value is numeric, false otherwise.
36     */
37    #[Override]
38    public function validate(FieldContext $ctx): bool
39    {
40        if (is_int($ctx->value) || is_float($ctx->value)) {
41            return true;
42        }
43
44        if (is_string($ctx->value) && is_numeric($ctx->value)) {
45            $ctx->normalize(+$ctx->value);
46            return true;
47        }
48
49        return false;
50    }
51}

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    }