From 2beb3b97b66a9ecbac7eaf40ad1bd5d727f56054 Mon Sep 17 00:00:00 2001 From: Valdeir S Date: Sat, 8 Aug 2020 16:53:16 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20cria=20classes=20para=20valida=C3=A7ao?= =?UTF-8?q?=20de=20dados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CPF - CNPJ --- .../PagSeguro/src/Validation/Factory.php | 54 ++++++++++++++++ .../PagSeguro/src/Validation/Rules/Cnpj.php | 64 +++++++++++++++++++ .../PagSeguro/src/Validation/Rules/Cpf.php | 42 ++++++++++++ .../src/Validation/Rules/IValidation.php | 13 ++++ .../PagSeguro/src/Validation/Validator.php | 11 ++++ .../tests/unit/Validation/Rules/CnpjTest.php | 45 +++++++++++++ .../tests/unit/Validation/Rules/CpfTest.php | 47 ++++++++++++++ 7 files changed, 276 insertions(+) create mode 100644 upload/system/library/PagSeguro/src/Validation/Factory.php create mode 100644 upload/system/library/PagSeguro/src/Validation/Rules/Cnpj.php create mode 100644 upload/system/library/PagSeguro/src/Validation/Rules/Cpf.php create mode 100644 upload/system/library/PagSeguro/src/Validation/Rules/IValidation.php create mode 100644 upload/system/library/PagSeguro/src/Validation/Validator.php create mode 100644 upload/system/library/PagSeguro/tests/unit/Validation/Rules/CnpjTest.php create mode 100644 upload/system/library/PagSeguro/tests/unit/Validation/Rules/CpfTest.php diff --git a/upload/system/library/PagSeguro/src/Validation/Factory.php b/upload/system/library/PagSeguro/src/Validation/Factory.php new file mode 100644 index 0000000..a1c3d0a --- /dev/null +++ b/upload/system/library/PagSeguro/src/Validation/Factory.php @@ -0,0 +1,54 @@ +isInstantiable()) { + throw new \BadMethodCallException("Class $className is not instantiable", 2001); + } + + if ($reflection->getConstructor() !== null) { + return $reflection->newInstanceArgs($args); + } else { + return $reflection->newInstanceWithoutConstructor(); + } + } +} diff --git a/upload/system/library/PagSeguro/src/Validation/Rules/Cnpj.php b/upload/system/library/PagSeguro/src/Validation/Rules/Cnpj.php new file mode 100644 index 0000000..2d13999 --- /dev/null +++ b/upload/system/library/PagSeguro/src/Validation/Rules/Cnpj.php @@ -0,0 +1,64 @@ +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) + ) + ); + } +} diff --git a/upload/system/library/PagSeguro/src/Validation/Rules/Cpf.php b/upload/system/library/PagSeguro/src/Validation/Rules/Cpf.php new file mode 100644 index 0000000..566bc19 --- /dev/null +++ b/upload/system/library/PagSeguro/src/Validation/Rules/Cpf.php @@ -0,0 +1,42 @@ += 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; + } +} diff --git a/upload/system/library/PagSeguro/src/Validation/Rules/IValidation.php b/upload/system/library/PagSeguro/src/Validation/Rules/IValidation.php new file mode 100644 index 0000000..b8f7b64 --- /dev/null +++ b/upload/system/library/PagSeguro/src/Validation/Rules/IValidation.php @@ -0,0 +1,13 @@ +rule($name, $args); + } +} diff --git a/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CnpjTest.php b/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CnpjTest.php new file mode 100644 index 0000000..c8c1f5b --- /dev/null +++ b/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CnpjTest.php @@ -0,0 +1,45 @@ +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"] + ]; + } +} diff --git a/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CpfTest.php b/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CpfTest.php new file mode 100644 index 0000000..2d4ee9c --- /dev/null +++ b/upload/system/library/PagSeguro/tests/unit/Validation/Rules/CpfTest.php @@ -0,0 +1,47 @@ +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"], + ]; + } +}