-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cria classes para validaçao de dados
- CPF - CNPJ
- Loading branch information
1 parent
8960baf
commit 2beb3b9
Showing
7 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
upload/system/library/PagSeguro/src/Validation/Factory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Validation; | ||
|
||
class Factory | ||
{ | ||
const RULES_NAMESPACE = '\\ValdeirPsr\\PagSeguro\\Validation\\Rules\\'; | ||
|
||
private static $defaultInstance; | ||
|
||
/** | ||
* Captura a intancia atual do objeto | ||
*/ | ||
public static function getDefaultInstance() | ||
{ | ||
if (self::$defaultInstance === null) { | ||
self::$defaultInstance = new self(); | ||
} | ||
|
||
return self::$defaultInstance; | ||
} | ||
|
||
/** | ||
* Instancia, caso exista, a regra informada | ||
* | ||
* @param string $name regra | ||
* @param string $args | ||
* | ||
* @throws \UnexpectedValueException Caso a regra não exista | ||
* @throws \BadMethodCallException Caso a classe da regra não seja instanciável | ||
* | ||
* @return IValidation | ||
*/ | ||
public function rule($name, $args) | ||
{ | ||
$className = self::RULES_NAMESPACE . ucfirst($name); | ||
|
||
if (!class_exists($className)) { | ||
throw new \UnexpectedValueException("Rule {$className} not found", 2000); | ||
} | ||
|
||
$reflection = new \ReflectionClass($className); | ||
|
||
if (!$reflection->isInstantiable()) { | ||
throw new \BadMethodCallException("Class $className is not instantiable", 2001); | ||
} | ||
|
||
if ($reflection->getConstructor() !== null) { | ||
return $reflection->newInstanceArgs($args); | ||
} else { | ||
return $reflection->newInstanceWithoutConstructor(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
upload/system/library/PagSeguro/src/Validation/Rules/Cnpj.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Validation\Rules; | ||
|
||
/** | ||
* Valida um número de Cnpj | ||
* | ||
* @link https://github.com/Respect/Validation/blob/master/library/Rules/Cnpj.php | ||
*/ | ||
class Cnpj implements IValidation | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($input): bool | ||
{ | ||
if (!is_scalar($input)) { | ||
return false; | ||
} | ||
|
||
// Code ported from jsfromhell.com | ||
$bases = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
$digits = $this->getDigits((string) $input); | ||
|
||
if (array_sum($digits) < 1) { | ||
return false; | ||
} | ||
|
||
if (count($digits) !== 14) { | ||
return false; | ||
} | ||
|
||
$n = 0; | ||
for ($i = 0; $i < 12; ++$i) { | ||
$n += $digits[$i] * $bases[$i + 1]; | ||
} | ||
|
||
if ($digits[12] != (($n %= 11) < 2 ? 0 : 11 - $n)) { | ||
return false; | ||
} | ||
|
||
$n = 0; | ||
for ($i = 0; $i <= 12; ++$i) { | ||
$n += $digits[$i] * $bases[$i]; | ||
} | ||
|
||
$check = ($n %= 11) < 2 ? 0 : 11 - $n; | ||
|
||
return $digits[13] == $check; | ||
} | ||
|
||
/** | ||
* @return int[] | ||
*/ | ||
private function getDigits(string $input): array | ||
{ | ||
return array_map( | ||
'intval', | ||
str_split( | ||
(string) preg_replace('/\D/', '', $input) | ||
) | ||
); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
upload/system/library/PagSeguro/src/Validation/Rules/Cpf.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Validation\Rules; | ||
|
||
/** | ||
* Valida um número de CPF | ||
* | ||
* @link https://github.com/Respect/Validation/blob/master/library/Rules/Cpf.php | ||
*/ | ||
class Cpf implements IValidation | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($input) | ||
{ | ||
// Code ported from jsfromhell.com | ||
$c = preg_replace('/\D/', '', $input); | ||
|
||
if (strlen($c) != 11 || preg_match('/^'.$c[0].'{11}$/', $c) || $c === '01234567890') { | ||
return false; | ||
} | ||
|
||
$n = 0; | ||
for ($s = 10, $i = 0; $s >= 2; ++$i, --$s) { | ||
$n += $c[$i] * $s; | ||
} | ||
|
||
if ($c[9] != (($n %= 11) < 2 ? 0 : 11 - $n)) { | ||
return false; | ||
} | ||
|
||
$n = 0; | ||
for ($s = 11, $i = 0; $s >= 2; ++$i, --$s) { | ||
$n += $c[$i] * $s; | ||
} | ||
|
||
$check = ($n %= 11) < 2 ? 0 : 11 - $n; | ||
|
||
return $c[10] == $check; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
upload/system/library/PagSeguro/src/Validation/Rules/IValidation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Validation\Rules; | ||
|
||
interface IValidation | ||
{ | ||
/** | ||
* Valida uma informação | ||
* | ||
* @return bool | ||
*/ | ||
public function validate($value); | ||
} |
11 changes: 11 additions & 0 deletions
11
upload/system/library/PagSeguro/src/Validation/Validator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Validation; | ||
|
||
class Validator | ||
{ | ||
public static function __callStatic($name, $args) | ||
{ | ||
return Factory::getDefaultInstance()->rule($name, $args); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
upload/system/library/PagSeguro/tests/unit/Validation/Rules/CnpjTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Validation\Validator; | ||
|
||
class CnpjTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerValid | ||
* @test | ||
*/ | ||
public function CheckValidArguments($input) | ||
{ | ||
$v = Validator::Cnpj()->validate($input); | ||
$this->assertTrue($v); | ||
} | ||
|
||
/** | ||
* @dataProvider providerInvalid | ||
* @test | ||
*/ | ||
public function CheckInvalidArguments($input) | ||
{ | ||
$v = Validator::Cnpj()->validate($input); | ||
$this->assertFalse($v); | ||
} | ||
|
||
public function providerValid() | ||
{ | ||
return [ | ||
["00.000.000/0001-91"], | ||
["23.572.852/0001-59"], | ||
["27.697.5010001-25"], | ||
["27.697.501/000125"], | ||
]; | ||
} | ||
|
||
public function providerInvalid() | ||
{ | ||
return [ | ||
["37.887.406/0001-49"], | ||
["38.887.406/0001-48"] | ||
]; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
upload/system/library/PagSeguro/tests/unit/Validation/Rules/CpfTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Validation\Validator; | ||
|
||
class CpfTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerValid | ||
* @test | ||
*/ | ||
public function CheckValidArguments($input) | ||
{ | ||
$v = Validator::cpf()->validate($input); | ||
$this->assertTrue($v); | ||
} | ||
|
||
/** | ||
* @dataProvider providerInvalid | ||
* @test | ||
*/ | ||
public function CheckInvalidArguments($input) | ||
{ | ||
$v = Validator::cpf()->validate($input); | ||
$this->assertFalse($v); | ||
} | ||
|
||
public function providerValid() | ||
{ | ||
return [ | ||
["641.155.660-19"], | ||
["64115566019"], | ||
["641.155.66019"], | ||
["641.15566019"], | ||
]; | ||
} | ||
|
||
public function providerInvalid() | ||
{ | ||
return [ | ||
["641.155.660-20"], | ||
["64115566020"], | ||
["641.155.66020"], | ||
["641.15566020"], | ||
]; | ||
} | ||
} |