comparison vendor/symfony/validator/Constraints/IssnValidator.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
18 /**
19 * Validates whether the value is a valid ISSN.
20 *
21 * @author Antonio J. GarcĂ­a Lagar <aj@garcialagar.es>
22 * @author Bernhard Schussek <bschussek@gmail.com>
23 *
24 * @see https://en.wikipedia.org/wiki/Issn
25 */
26 class IssnValidator extends ConstraintValidator
27 {
28 /**
29 * {@inheritdoc}
30 */
31 public function validate($value, Constraint $constraint)
32 {
33 if (!$constraint instanceof Issn) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Issn');
35 }
36
37 if (null === $value || '' === $value) {
38 return;
39 }
40
41 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
42 throw new UnexpectedTypeException($value, 'string');
43 }
44
45 $value = (string) $value;
46 $canonical = $value;
47
48 // 1234-567X
49 // ^
50 if (isset($canonical[4]) && '-' === $canonical[4]) {
51 // remove hyphen
52 $canonical = substr($canonical, 0, 4).substr($canonical, 5);
53 } elseif ($constraint->requireHyphen) {
54 $this->context->buildViolation($constraint->message)
55 ->setParameter('{{ value }}', $this->formatValue($value))
56 ->setCode(Issn::MISSING_HYPHEN_ERROR)
57 ->addViolation();
58
59 return;
60 }
61
62 $length = strlen($canonical);
63
64 if ($length < 8) {
65 $this->context->buildViolation($constraint->message)
66 ->setParameter('{{ value }}', $this->formatValue($value))
67 ->setCode(Issn::TOO_SHORT_ERROR)
68 ->addViolation();
69
70 return;
71 }
72
73 if ($length > 8) {
74 $this->context->buildViolation($constraint->message)
75 ->setParameter('{{ value }}', $this->formatValue($value))
76 ->setCode(Issn::TOO_LONG_ERROR)
77 ->addViolation();
78
79 return;
80 }
81
82 // 1234567X
83 // ^^^^^^^ digits only
84 if (!ctype_digit(substr($canonical, 0, 7))) {
85 $this->context->buildViolation($constraint->message)
86 ->setParameter('{{ value }}', $this->formatValue($value))
87 ->setCode(Issn::INVALID_CHARACTERS_ERROR)
88 ->addViolation();
89
90 return;
91 }
92
93 // 1234567X
94 // ^ digit, x or X
95 if (!ctype_digit($canonical[7]) && 'x' !== $canonical[7] && 'X' !== $canonical[7]) {
96 $this->context->buildViolation($constraint->message)
97 ->setParameter('{{ value }}', $this->formatValue($value))
98 ->setCode(Issn::INVALID_CHARACTERS_ERROR)
99 ->addViolation();
100
101 return;
102 }
103
104 // 1234567X
105 // ^ case-sensitive?
106 if ($constraint->caseSensitive && 'x' === $canonical[7]) {
107 $this->context->buildViolation($constraint->message)
108 ->setParameter('{{ value }}', $this->formatValue($value))
109 ->setCode(Issn::INVALID_CASE_ERROR)
110 ->addViolation();
111
112 return;
113 }
114
115 // Calculate a checksum. "X" equals 10.
116 $checkSum = 'X' === $canonical[7]
117 || 'x' === $canonical[7]
118 ? 10
119 : $canonical[7];
120
121 for ($i = 0; $i < 7; ++$i) {
122 // Multiply the first digit by 8, the second by 7, etc.
123 $checkSum += (8 - $i) * $canonical[$i];
124 }
125
126 if (0 !== $checkSum % 11) {
127 $this->context->buildViolation($constraint->message)
128 ->setParameter('{{ value }}', $this->formatValue($value))
129 ->setCode(Issn::CHECKSUM_FAILED_ERROR)
130 ->addViolation();
131 }
132 }
133 }