Validate_ptBR
[ class tree: Validate_ptBR ] [ index: Validate_ptBR ] [ all elements ]

Source for file ptBR.php

Documentation is available at ptBR.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Specific validation methods for data used in Brazil
  5.  *
  6.  * PHP Versions 5
  7.  *
  8.  * This source file is subject to the New BSD license, That is bundled
  9.  * with this package in the file LICENSE, and is available through
  10.  * the world-wide-web at
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the new BSDlicense and are unable
  13.  * to obtain it through the world-wide-web, please send a note to
  14.  * pajoye@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category  Validate
  17.  * @package   Validate_ptBR
  18.  * @author    Silvano Girardi Jr. <silvano@php.net>
  19.  * @author    Marcelo Santos Araujo <msaraujo@php.net>
  20.  * @copyright 1997-2005  Silvano Girardi Jr.
  21.  * @license   http://www.opensource.org/licenses/bsd-license.php  new BSD
  22.  * @version   CVS: $Id: ptBR.php,v 1.18 2008/11/27 00:33:19 ifeghali Exp $
  23.  * @link      http://pear.php.net/package/Validate_ptBR
  24.  */
  25.  
  26. /**
  27.  * Data validation class for Brazil
  28.  *
  29.  * This class provides methods to validate:
  30.  *  - Postal code CEP (Código de Endereçamento Postal)
  31.  *  - CPF (Cadastro de Pessoa Física)
  32.  *  - CNPJ (Cadastro Nacional de Pessoa Jurídica)
  33.  *  - Regions - brazilian states (Estados brasileiros)
  34.  *  - Phone Numbers - brazilian phone numbers
  35.  *  - Vehicle Plates - brazilian vehicle's plate
  36.  * 
  37.  * @category  Validate
  38.  * @package   Validate_ptBR
  39.  * @author    Silvano Girardi Jr. <silvano@php.net>
  40.  * @author    Marcelo Santos Araujo <msaraujo@php.net>
  41.  * @copyright 1997-2005  Silvano Girardi Jr.
  42.  * @license   http://www.opensource.org/licenses/bsd-license.php  new BSD
  43.  * @version   Release: @package_version@
  44.  * @link      http://pear.php.net/package/Validate_ptBR
  45.  */
  46. {
  47.     /**
  48.      * Validate CEP (Código de Endereçamento Postal, like postcode in US
  49.      * and other languages)
  50.      * format: xxxxx-xxx,xxxxx xxx,xxxxxxxx
  51.      *
  52.      * @param string $postalCode pt_BR CEP/postalCode to validate
  53.      * @param bool   $strong     optional; strong checks (e.g. against a list
  54.      *                            of postcodes) (not implanted)
  55.      *
  56.      * @return  bool true if $cep is ok, false otherwise
  57.      */
  58.     public static function postalCode($postalCode$strong = false)
  59.     {
  60.          return (bool) preg_match('/^([0-9]{2}\.?[0-9]{3})[- ]?([0-9]{3})$/'addcslashes($postalCode"\n"));
  61.     }
  62.  
  63.     /**
  64.      * Validade CPF (Cadastro de Pessoa Física)
  65.      *
  66.      * @param string $cpf CPF to validate
  67.      *
  68.      * @return  bool true if $cpf is ok, false otherwise
  69.      */
  70.     public static function cpf($cpf)
  71.     {
  72.         if(!preg_match("/^\d{3}\.?\d{3}\.?\d{3}\.?-?\d{2}$/"$cpf)) {  
  73.            return false;
  74.         }
  75.  
  76.         $cpf preg_replace("/[^\d]/"''$cpf);
  77.  
  78.         if (strlen($cpf!= 11{
  79.             return false;
  80.         elseif (in_array($cpfarray("00000000000""11111111111",
  81.                                        "22222222222""33333333333",
  82.                                        "44444444444""55555555555",
  83.                                        "66666666666""77777777777",
  84.                                        "88888888888""99999999999"))) {
  85.             return false;
  86.         else {
  87.             $number[0]  intval(substr($cpf01));
  88.             $number[1]  intval(substr($cpf11));
  89.             $number[2]  intval(substr($cpf21));
  90.             $number[3]  intval(substr($cpf31));
  91.             $number[4]  intval(substr($cpf41));
  92.             $number[5]  intval(substr($cpf51));
  93.             $number[6]  intval(substr($cpf61));
  94.             $number[7]  intval(substr($cpf71));
  95.             $number[8]  intval(substr($cpf81));
  96.             $number[9]  intval(substr($cpf91));
  97.             $number[10intval(substr($cpf101));
  98.  
  99.             $sum = 10*$number[0]+9*$number[1]+8*$number[2]+7*$number[3]+
  100.                 6*$number[4]+5*$number[5]+4*$number[6]+3*$number[7]+
  101.                 2*$number[8];
  102.  
  103.             $sum -= (11*(intval($sum/11)));
  104.  
  105.             if ($sum == 0 || $sum == 1{
  106.                 $result1 = 0;
  107.             else {
  108.                 $result1 = 11 - $sum;
  109.             }
  110.  
  111.             if ($result1 == $number[9]{
  112.                 $sum  $number[0]*11+$number[1]*10+$number[2]*9+$number[3]*8+
  113.                     $number[4]*7+$number[5]*6+$number[6]*5+$number[7]*4+
  114.                     $number[8]*3+$number[9]*2;
  115.                 $sum -= (11*(intval($sum/11)));
  116.  
  117.                 if ($sum == 0 || $sum == 1{
  118.                     $result2 = 0;
  119.                 else {
  120.                     $result2 = 11-$sum;
  121.                 }
  122.  
  123.                 if ($result2 == $number[10]{
  124.                     return true;
  125.                 else {
  126.                     return false;
  127.                 }
  128.             else {
  129.                 return false;
  130.             }
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * Validade CNPJ (Cadastro Nacional de Pessoa Jurídica)
  136.      *
  137.      * @param string $cnpj CNPJ to validate
  138.      *
  139.      * @return  bool true if $cnpj is ok, false otherwise
  140.      */
  141.     public static function cnpj($cnpj)
  142.     {
  143.         $cnpj addcslashes($cnpj"\n");
  144.        
  145.         // Valid Format
  146.         if(!preg_match("/^\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}$/"$cnpj))
  147.         {
  148.             return false;
  149.          }
  150.  
  151.         // Clear != digit
  152.         $cnpj preg_replace("/[^\d]/"''$cnpj);
  153.  
  154.         if (strlen($cnpj!= 14{
  155.             return false;
  156.         elseif ($cnpj == '00000000000000'{
  157.             return false;
  158.         else {
  159.             $number[0]  intval(substr($cnpj01));
  160.             $number[1]  intval(substr($cnpj11));
  161.             $number[2]  intval(substr($cnpj21));
  162.             $number[3]  intval(substr($cnpj31));
  163.             $number[4]  intval(substr($cnpj41));
  164.             $number[5]  intval(substr($cnpj51));
  165.             $number[6]  intval(substr($cnpj61));
  166.             $number[7]  intval(substr($cnpj71));
  167.             $number[8]  intval(substr($cnpj81));
  168.             $number[9]  intval(substr($cnpj91));
  169.             $number[10intval(substr($cnpj101));
  170.             $number[11intval(substr($cnpj111));
  171.             $number[12intval(substr($cnpj121));
  172.             $number[13intval(substr($cnpj131));
  173.  
  174.             $sum $number[0]*5+$number[1]*4+$number[2]*3+$number[3]*2+
  175.                 $number[4]*9+$number[5]*8+$number[6]*7+$number[7]*6+
  176.                 $number[8]*5+$number[9]*4+$number[10]*3+$number[11]*2;
  177.  
  178.             $sum -= (11*(intval($sum/11)));
  179.  
  180.             if ($sum == 0 || $sum == 1{
  181.                 $result1 = 0;
  182.             else {
  183.                 $result1 = 11-$sum;
  184.             }
  185.  
  186.             if ($result1 == $number[12]{
  187.                 $sum  $number[0]*6+$number[1]*5+$number[2]*4+$number[3]*3+
  188.                     $number[4]*2+$number[5]*9+$number[6]*8+$number[7]*7+
  189.                     $number[8]*6+$number[9]*5+$number[10]*4+$number[11]*3+
  190.                     $number[12]*2;
  191.                 $sum -= (11*(intval($sum/11)));
  192.                 if ($sum == 0 || $sum == 1{
  193.                     $result2 = 0;
  194.                 else {
  195.                     $result2 = 11-$sum;
  196.                 }
  197.  
  198.                 if ($result2 == $number[13]{
  199.                     return true;
  200.                 else {
  201.                     return false;
  202.                 }
  203.             else {
  204.                 return false;
  205.             }
  206.         }
  207.     }
  208.  
  209.     /**
  210.      * Validates a "region" (i.e. state) code
  211.      *
  212.      * @param string $region 2-letter state code
  213.      *
  214.      * @return bool  true if $region is ok, false otherwise
  215.      * @static
  216.      */
  217.     public static function region($region)
  218.     {
  219.         switch (strtoupper($region)) {
  220.         case 'AC':
  221.         case 'AP':
  222.         case 'AL':
  223.         case 'AM':
  224.         case 'BA':
  225.         case 'CE':
  226.         case 'DF':
  227.         case 'ES':
  228.         case 'GO':
  229.         case 'MA':
  230.         case 'MT':
  231.         case 'MS':
  232.         case 'MG':
  233.         case 'PA':
  234.         case 'PB':
  235.         case 'PR':
  236.         case 'PE':
  237.         case 'PI':
  238.         case 'RJ':
  239.         case 'RN':
  240.         case 'RS':
  241.         case 'RO':
  242.         case 'RR':
  243.         case 'SC':
  244.         case 'SP':
  245.         case 'SE':
  246.         case 'TO':
  247.             return true;
  248.         }
  249.         return false;
  250.     }
  251.  
  252.  
  253.     /**
  254.      * Validates a brazilian (ptBR) phone number.
  255.      * Also allows the formats
  256.      * If $requiredAreaCode is true:
  257.      * (XX)-XXXX-XXXX,(XX) XXXX XXXX, (XX)-XXXX XXXX, (XX) XXXX-XXXX,
  258.      * XX-XXXX-XXXX,XX XXXX XXXX,XX-XXXX XXXX,XX XXXX-XXXX,XX XXXXXXXX,(XX)XXXXXXXX
  259.      * If $requiredAreaCode is false:  XXXX-XXXX,XXXX XXXX, XXXXXXXX
  260.      *
  261.      * @param string $number          phone to validate
  262.      * @param bool   $requireAreaCode require the area code? (default: true)
  263.      *
  264.      * @return bool The valid or invalid phone number
  265.      */
  266.     public static function phoneNumber($number$requireAreaCode = true)
  267.     {
  268.         $number addcslashes($number"\n");
  269.         if (!$requireAreaCode{
  270.             if (preg_match("/^(\d{4})[- ]?(\d{4})$/"$number)) {
  271.                 return  true;
  272.             }
  273.         else {
  274.             $exp "/^(\()?[1-9]{2}(?(1)\))[- ]?(\d{4})[- ]?(\d{4})$/";
  275.             if (preg_match($exp$number)) {
  276.                 return true;
  277.             }
  278.         }
  279.         return false;
  280.     }
  281.  
  282.     /**
  283.      * Validates a brazilian (ptBR) vehicle's plate
  284.      * Also allows the following formats
  285.      * XXX-XXXX,XXX XXXX,XXXXXXX
  286.      * The first three chars are [A-Z]
  287.      * The last four chars are [0-9]
  288.      *
  289.      * @param string $reg vehicle's plate
  290.      *
  291.      * @return bool 
  292.      */
  293.     public static function carReg($reg)
  294.     {
  295.         return (bool) preg_match('/^[A-Z]{3}[- ]?[0-9]{4}$/'addcslashes($reg"\n"));
  296.     }
  297.  
  298.  
  299.  
  300.  
  301.     /**
  302.      * Validate PIS (Programa de Integracao Social)
  303.      * Allows the following formats
  304.      * XXX.XXX.XXX-XX, XXXXXXXXXXX or
  305.      * any combination with dots and/or hyphens
  306.      *
  307.      * @param string $pis pis code
  308.      *
  309.      * @return bool 
  310.      */
  311.     public static function pis($pis)
  312.     {
  313.         static $sum = 0;
  314.  
  315.         if (strlen($pis< 11{
  316.             return false;
  317.         }
  318.  
  319.         $pis = str_replace(array("-",".")""$pis);
  320.  
  321.         $multiplier = array(3,2,9,8,7,6,5,4,3,2);
  322.  
  323.         for ($i = 0; $i < 10; $i++{   
  324.             $sum += (int) $pis[$i$multiplier[$i];
  325.         }
  326.  
  327.         $mod $sum % 11;
  328.  
  329.         $mod ($mod < 2)  ? 0 : 11 - $mod;  
  330.  
  331.         return (int) $pis[10=== $mod);
  332.  
  333.     }
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. }
  341. ?>

Documentation generated on Wed, 26 Nov 2008 20:00:06 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.