Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TCPFValidator.php

Documentation is available at TCPFValidator.php

  1. <?php
  2. namespace Adianti\Validator;
  3.  
  4. use Adianti\Validator\TFieldValidator;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Exception;
  7.  
  8. /**
  9.  * CPF validation (Valid only in Brazil)
  10.  *
  11.  * @version    7.4
  12.  * @package    validator
  13.  * @author     Pablo Dall'Oglio
  14.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  15.  * @license    http://www.adianti.com.br/framework-license
  16.  */
  17. {
  18.     /**
  19.      * Validate a given value
  20.      * @param $label Identifies the value to be validated in case of exception
  21.      * @param $value Value to be validated
  22.      * @param $parameters aditional parameters for validation
  23.      */
  24.     public function validate($label$value$parameters NULL)
  25.     {
  26.         // cpfs inválidos
  27.         $nulos array("12345678909","11111111111","22222222222","33333333333",
  28.                        "44444444444","55555555555","66666666666","77777777777",
  29.                        "88888888888","99999999999","00000000000");
  30.         // Retira todos os caracteres que nao sejam 0-9
  31.         $cpf preg_replace("/[^0-9]/"""$value);
  32.         
  33.         if (strlen($cpf<> 11)
  34.         {
  35.             throw new Exception(AdiantiCoreTranslator::translate('The field ^1 has not a valid CPF'$label));
  36.         }
  37.         
  38.         // Retorna falso se houver letras no cpf
  39.         if (!(preg_match("/[0-9]/",$cpf)))
  40.         {
  41.             throw new Exception(AdiantiCoreTranslator::translate('The field ^1 has not a valid CPF'$label));
  42.         }
  43.  
  44.         // Retorna falso se o cpf for nulo
  45.         ifin_array($cpf$nulos) )
  46.         {
  47.             throw new Exception(AdiantiCoreTranslator::translate('The field ^1 has not a valid CPF'$label));
  48.         }
  49.  
  50.         // Calcula o penúltimo dígito verificador
  51.         $acum=0;
  52.         for($i=0$i<9$i++)
  53.         {
  54.           $acum+= $cpf[$i]*(10-$i);
  55.         }
  56.  
  57.         $x=$acum 11;
  58.         $acum ($x>1(11 $x0;
  59.         // Retorna falso se o digito calculado eh diferente do passado na string
  60.         if ($acum != $cpf[9])
  61.         {
  62.           throw new Exception(AdiantiCoreTranslator::translate('The field ^1 has not a valid CPF'$label));
  63.         }
  64.         // Calcula o último dígito verificador
  65.         $acum=0;
  66.         for ($i=0$i<10$i++)
  67.         {
  68.           $acum+= $cpf[$i]*(11-$i);
  69.         }  
  70.  
  71.         $x=$acum 11;
  72.         $acum ($x 1(11-$x0;
  73.         // Retorna falso se o digito calculado eh diferente do passado na string
  74.         if $acum != $cpf[10])
  75.         {
  76.           throw new Exception(AdiantiCoreTranslator::translate('The field ^1 has not a valid CPF'$label));
  77.         }  
  78.     }
  79. }