Created
September 18, 2013 15:13
-
-
Save viniciusss/6610637 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/** | |
* Validador para Cadastro de Pessoa Jurídica | |
* | |
* @author Wanderson Henrique Camargo Rosa | |
* @category Hazel | |
* @package Hazel_Validator | |
*/ | |
class MS_Validator_Cnpj extends MS_Validator_CpAbstract | |
{ | |
/** | |
* Tamanho do Campo | |
* @var int | |
*/ | |
protected $_size = 14; | |
/** | |
* Modificadores de Dígitos | |
* @var array | |
*/ | |
protected $_modifiers = array( | |
array(5,4,3,2,9,8,7,6,5,4,3,2), | |
array(6,5,4,3,2,9,8,7,6,5,4,3,2) | |
); | |
} |
This file contains hidden or 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
<?php | |
/** | |
* Validador para Cadastro de Pessoas | |
* | |
* Implementação de algoritmos para cadastro de pessoas físicas e jurídicas | |
* conforme Ministério da Fazenda do Governo Federal. | |
* | |
* @category Hazel | |
* @package Hazel_Validator | |
* @author Wanderson Henrique Camargo Rosa | |
*/ | |
abstract class MS_Validator_CpAbstract extends Zend_Validate_Abstract { | |
/** | |
* Tamanho Inválido | |
* @var string | |
*/ | |
const SIZE = 'size'; | |
/** | |
* Números Expandidos | |
* @var string | |
*/ | |
const EXPANDED = 'expanded'; | |
/** | |
* Dígito Verificador | |
* @var string | |
*/ | |
const DIGIT = 'digit'; | |
/** | |
* Tamanho do Campo | |
* @var int | |
*/ | |
protected $_size = 0; | |
/** | |
* Modelos de Mensagens | |
* @var string | |
*/ | |
protected $_messageTemplates = array( | |
self::SIZE => "'%value%' não possui tamanho esperado", | |
self::EXPANDED => "'%value%' não possui um formato aceitável", | |
self::DIGIT => "'%value%' não é um documento válido" | |
); | |
/** | |
* Modificadores de Dígitos | |
* @var array | |
*/ | |
protected $_modifiers = array(); | |
/** | |
* Validação Interna do Documento | |
* @param string $value Dados para Validação | |
* @return boolean Confirmação de Documento Válido | |
*/ | |
protected function _check($value) { | |
// Captura dos Modificadores | |
foreach ($this->_modifiers as $modifier) { | |
$result = 0; // Resultado Inicial | |
$size = count($modifier); // Tamanho dos Modificadores | |
for ($i = 0; $i < $size; $i++) { | |
$result += $value[$i] * $modifier[$i]; // Somatório | |
} | |
$result = $result % 11; | |
$digit = ($result < 2 ? 0 : 11 - $result); // Dígito | |
// Verificação | |
if ($value[$size] != $digit) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public function isValid($value) { | |
// Filtro de Dados | |
$data = preg_replace('/[^0-9]/', '', $value); | |
// Verificação de Tamanho | |
if (strlen($data) != $this->_size) { | |
$this->_error(self::SIZE, $value); | |
return false; | |
} | |
// Verificação de Dígitos Expandidos | |
if (str_repeat($data[0], $this->_size) == $data) { | |
$this->_error(self::EXPANDED, $value); | |
return false; | |
} | |
// Verificação de Dígitos | |
if (!$this->_check($data)) { | |
$this->_error(self::DIGIT, $value); | |
return false; | |
} | |
// Comparações Concluídas | |
return true; // Todas Verificações Executadas | |
} | |
} |
This file contains hidden or 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
<?php | |
/** | |
* Validador para Cadastro de Pessoa Física | |
* | |
* @author Wanderson Henrique Camargo Rosa | |
* @category Hazel | |
* @package Hazel_Validator | |
*/ | |
class MS_Validator_Cpf extends MS_Validator_CpAbstract | |
{ | |
/** | |
* Tamanho do Campo | |
* @var int | |
*/ | |
protected $_size = 11; | |
/** | |
* Modificadores de Dígitos | |
* @var array | |
*/ | |
protected $_modifiers = array( | |
array(10,9,8,7,6,5,4,3,2), | |
array(11,10,9,8,7,6,5,4,3,2) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment