Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Sep 28, 2023
2 parents d72e005 + cdac886 commit e21c3c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
8 changes: 4 additions & 4 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
'document' => [
'cnpj' => [
'invalid' => 'The :attribute isn\'t a valid CNPJ.',
'size' => 'The :attribute must have 14 digits to be a valid CNPJ.'
'size' => 'The :attribute must have 14 digits to be a valid CNPJ.',
],
'cpf' => [
'invalid' => 'The :attribute isn\'t a valid CPF.',
'size' => 'The :attribute must have 11 digits to be a valid CPF.'
'size' => 'The :attribute must have 11 digits to be a valid CPF.',
],
'generic' => [
'invalid' => 'The :attribute isn\'t a valid CNPJ or CPF.',
'size' => 'The :attribute must have 11 or 14 digits to be a valid CNPJ or CPF.'
]
'size' => 'The :attribute must have 11 or 14 digits to be a valid CNPJ or CPF.',
],
],
'phone' => [
'generic' => 'The :attribute isn\'t a valid phone.',
Expand Down
9 changes: 4 additions & 5 deletions resources/lang/pt_BR/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
'document' => [
'cnpj' => [
'invalid' => 'O campo :attribute não é um CNPJ válido.',
'size' => 'O campo :attribute deve ter 14 dígitos para ser um CNPJ válido.'
'size' => 'O campo :attribute deve ter 14 dígitos para ser um CNPJ válido.',
],
'cpf' => [
'invalid' => 'O campo :attribute não é um CPF válido.',
'size' => 'O campo :attribute deve ter 11 dígitos para ser um CPF válido.'
'size' => 'O campo :attribute deve ter 11 dígitos para ser um CPF válido.',
],
'generic' => [
'invalid' => 'O campo :attribute não é um CNPJ ou CPF válido.',
'size' => 'O campo :attribute deve ter 11 dígitos para CPF ou 14 dígitos para CNPJ.'
]
'size' => 'O campo :attribute deve ter 11 dígitos para CPF ou 14 dígitos para CNPJ.',
],
],
'phone' => [
'generic' => 'O campo :attribute não é um telefone válido.',
Expand All @@ -32,5 +32,4 @@
'public_services' => 'O campo :attribute não é um telefone de serviços públicos válido.',
],


];
8 changes: 5 additions & 3 deletions src/Rules/pt_BR/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

readonly class Document implements ValidationRule
{

public function __construct(
public bool $allowCNPJ = true,
public bool $allowCPF = true,
) {
if ( ! $this->allowCNPJ && ! $this->allowCPF) {
if (! $this->allowCNPJ && ! $this->allowCPF) {
throw new \InvalidArgumentException('You must allow at least one document type.');
}
}
Expand All @@ -39,8 +38,9 @@ public static function generic(): self
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!is_string($value)) {
if (! is_string($value)) {
$fail(__('validation.string'));

return;
}
$value = preg_replace('/[^0-9]/i', '', $value);
Expand Down Expand Up @@ -86,6 +86,7 @@ protected function validateCnpj(string $value): bool
if ($value[13] != ($rest < 2 ? 0 : 11 - $rest)) {
return false;
}

return true;
}

Expand All @@ -100,6 +101,7 @@ protected function validateCpf(string $value): bool
return false;
}
}

return true;
}
}
17 changes: 9 additions & 8 deletions src/Rules/pt_BR/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

readonly class Phone implements ValidationRule
{

public function __construct(
public bool $allowCellphone = true,
public bool $allowLocalFare = true,
public bool $allowNonRegional = true,
public bool $allowPhone = true,
public bool $allowPublicServices = true,
) {
if ( ! $allowCellphone && ! $allowLocalFare && ! $allowNonRegional && ! $allowPhone && ! $allowPublicServices) {
if (! $allowCellphone && ! $allowLocalFare && ! $allowNonRegional && ! $allowPhone && ! $allowPublicServices) {
throw new \InvalidArgumentException('At least one of the options must be true.');
}
}
Expand Down Expand Up @@ -57,8 +56,9 @@ public static function publicServices(): self
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!is_string($value)) {
if (! is_string($value)) {
$fail(__('validation.string'));

return;
}
$value = preg_replace('/[^0-9]/i', '', $value);
Expand Down Expand Up @@ -119,21 +119,22 @@ protected function validatePublicServices(string $value): string|true

protected function validateGeneric(string $value): string|true
{
if (preg_match('/^[1-9][0-9]9$/', substr($value, 0, 3)) === 1) {
if (preg_match('/^[1-9][0-9]9$/', substr($value, 0, 3)) === 1) {
return $this->validateCellphone($value);
}
if (preg_match('/^400$/', substr($value, 0, 3)) === 1) {
if (preg_match('/^400$/', substr($value, 0, 3)) === 1) {
return $this->validateLocalFare($value);
}
if (preg_match('/^0[3589]00$/', substr($value, 0, 4)) === 1) {
if (preg_match('/^0[3589]00$/', substr($value, 0, 4)) === 1) {
return $this->validateNonRegional($value);
}
if (preg_match('/^[1-9][0-9][1-5][0-9]$/', substr($value, 0, 4)) === 1) {
if (preg_match('/^[1-9][0-9][1-5][0-9]$/', substr($value, 0, 4)) === 1) {
return $this->validatePhone($value);
}
if (preg_match('/^1[0-9]{2}$/', substr($value, 0, 3)) === 1) {
if (preg_match('/^1[0-9]{2}$/', substr($value, 0, 3)) === 1) {
return $this->validatePublicServices($value);
}

return __('laravuewind::validation.phone.generic');
}
}

0 comments on commit e21c3c4

Please sign in to comment.