Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit a299253

Browse files
committed
Merge pull request #2 from xmon/master
Añadir validación por CIF
2 parents d7589f8 + 5034432 commit a299253

6 files changed

Lines changed: 268 additions & 0 deletions

File tree

Resources/translations/validators.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ validator.dni.not_valid: DNI incorrect
22
validator.phone.not_valid: Phone number incorrect
33
validator.phone.mobile.not_valid: Mobilephone number incorrect
44
validator.zip_code.not_valid: Zip code incorrect
5+
6+
7+
validator.cif.not_valid: CIF incorrect
8+
validator.dnicif.not_valid: DNI or CIF incorrect

Resources/translations/validators.es.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ validator.dni.not_valid: DNI no válido
22
validator.phone.not_valid: Número de teléfono no válido
33
validator.phone.mobile.not_valid: Número de teléfono móvil no válido
44
validator.zip_code.not_valid: Código postal no válido
5+
6+
7+
validator.cif.not_valid: CIF no válido
8+
validator.dnicif.not_valid: DNI o CIF no válido

Validator/Cif.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Xinjia\SpainValidatorBundle\Validator;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* Class Cif
9+
* @package Xinjia\SpainValidatorBundle\Validator
10+
* @author Juanjo García <[email protected]>
11+
*
12+
* @Annotation
13+
*/
14+
class Cif extends Constraint
15+
{
16+
public $message = 'validator.cif.not_valid';
17+
}

Validator/CifValidator.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 CifValidator
10+
* @package Xinjia\SpainValidatorBundle\Validator
11+
* @author Juanjo García <[email protected]>
12+
*/
13+
class CifValidator extends ConstraintValidator {
14+
15+
private $cifFormatExpr1 = '/^[ABEH][0-9]{8}$/i';
16+
private $cifFormatExpr2 = '/^[KPQS][0-9]{7}[A-J]$/i';
17+
private $cifFormatExpr3 = '/^[CDFGJLMNRUVW][0-9]{7}[0-9A-J]$/i';
18+
19+
/**
20+
* @var \Symfony\Component\Validator\Context\ExecutionContextInterface
21+
*/
22+
protected $context;
23+
24+
/**
25+
* Checks if the passed value is valid.
26+
*
27+
* @param mixed $value The value that should be validated
28+
* @param Constraint $constraint The constraint for the validation
29+
*
30+
* @api
31+
*/
32+
public function validate($value, Constraint $constraint) {
33+
if (!$this->checkCif($value)) {
34+
$this->context->buildViolation($constraint->message)
35+
->addViolation();
36+
}
37+
}
38+
39+
/**
40+
* @param $cif
41+
* @return boolean
42+
*/
43+
protected function checkCifFormat($cif) {
44+
45+
if (preg_match($this->cifFormatExpr1, $cif) || preg_match($this->cifFormatExpr2, $cif) || preg_match($this->cifFormatExpr3, $cif)) {
46+
47+
$control = $cif[strlen($cif) - 1];
48+
$suma_A = 0;
49+
$suma_B = 0;
50+
51+
for ($i = 1; $i < 8; $i++) {
52+
if ($i % 2 == 0)
53+
$suma_A += intval($cif[$i]);
54+
else {
55+
$t = (intval($cif[$i]) * 2);
56+
$p = 0;
57+
58+
for ($j = 0; $j < strlen($t); $j++) {
59+
$p += substr($t, $j, 1);
60+
}
61+
$suma_B += $p;
62+
}
63+
}
64+
65+
$suma_C = (intval($suma_A + $suma_B)) . "";
66+
$suma_D = (10 - intval($suma_C[strlen($suma_C) - 1])) % 10;
67+
68+
$letras = "JABCDEFGHI";
69+
70+
if ($control >= "0" && $control <= "9")
71+
return ($control == $suma_D);
72+
else
73+
return (strtoupper($control) == $letras[$suma_D]);
74+
} else {
75+
76+
return false;
77+
}
78+
}
79+
80+
/**
81+
* @param $cif
82+
* @return boolean
83+
*/
84+
protected function checkCif($cif) {
85+
86+
$cif = strtoupper($cif);
87+
88+
return $this->checkCifFormat($cif);
89+
}
90+
91+
}

Validator/DniCif.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Xinjia\SpainValidatorBundle\Validator;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* Class DniCif
9+
* @package Xinjia\SpainValidatorBundle\Validator
10+
* @author Juanjo García <[email protected]>
11+
*
12+
* @Annotation
13+
*/
14+
class DniCif extends Constraint
15+
{
16+
public $message = 'validator.dnicif.not_valid';
17+
}

Validator/DniCifValidator.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)