Skip to content

Melhorias de leitura nos códigos #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2019
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
85 changes: 10 additions & 75 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,40 @@

use Illuminate\Validation\Validator as BaseValidator;

use function preg_match;
use function preg_replace;
use function strlen;
use function str_repeat;
use function sprintf;
use function substr;

/**
*
* @author Daniel Rodrigues Lima
* @email [email protected]
*/
class Validator extends BaseValidator
{
/**
* Valida o formato do cpf
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateFormatoCpf($attribute, $value)
{
return preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $value) > 0;
}

/**
* Valida o formato do cnpj
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateFormatoCnpj($attribute, $value)
{
return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0;
}

/**
* Valida o formato do cpf ou cnpj
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateFormatoCpfCnpj($attribute, $value)
{
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
}

/**
* Valida o formato do PIS/PASEP/NIS/NIT
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateFormatoNis($attribute, $value)
{
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
}

/**
* Valida CPF
* @param string $attribute
* @param string $value
* @return boolean
*/

protected function validateCpf($attribute, $value)
{
$c = preg_replace('/\D/', '', $value);
Expand All @@ -85,12 +61,6 @@ protected function validateCpf($attribute, $value)
return true;
}

/**
* Valida CNPJ
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateCnpj($attribute, $value)
{
$c = preg_replace('/\D/', '', $value);
Expand All @@ -116,28 +86,16 @@ protected function validateCnpj($attribute, $value)
return true;
}

/**
* Valida CNPJ ou CPF
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateCpfCnpj($attribute, $value)
{
return ($this->validateCpf($attribute, $value) || $this->validateCnpj($attribute, $value));
}

/**
* Valida CNH
* @param string $attribute
* @param string $value
* @return boolean
* Trecho retirado do respect validation
*/

protected function validateCnh($attribute, $value)
{
// Trecho retirado do respect validation

$ret = false;

if ((strlen($input = preg_replace('/[^\d]/', '', $value)) == 11)
Expand All @@ -146,22 +104,16 @@ protected function validateCnh($attribute, $value)
$dsc = 0;

for ($i = 0, $j = 9, $v = 0; $i < 9; ++$i, --$j) {

$v += (int)$input[$i] * $j;

$v += (int) $input[$i] * $j;
}

if (($vl1 = $v % 11) >= 10) {

$vl1 = 0;
$dsc = 2;

}

for ($i = 0, $j = 1, $v = 0; $i < 9; ++$i, ++$j) {

$v += (int)$input[$i] * $j;

$v += (int) $input[$i] * $j;
}

$vl2 = ($x = ($v % 11)) >= 10 ? 0 : $x - $dsc;
Expand All @@ -172,16 +124,8 @@ protected function validateCnh($attribute, $value)
return $ret;
}

/**
* Valida Titulo de Eleitor
* @param string $attribute
* @param string $value
* @return boolean
*/

protected function validateTituloEleitor($attribute, $value)
{

$input = preg_replace('/[^\d]/', '', $value);

$uf = substr($input, -4, 2);
Expand Down Expand Up @@ -226,21 +170,13 @@ protected function validateTituloEleitor($attribute, $value)
switch ($i) {
case '0':
$sequencia = $uf . $digito;

break;
}
}

return true;
}

/**
* Valida PIS/PASEP/NIS/NIT
* @param string $attribute
* @param string $value
* @return boolean
*/

protected function validateNis($attribute, $value)
{
$nis = sprintf('%011s', preg_replace('{\D}', '', $value));
Expand All @@ -255,5 +191,4 @@ protected function validateNis($attribute, $value)

return ($nis[10] == (((10 * $d) % 11) % 10));
}

}
4 changes: 1 addition & 3 deletions src/validator-docs/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ValidatorProvider extends ServiceProvider

public function boot()
{

$me = $this;

$this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $attributes) use ($me) {
Expand Down Expand Up @@ -66,7 +65,6 @@ public function register()
*/
public function provides()
{
return array();
return [];
}

}