|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Xinjia\SpainValidatorBundle\Validator; |
| 4 | + |
| 5 | +use Symfony\Component\Validator\Constraint; |
| 6 | +use Symfony\Component\Validator\ConstraintValidator; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class DniCifValidator |
| 10 | + * @package Xinjia\SpainValidatorBundle\Validator |
| 11 | + * @author Juanjo García <[email protected]> |
| 12 | + */ |
| 13 | +class DniCifValidator extends ConstraintValidator { |
| 14 | + |
| 15 | + private $dniFormatExpr = '/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/'; |
| 16 | + private $standardDniExpr = '/(^[0-9]{8}[A-Z]{1}$)/'; |
| 17 | + private $availableLastChar = 'TRWAGMYFPDXBNJZSQVHLCKE'; |
| 18 | + private $cifFormatExpr1 = '/^[ABEH][0-9]{8}$/i'; |
| 19 | + private $cifFormatExpr2 = '/^[KPQS][0-9]{7}[A-J]$/i'; |
| 20 | + private $cifFormatExpr3 = '/^[CDFGJLMNRUVW][0-9]{7}[0-9A-J]$/i'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Symfony\Component\Validator\Context\ExecutionContextInterface |
| 24 | + */ |
| 25 | + protected $context; |
| 26 | + |
| 27 | + /** |
| 28 | + * Checks if the passed value is valid. |
| 29 | + * |
| 30 | + * @param mixed $value The value that should be validated |
| 31 | + * @param Constraint $constraint The constraint for the validation |
| 32 | + * |
| 33 | + * @api |
| 34 | + */ |
| 35 | + public function validate($value, Constraint $constraint) { |
| 36 | + if (!$this->checkDni($value) && !$this->checkCif($value)) { |
| 37 | + $this->context->buildViolation($constraint->message) |
| 38 | + ->addViolation(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private function splitDni($dni) { |
| 43 | + return str_split($dni, 1); |
| 44 | + } |
| 45 | + |
| 46 | + protected function checkDniFormat($dni) { |
| 47 | + return preg_match($this->dniFormatExpr, $dni); |
| 48 | + } |
| 49 | + |
| 50 | + protected function isValidDniLastChar($dni) { |
| 51 | + $dniCharacters = $this->splitDni($dni); |
| 52 | + return ($dniCharacters[8] == substr($this->availableLastChar, substr($dni, 0, 8) % 23, 1)); |
| 53 | + } |
| 54 | + |
| 55 | + protected function checkStandardDni($dni) { |
| 56 | + // Check if standard DNI |
| 57 | + return (preg_match($this->standardDniExpr, $dni)) ? $this->isValidDniLastChar($dni) : false; |
| 58 | + } |
| 59 | + |
| 60 | + protected function checkSpecialDni($dni) { |
| 61 | + $dniCharacters = $this->splitDni($dni); |
| 62 | + |
| 63 | + $plus = $dniCharacters[2] + $dniCharacters[4] + $dniCharacters[6]; |
| 64 | + for ($i = 1; $i < 8; $i += 2) |
| 65 | + $plus += (int) substr((2 * $dniCharacters[$i]), 0, 1) + (int) substr((2 * $dniCharacters[$i]), 1, 1); |
| 66 | + |
| 67 | + $n = 10 - substr($plus, strlen($plus) - 1, 1); |
| 68 | + |
| 69 | + return (preg_match('/^[KLM]{1}/', $dni)) ? ($dniCharacters[8] == chr(64 + $n) || $this->isValidDniLastChar($dni)) : false; |
| 70 | + } |
| 71 | + |
| 72 | + protected function checkDni($dni) { |
| 73 | + $dni = strtoupper($dni); |
| 74 | + |
| 75 | + // Invalid format |
| 76 | + if (!$this->checkDniFormat($dni)) |
| 77 | + return false; |
| 78 | + |
| 79 | + return ($this->checkStandardDni($dni) || $this->checkSpecialDni($dni)); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * @param $cif |
| 85 | + * @return boolean |
| 86 | + */ |
| 87 | + protected function checkCifFormat($cif) { |
| 88 | + |
| 89 | + if (preg_match($this->cifFormatExpr1, $cif) || preg_match($this->cifFormatExpr2, $cif) || preg_match($this->cifFormatExpr3, $cif)) { |
| 90 | + |
| 91 | + $control = $cif[strlen($cif) - 1]; |
| 92 | + $suma_A = 0; |
| 93 | + $suma_B = 0; |
| 94 | + |
| 95 | + for ($i = 1; $i < 8; $i++) { |
| 96 | + if ($i % 2 == 0) |
| 97 | + $suma_A += intval($cif[$i]); |
| 98 | + else { |
| 99 | + $t = (intval($cif[$i]) * 2); |
| 100 | + $p = 0; |
| 101 | + |
| 102 | + for ($j = 0; $j < strlen($t); $j++) { |
| 103 | + $p += substr($t, $j, 1); |
| 104 | + } |
| 105 | + $suma_B += $p; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + $suma_C = (intval($suma_A + $suma_B)) . ""; |
| 110 | + $suma_D = (10 - intval($suma_C[strlen($suma_C) - 1])) % 10; |
| 111 | + |
| 112 | + $letras = "JABCDEFGHI"; |
| 113 | + |
| 114 | + if ($control >= "0" && $control <= "9") |
| 115 | + return ($control == $suma_D); |
| 116 | + else |
| 117 | + return (strtoupper($control) == $letras[$suma_D]); |
| 118 | + } else { |
| 119 | + |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param $cif |
| 126 | + * @return boolean |
| 127 | + */ |
| 128 | + protected function checkCif($cif) { |
| 129 | + |
| 130 | + $cif = strtoupper($cif); |
| 131 | + |
| 132 | + return $this->checkCifFormat($cif); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments