|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraint; |
|
6
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
7
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
8
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CifValidator |
|
12
|
|
|
* @package ByTIC\Validator\Constraints |
|
13
|
|
|
*/ |
|
14
|
|
|
class CifValidator extends ConstraintValidator |
|
15
|
|
|
{ |
|
16
|
|
|
private static $controlKey = [7, 5, 3, 2, 1, 7, 5, 3, 2]; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritDoc |
|
20
|
|
|
*/ |
|
21
|
|
|
public function validate($value, Constraint $constraint) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!$constraint instanceof Cif) { |
|
24
|
|
|
throw new UnexpectedTypeException($constraint, Cif::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (null === $value || '' === $value) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!is_numeric($value) && !is_string($value)) { |
|
32
|
|
|
// throw this exception if your validator cannot handle the passed type so that it can be marked as invalid |
|
33
|
|
|
throw new UnexpectedValueException($value, 'string'); |
|
34
|
|
|
|
|
35
|
|
|
// separate multiple types using pipes |
|
36
|
|
|
// throw new UnexpectedValueException($value, 'string|int'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($this->validateCIF($value) === false) { |
|
40
|
|
|
// the argument must be a string or an object implementing __toString() |
|
41
|
|
|
$this->context->buildViolation($constraint->message) |
|
42
|
|
|
->setParameter('{{ string }}', $value) |
|
43
|
|
|
->addViolation(); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function validateCIF($cif){ |
|
48
|
|
|
// Daca este string, elimina atributul fiscal si spatiile |
|
49
|
|
|
if(!is_numeric($cif)){ |
|
50
|
|
|
$cif = strtoupper($cif); |
|
51
|
|
|
if(strpos($cif, 'RO') === 0){ |
|
52
|
|
|
$cif = substr($cif, 2); |
|
53
|
|
|
} |
|
54
|
|
|
$cif = (int) trim($cif); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ((int) $cif <= 0) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$cifLength = strlen($cif); |
|
62
|
|
|
// daca are mai mult de 10 cifre sau mai putin de 2, nu-i valid |
|
63
|
|
|
if($cifLength > 10 || $cifLength < 2){ |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// extrage cifra de control |
|
68
|
|
|
$controlKey = (int) substr($cif, -1); |
|
69
|
|
|
|
|
70
|
|
|
$cif = substr($cif, 0, -1); |
|
71
|
|
|
$cif = str_pad($cif, 9, '0', STR_PAD_LEFT); |
|
72
|
|
|
$suma = 0; |
|
73
|
|
|
foreach (self::$controlKey as $i => $key) { |
|
74
|
|
|
$suma += $cif[$i] * $key; |
|
75
|
|
|
} |
|
76
|
|
|
$suma = $suma * 10; |
|
77
|
|
|
$rest = (int) ($suma % 11); |
|
78
|
|
|
|
|
79
|
|
|
// daca modulo 11 este 10, atunci cifra de control este 0 |
|
80
|
|
|
$rest = ($rest == 10) ? 0 : $rest; |
|
81
|
|
|
|
|
82
|
|
|
return $rest === $controlKey; |
|
83
|
|
|
} |
|
84
|
|
|
} |