value = $digits; } public static function castUsing(array $arguments): string { return CpfCast::class; } public static function rule(): Closure { return static function (string $attribute, mixed $value, Closure $fail): void { if (! is_string($value)) { $fail(__('validation.cpf')); return; } try { new self($value); } catch (InvalidArgumentException) { $fail(__('validation.cpf')); } }; } public function value(): string { return $this->value; } public function formatted(): string { return preg_replace( '/(\d{3})(\d{3})(\d{3})(\d{2})/', '$1.$2.$3-$4', $this->value, ); } public function __toString(): string { return $this->value; } public function jsonSerialize(): string { return $this->value; } private static function isValid(string $digits): bool { if (strlen($digits) !== 11 || preg_match('/^(\d)\1{10}$/', $digits)) { return false; } for ($position = 9; $position < 11; $position++) { $sum = 0; for ($index = 0; $index < $position; $index++) { $sum += (int) $digits[$index] * (($position + 1) - $index); } $checkDigit = (($sum * 10) % 11) % 10; if ((int) $digits[$position] !== $checkDigit) { return false; } } return true; } }