Skip to content

Commit b6c788b

Browse files
committed
First commit
0 parents  commit b6c788b

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
vendor/

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "codeedu/code_validator",
3+
"description": "Lib to validator things",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Luiz",
9+
"email": "argentinaluiz@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"zendframework/zend-validator": "^2.8"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Code\\": "src/"
18+
}
19+
}
20+
}

composer.lock

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Validator/Cnpj.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Code\Validator;
4+
5+
/**
6+
* Description of Cnpj
7+
*
8+
* @author Luiz Carlos
9+
*/
10+
class Cnpj extends DocumentNumberAbstract {
11+
12+
/**
13+
* Tamanho do Campo
14+
* @var int
15+
*/
16+
protected $size = 14;
17+
18+
/**
19+
* Modificadores de Dígitos
20+
* @var array
21+
*/
22+
protected $modifiers = [
23+
[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2],
24+
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
25+
];
26+
27+
}

src/Validator/Cpf.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Code\Validator;
4+
5+
class Cpf extends DocumentNumberAbstract {
6+
7+
/**
8+
* Tamanho do Campo
9+
* @var int
10+
*/
11+
protected $size = 11;
12+
13+
/**
14+
* Modificadores de Dígitos
15+
* @var array
16+
*/
17+
protected $modifiers = [
18+
[10, 9, 8, 7, 6, 5, 4, 3, 2],
19+
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
20+
];
21+
22+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Code\Validator;
4+
5+
use Zend\Validator\AbstractValidator;
6+
7+
/**
8+
* Classe DocumentNumberAbstract para validar tanto cpf quanto cnpj.
9+
* Existem no pacote as classes Cpf e Cnpj que a extendem.
10+
*/
11+
abstract class DocumentNumberAbstract extends AbstractValidator {
12+
13+
/**
14+
* Tamanho Inválido
15+
* @var string
16+
*/
17+
const SIZE = 'size';
18+
19+
/**
20+
* Números Expandidos
21+
* @var string
22+
*/
23+
const EXPANDED = 'expanded';
24+
25+
/**
26+
* Dígito Verificador
27+
* @var string
28+
*/
29+
const DIGIT = 'digit';
30+
31+
/**
32+
* Tamanho do Campo
33+
* @var int
34+
*/
35+
protected $size = 0;
36+
37+
38+
/**
39+
* Modelos de Mensagens
40+
* @var string
41+
*/
42+
protected $messageTemplates = [
43+
self::SIZE => "'%value%' não possui tamanho esperado.",
44+
self::EXPANDED => "'%value%' não possui um formato aceitável.",
45+
self::DIGIT => "'%value%' não é um documento válido."
46+
];
47+
48+
/**
49+
* Modificadores de Dígitos
50+
* @var array
51+
*/
52+
protected $modifiers = array();
53+
protected $validIfEmpty = true;
54+
55+
public function __construct($options = null) {
56+
parent::__construct($options);
57+
if (array_key_exists('valid_if_empty', $options))
58+
$this->validIfEmpty = $options['valid_if_empty'];
59+
}
60+
61+
/**
62+
* Validação Interna do Documento
63+
* @param string $value Dados para Validação
64+
* @return boolean Confirmação de Documento Válido
65+
*/
66+
protected function check($value) {
67+
// Captura dos Modificadores
68+
foreach ($this->modifiers as $modifier) {
69+
$result = 0; // Resultado Inicial
70+
$size = count($modifier); // Tamanho dos Modificadores
71+
for ($i = 0; $i < $size; $i++) {
72+
$result += $value[$i] * $modifier[$i]; // Somatório
73+
}
74+
$result = $result % 11;
75+
$digit = ($result < 2 ? 0 : 11 - $result); // Dígito
76+
// Verificação
77+
if ($value[$size] != $digit) {
78+
return false;
79+
}
80+
}
81+
return true;
82+
}
83+
84+
public function isValid($value) {
85+
if (!$this->validIfEmpty && empty($value)) {
86+
return true;
87+
}
88+
// Filtro de Dados
89+
$data = preg_replace('/[^0-9]/', '', $value);
90+
// Verificação de Tamanho
91+
if (strlen($data) != $this->size) {
92+
$this->error(self::SIZE, $value);
93+
return false;
94+
}
95+
// Verificação de Dígitos Expandidos
96+
if (str_repeat($data[0], $this->size) == $data) {
97+
$this->error(self::EXPANDED, $value);
98+
return false;
99+
}
100+
// Verificação de Dígitos
101+
if (!$this->check($data)) {
102+
$this->error(self::DIGIT, $value);
103+
return false;
104+
}
105+
// Comparações Concluídas
106+
return true; // Todas Verificações Executadas
107+
}
108+
109+
}

0 commit comments

Comments
 (0)