Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/Contracts/Formatted.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Formats/CNPJ.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Formats/CPF.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Formats/PIS.php

This file was deleted.

19 changes: 11 additions & 8 deletions src/Rules/CEP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
25 changes: 10 additions & 15 deletions src/Rules/CNH.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
35 changes: 23 additions & 12 deletions src/Rules/CNPJ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
50 changes: 28 additions & 22 deletions src/Rules/CNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
33 changes: 22 additions & 11 deletions src/Rules/CPF.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
34 changes: 25 additions & 9 deletions src/Rules/CPForCNPJ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading
Loading