|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraint; |
| 15 | +use Symfony\Component\Validator\ConstraintValidator; |
| 16 | +use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 17 | +use Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Laurent Masforné <l.masforne@gmail.com> |
| 21 | + * |
| 22 | + * @see https://en.wikipedia.org/wiki/International_Securities_Identification_Number |
| 23 | + */ |
| 24 | +class IsinValidator extends ConstraintValidator |
| 25 | +{ |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function validate($value, Constraint $constraint) |
| 30 | + { |
| 31 | + if (!$constraint instanceof Isin) { |
| 32 | + throw new UnexpectedTypeException($constraint, Isin::class); |
| 33 | + } |
| 34 | + |
| 35 | + if (null === $value || '' === $value) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { |
| 40 | + throw new UnexpectedValueException($value, 'string'); |
| 41 | + } |
| 42 | + |
| 43 | + $value = strtoupper($value); |
| 44 | + |
| 45 | + if (Isin::VALIDATION_LENGTH !== \strlen($value)) { |
| 46 | + $this->context->buildViolation($constraint->message) |
| 47 | + ->setParameter('{{ value }}', $this->formatValue($value)) |
| 48 | + ->setCode(Isin::INVALID_LENGTH_ERROR) |
| 49 | + ->addViolation(); |
| 50 | + |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (!preg_match(Isin::VALIDATION_PATTERN, $value)) { |
| 55 | + $this->context->buildViolation($constraint->message) |
| 56 | + ->setParameter('{{ value }}', $this->formatValue($value)) |
| 57 | + ->setCode(Isin::INVALID_PATTERN_ERROR) |
| 58 | + ->addViolation(); |
| 59 | + |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (!$this->isCorrectChecksum($value)) { |
| 64 | + $this->context->buildViolation($constraint->message) |
| 65 | + ->setParameter('{{ value }}', $this->formatValue($value)) |
| 66 | + ->setCode(Isin::INVALID_CHECKSUM_ERROR) |
| 67 | + ->addViolation(); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + return $value; |
| 73 | + } |
| 74 | + |
| 75 | + private function isCorrectChecksum($input) |
| 76 | + { |
| 77 | + $characters = str_split($input); |
| 78 | + foreach ($characters as $i => $char) { |
| 79 | + $characters[$i] = \intval($char, 36); |
| 80 | + } |
| 81 | + $checkDigit = array_pop($characters); |
| 82 | + $number = implode('', $characters); |
| 83 | + $expectedCheckDigit = $this->getCheckDigit($number); |
| 84 | + |
| 85 | + return $checkDigit === $expectedCheckDigit; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * This method performs the luhn algorithm |
| 90 | + * to obtain a check digit. |
| 91 | + */ |
| 92 | + private function getCheckDigit($input) |
| 93 | + { |
| 94 | + // first split up the string |
| 95 | + $numbers = str_split($input); |
| 96 | + |
| 97 | + // calculate the positional value. |
| 98 | + // when there is an even number of digits the second group will be multiplied, so p starts on 0 |
| 99 | + // when there is an odd number of digits the first group will be multiplied, so p starts on 1 |
| 100 | + $p = \count($numbers) % 2; |
| 101 | + // run through each number |
| 102 | + foreach ($numbers as $i => $num) { |
| 103 | + $num = (int) $num; |
| 104 | + // every positional number needs to be multiplied by 2 |
| 105 | + if ($p % 2) { |
| 106 | + $num = $num * 2; |
| 107 | + // if the result was more than 9 |
| 108 | + // add the individual digits |
| 109 | + $num = array_sum(str_split($num)); |
| 110 | + } |
| 111 | + $numbers[$i] = $num; |
| 112 | + ++$p; |
| 113 | + } |
| 114 | + |
| 115 | + // get the total value of all the digits |
| 116 | + $sum = array_sum($numbers); |
| 117 | + |
| 118 | + // get the remainder when dividing by 10 |
| 119 | + $mod = $sum % 10; |
| 120 | + |
| 121 | + // subtract from 10 |
| 122 | + $rem = 10 - $mod; |
| 123 | + |
| 124 | + // mod from 10 to catch if the result was 0 |
| 125 | + $digit = $rem % 10; |
| 126 | + |
| 127 | + return $digit; |
| 128 | + } |
| 129 | +} |
0 commit comments