From 4c07b0cb62501c0db3bd4651911c9fe3aac548d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Lopes?= Date: Fri, 17 Apr 2026 08:53:33 -0300 Subject: [PATCH] feat(rules): update rules using new interface --- src/Contracts/Formatted.php | 11 ------ src/Formats/CNPJ.php | 16 --------- src/Formats/CPF.php | 16 --------- src/Formats/PIS.php | 16 --------- src/Rules/CEP.php | 19 +++++----- src/Rules/CNH.php | 25 ++++++-------- src/Rules/CNPJ.php | 35 ++++++++++++------- src/Rules/CNS.php | 50 +++++++++++++++------------ src/Rules/CPF.php | 33 ++++++++++++------ src/Rules/CPForCNPJ.php | 34 +++++++++++++----- src/Rules/CellPhone.php | 19 +++++----- src/Rules/CellPhoneWithCode.php | 19 +++++----- src/Rules/CellPhoneWithCodeNoMask.php | 19 +++++----- src/Rules/CellPhoneWithDDD.php | 19 +++++----- src/Rules/Money.php | 25 ++++++-------- src/Rules/PIS.php | 33 ++++++++++++------ src/Rules/TelePhone.php | 19 +++++----- src/Rules/TelePhoneWithCode.php | 19 +++++----- src/Rules/TelePhoneWithDDD.php | 19 +++++----- src/Rules/UF.php | 21 +++++------ src/Rules/VehiclePlate.php | 19 +++++----- src/ServiceProvider.php | 11 ++++-- 22 files changed, 259 insertions(+), 238 deletions(-) delete mode 100644 src/Contracts/Formatted.php delete mode 100644 src/Formats/CNPJ.php delete mode 100644 src/Formats/CPF.php delete mode 100644 src/Formats/PIS.php diff --git a/src/Contracts/Formatted.php b/src/Contracts/Formatted.php deleted file mode 100644 index 5cf335f..0000000 --- a/src/Contracts/Formatted.php +++ /dev/null @@ -1,11 +0,0 @@ - 0; - } -} diff --git a/src/Formats/CPF.php b/src/Formats/CPF.php deleted file mode 100644 index 21be125..0000000 --- a/src/Formats/CPF.php +++ /dev/null @@ -1,16 +0,0 @@ - 0; - } -} diff --git a/src/Formats/PIS.php b/src/Formats/PIS.php deleted file mode 100644 index 44f47f3..0000000 --- a/src/Formats/PIS.php +++ /dev/null @@ -1,16 +0,0 @@ - 0; - } -} diff --git a/src/Rules/CEP.php b/src/Rules/CEP.php index b42980a..736e65b 100644 --- a/src/Rules/CEP.php +++ b/src/Rules/CEP.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CEP implements Rule +class CEP implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^\d{2}\.?\d{3}-\d{3}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cep')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('format_cep'); + return preg_match('/^\d{2}\.?\d{3}-\d{3}$/', $value) > 0; } } diff --git a/src/Rules/CNH.php b/src/Rules/CNH.php index 42f99a8..1a7680c 100644 --- a/src/Rules/CNH.php +++ b/src/Rules/CNH.php @@ -2,31 +2,26 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CNH implements Rule +class CNH implements ValidationRule { /** - * Determine if the validation rule passes. - */ - public function passes($attribute, $value): bool - { - return $this->checkCNH($value); - } - - /** - * Get the validation error message. + * Run the validation rule. */ - public function message(): string + public function validate(string $attribute, mixed $value, Closure $fail): void { - return Helpers::getMessage('cnh'); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cnh')); + } } /** - * Determine if the CNH is valid. + * Determine if the validation rule passes. */ - private function checkCNH(mixed $value): bool + protected function passes(mixed $value): bool { $ret = false; diff --git a/src/Rules/CNPJ.php b/src/Rules/CNPJ.php index 9304bbd..b0f7157 100644 --- a/src/Rules/CNPJ.php +++ b/src/Rules/CNPJ.php @@ -2,38 +2,49 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Str; -use ValidatorDocs\Formats\CNPJ as FormatsCNPJ; use ValidatorDocs\Support\Helpers; use ValidatorDocs\Traits\WithParameters; -class CNPJ implements Rule +class CNPJ implements ValidationRule { use WithParameters; /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (! $this->hasFormat()) { - return $this->checkCNPJ(Str::onlyNumbers($value)); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cnpj')); } + } - return (new FormatsCNPJ)->formatted($value) && $this->checkCNPJ(Str::onlyNumbers($value)); + /** + * Determine if the validation rule passes. + */ + protected function passes(mixed $value): bool + { + return $this->checkFormatted($value) + && $this->checkCNPJ(Str::onlyNumbers($value)); } /** - * Get the validation error message. + * Check if the value is formatted. */ - public function message(): string + private function checkFormatted(mixed $value): bool { - return Helpers::getMessage('cnpj'); + if (! $this->hasFormat()) { + return true; + } + + return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0; } /** - * Determine if the CNPJ is valid. + * Check if the CNPJ is valid. */ private function checkCNPJ(mixed $c): bool { diff --git a/src/Rules/CNS.php b/src/Rules/CNS.php index d9e8097..358bfe2 100644 --- a/src/Rules/CNS.php +++ b/src/Rules/CNS.php @@ -2,12 +2,26 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CNS implements Rule +class CNS implements ValidationRule { - public function passes($attribute, $cns) + /** + * Run the validation rule. + */ + public function validate(string $attribute, mixed $cns, Closure $fail): void + { + if ($this->passes($cns) === false) { + $fail(Helpers::getMessage('cns')); + } + } + + /** + * Determine if the validation rule passes. + */ + protected function passes(mixed $cns): bool { if (! isset($cns[0])) { return false; @@ -23,11 +37,19 @@ public function passes($attribute, $cns) } /** - * Get the validation error message. + * Validate CNS that starts with 7, 8, 9 */ - public function message(): string + protected function cnsProv(string $cns): bool { - return Helpers::getMessage('cns'); + if (strlen($cns) != 15) { + return false; + } + + for ($s = 0, $i = 0, $j = 15; $i < 15; $i++, $j--) { + $s += intval($cns[$i]) * $j; + } + + return $s % 11 === 0; } /** @@ -62,20 +84,4 @@ protected function cns(string $cns): bool return $cns === $result; } - - /** - * Validate CNS that starts with 7, 8, 9 - */ - protected function cnsProv(string $cns): bool - { - if (strlen($cns) != 15) { - return false; - } - - for ($s = 0, $i = 0, $j = 15; $i < 15; $i++, $j--) { - $s += intval($cns[$i]) * $j; - } - - return $s % 11 === 0; - } } diff --git a/src/Rules/CPF.php b/src/Rules/CPF.php index 66eac63..12c8b8a 100644 --- a/src/Rules/CPF.php +++ b/src/Rules/CPF.php @@ -2,34 +2,45 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Str; -use ValidatorDocs\Formats\CPF as FormatsCPF; use ValidatorDocs\Support\Helpers; use ValidatorDocs\Traits\WithParameters; -class CPF implements Rule +class CPF implements ValidationRule { use WithParameters; /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (! $this->hasFormat()) { - return $this->checkCPF(Str::onlyNumbers($value)); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cpf')); } + } - return (new FormatsCPF)->formatted($value) && $this->checkCPF(Str::onlyNumbers($value)); + /** + * Determine if the validation rule passes. + */ + protected function passes(mixed $value): bool + { + return $this->checkFormatted($value) + && $this->checkCPF(Str::onlyNumbers($value)); } /** - * Get the validation error message. + * Check if the value is formatted. */ - public function message(): string + private function checkFormatted(mixed $value): bool { - return Helpers::getMessage('cpf'); + if (! $this->hasFormat()) { + return true; + } + + return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0; } /** diff --git a/src/Rules/CPForCNPJ.php b/src/Rules/CPForCNPJ.php index ede9560..8722ffa 100644 --- a/src/Rules/CPForCNPJ.php +++ b/src/Rules/CPForCNPJ.php @@ -2,28 +2,44 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; use ValidatorDocs\Traits\WithParameters; -class CPForCNPJ implements Rule +class CPForCNPJ implements ValidationRule { use WithParameters; /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return (new CPF)->parameters($this->parameters)->passes($attribute, $value) - || (new CNPJ)->parameters($this->parameters)->passes($attribute, $value); + if ($this->passes($attribute, $value) === false) { + $fail(Helpers::getMessage('cpf_or_cnpj')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(string $attribute, mixed $value): bool { - return Helpers::getMessage('cpf_or_cnpj'); + $cpfPasses = true; + (new CPF)->parameters($this->parameters)->validate($attribute, $value, function () use (&$cpfPasses): void { + $cpfPasses = false; + }); + + if ($cpfPasses) { + return true; + } + + $cnpjPasses = true; + (new CNPJ)->parameters($this->parameters)->validate($attribute, $value, function () use (&$cnpjPasses): void { + $cnpjPasses = false; + }); + + return $cnpjPasses; } } diff --git a/src/Rules/CellPhone.php b/src/Rules/CellPhone.php index b8bdfe5..03cfc8a 100644 --- a/src/Rules/CellPhone.php +++ b/src/Rules/CellPhone.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CellPhone implements Rule +class CellPhone implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^\d{4,5}-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cellphone')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('cellphone'); + return preg_match('/^\d{4,5}-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/CellPhoneWithCode.php b/src/Rules/CellPhoneWithCode.php index f48e779..2a3677b 100644 --- a/src/Rules/CellPhoneWithCode.php +++ b/src/Rules/CellPhoneWithCode.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CellPhoneWithCode implements Rule +class CellPhoneWithCode implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^[+]\d{1,2}\s?\(\d{2}\)\s?\d{4,5}\-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cellphone_with_code')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('cellphone_with_code'); + return preg_match('/^[+]\d{1,2}\s?\(\d{2}\)\s?\d{4,5}\-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/CellPhoneWithCodeNoMask.php b/src/Rules/CellPhoneWithCodeNoMask.php index df81d42..1c75f0d 100644 --- a/src/Rules/CellPhoneWithCodeNoMask.php +++ b/src/Rules/CellPhoneWithCodeNoMask.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CellPhoneWithCodeNoMask implements Rule +class CellPhoneWithCodeNoMask implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^[+]\d{1,2}\s?\d{2}\s?\d{4,5}\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cellphone_with_code_no_mask')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('cellphone_with_code_no_mask'); + return preg_match('/^[+]\d{1,2}\s?\d{2}\s?\d{4,5}\d{4}$/', $value) > 0; } } diff --git a/src/Rules/CellPhoneWithDDD.php b/src/Rules/CellPhoneWithDDD.php index f58e940..1abd945 100644 --- a/src/Rules/CellPhoneWithDDD.php +++ b/src/Rules/CellPhoneWithDDD.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class CellPhoneWithDDD implements Rule +class CellPhoneWithDDD implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^\(\d{2}\)\s?\d{4,5}-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('cellphone_with_ddd')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('cellphone_with_ddd'); + return preg_match('/^\(\d{2}\)\s?\d{4,5}-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/Money.php b/src/Rules/Money.php index ca41053..f573c8c 100644 --- a/src/Rules/Money.php +++ b/src/Rules/Money.php @@ -2,36 +2,31 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Number; use Illuminate\Support\Str; use ValidatorDocs\Support\Helpers; use ValidatorDocs\Traits\WithParameters; -class Money implements Rule +class Money implements ValidationRule { use WithParameters; /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return $this->checkMoney($value); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('money')); + } } /** - * Get the validation error message. - */ - public function message(): string - { - return Helpers::getMessage('money'); - } - - /** - * Determine if the money is valid. + * Determine if the validation rule passes. */ - private function checkMoney(mixed $value): bool + protected function passes(mixed $value): bool { $money = Str::moneyValue($value); diff --git a/src/Rules/PIS.php b/src/Rules/PIS.php index 1e84be9..91a02fe 100644 --- a/src/Rules/PIS.php +++ b/src/Rules/PIS.php @@ -2,34 +2,45 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Str; -use ValidatorDocs\Formats\PIS as FormatsPIS; use ValidatorDocs\Support\Helpers; use ValidatorDocs\Traits\WithParameters; -class PIS implements Rule +class PIS implements ValidationRule { use WithParameters; /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (! $this->hasFormat()) { - return $this->checkPIS(Str::onlyNumbers($value)); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('pis')); } + } - return (new FormatsPIS)->formatted($value) && $this->checkPIS(Str::onlyNumbers($value)); + /** + * Determine if the validation rule passes. + */ + protected function passes(mixed $value): bool + { + return $this->checkFormatted($value) + && $this->checkPIS(Str::onlyNumbers($value)); } /** - * Get the validation error message. + * Check if the value is formatted. */ - public function message(): string + private function checkFormatted(mixed $value): bool { - return Helpers::getMessage('pis'); + if (! $this->hasFormat()) { + return true; + } + + return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0; } /** diff --git a/src/Rules/TelePhone.php b/src/Rules/TelePhone.php index 5f26efd..124e675 100644 --- a/src/Rules/TelePhone.php +++ b/src/Rules/TelePhone.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class TelePhone implements Rule +class TelePhone implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^\d{4}-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('telephone')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('telephone'); + return preg_match('/^\d{4}-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/TelePhoneWithCode.php b/src/Rules/TelePhoneWithCode.php index e00fd9f..2a26a9c 100644 --- a/src/Rules/TelePhoneWithCode.php +++ b/src/Rules/TelePhoneWithCode.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class TelePhoneWithCode implements Rule +class TelePhoneWithCode implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^[+]\d{1,2}\s?\(\d{2}\)\s?\d{4,5}\-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('telephone_with_code')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('telephone_with_code'); + return preg_match('/^[+]\d{1,2}\s?\(\d{2}\)\s?\d{4,5}\-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/TelePhoneWithDDD.php b/src/Rules/TelePhoneWithDDD.php index 31f2c43..fd6561f 100644 --- a/src/Rules/TelePhoneWithDDD.php +++ b/src/Rules/TelePhoneWithDDD.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class TelePhoneWithDDD implements Rule +class TelePhoneWithDDD implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^\(\d{2}\)\s?\d{4}-\d{4}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('telephone_with_ddd')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('telephone_with_ddd'); + return preg_match('/^\(\d{2}\)\s?\d{4}-\d{4}$/', $value) > 0; } } diff --git a/src/Rules/UF.php b/src/Rules/UF.php index 7a9a1ce..320850b 100644 --- a/src/Rules/UF.php +++ b/src/Rules/UF.php @@ -2,27 +2,28 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Enum\StateEnum; use ValidatorDocs\Support\Helpers; -class UF implements Rule +class UF implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return collect(StateEnum::cases()) - ->map(fn (StateEnum $item) => $item->value) - ->contains($value); + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('uf')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('uf'); + return collect(StateEnum::cases())->pluck('value')->contains($value); } } diff --git a/src/Rules/VehiclePlate.php b/src/Rules/VehiclePlate.php index 96e1386..b5e402d 100644 --- a/src/Rules/VehiclePlate.php +++ b/src/Rules/VehiclePlate.php @@ -2,24 +2,27 @@ namespace ValidatorDocs\Rules; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; use ValidatorDocs\Support\Helpers; -class VehiclePlate implements Rule +class VehiclePlate implements ValidationRule { /** - * Determine if the validation rule passes. + * Run the validation rule. */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { - return preg_match('/^[a-zA-Z]{3}\-?[0-9][0-9a-zA-Z][0-9]{2}$/', $value) > 0; + if ($this->passes($value) === false) { + $fail(Helpers::getMessage('vehicle_plate')); + } } /** - * Get the validation error message. + * Determine if the validation rule passes. */ - public function message(): string + protected function passes(mixed $value): bool { - return Helpers::getMessage('format_vehicle_plate'); + return preg_match('/^[a-zA-Z]{3}\-?[0-9][0-9a-zA-Z][0-9]{2}$/', $value) > 0; } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 680e417..eb4630c 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; +use ValidatorDocs\Support\Helpers; use ValidatorDocs\Support\Macros; /** @@ -63,14 +64,20 @@ private function bootRules(): void $rule = new $class; $extension = static function ($attribute, $value, $parameters) use ($rule) { + $failed = false; + $rule = static::getRule($rule, $parameters); - return $rule->passes($attribute, $value); + $rule->validate($attribute, $value, static function () use (&$failed): void { + $failed = true; + }); + + return ! $failed; }; $this->app['validator']->extend($name, $extension); - $this->app['validator']->replacer($name, fn () => $rule->message()); + $this->app['validator']->replacer($name, fn () => Helpers::getMessage($name)); }); }