Exemplo n.º 1
0
 /**
  * NIF and DNI validation
  * @param $nif string The NIF or NIE
  * @return bool
  */
 protected function validateNif($nif)
 {
     $nif_codes = 'TRWAGMYFPDXBNJZSQVHLCKE';
     $sum = (string) Utils::getCifSum($nif);
     $n = 10 - substr($sum, -1);
     if (preg_match('/^[0-9]{8}[A-Z]{1}$/', $nif)) {
         // DNIs
         $num = substr($nif, 0, 8);
         return $nif[8] == $nif_codes[$num % 23];
     } elseif (preg_match('/^[XYZ][0-9]{7}[A-Z]{1}$/', $nif)) {
         // NIEs normales
         $tmp = substr($nif, 1, 7);
         $tmp = strtr(substr($nif, 0, 1), 'XYZ', '012') . $tmp;
         return $nif[8] == $nif_codes[$tmp % 23];
     } elseif (preg_match('/^[KLM]{1}/', $nif)) {
         // NIFs especiales
         return $nif[8] == chr($n + 64);
     } elseif (preg_match('/^[T]{1}[A-Z0-9]{8}$/', $nif)) {
         // NIE extraño
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * CIFs validation
  * This is used to validate the CIF
  * @param $cif
  * @return bool
  */
 protected function validateCif($cif)
 {
     $cif_codes = 'JABCDEFGHI';
     $sum = (string) Utils::getCifSum($cif);
     $n = (10 - substr($sum, -1)) % 10;
     if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $cif)) {
         if (in_array($cif[0], array('A', 'B', 'E', 'H'))) {
             // Numerico
             return $cif[8] == $n;
         } elseif (in_array($cif[0], array('K', 'P', 'Q', 'S'))) {
             // Letras
             return $cif[8] == $cif_codes[$n];
         } else {
             // Alfanumérico
             if (is_numeric($cif[8])) {
                 return $cif[8] == $n;
             } else {
                 return $cif[8] == $cif_codes[$n];
             }
         }
     }
     return false;
 }