4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19 * Validates whether the value is a valid ISBN-10 or ISBN-13.
21 * @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
22 * @author Manuel Reinhard <manu@sprain.ch>
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 * @see https://en.wikipedia.org/wiki/Isbn
27 class IsbnValidator extends ConstraintValidator
32 public function validate($value, Constraint $constraint)
34 if (!$constraint instanceof Isbn) {
35 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
38 if (null === $value || '' === $value) {
42 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
43 throw new UnexpectedTypeException($value, 'string');
46 $value = (string) $value;
47 $canonical = str_replace('-', '', $value);
49 // Explicitly validate against ISBN-10
50 if ('isbn10' === $constraint->type) {
51 if (true !== ($code = $this->validateIsbn10($canonical))) {
52 $this->context->buildViolation($this->getMessage($constraint, $constraint->type))
53 ->setParameter('{{ value }}', $this->formatValue($value))
61 // Explicitly validate against ISBN-13
62 if ('isbn13' === $constraint->type) {
63 if (true !== ($code = $this->validateIsbn13($canonical))) {
64 $this->context->buildViolation($this->getMessage($constraint, $constraint->type))
65 ->setParameter('{{ value }}', $this->formatValue($value))
76 $code = $this->validateIsbn10($canonical);
78 // The ISBN can only be an ISBN-13 if the value was too long for ISBN-10
79 if (Isbn::TOO_LONG_ERROR === $code) {
81 $code = $this->validateIsbn13($canonical);
83 // If too short, this means we have 11 or 12 digits
84 if (Isbn::TOO_SHORT_ERROR === $code) {
85 $code = Isbn::TYPE_NOT_RECOGNIZED_ERROR;
90 $this->context->buildViolation($this->getMessage($constraint))
91 ->setParameter('{{ value }}', $this->formatValue($value))
97 protected function validateIsbn10($isbn)
99 // Choose an algorithm so that ERROR_INVALID_CHARACTERS is preferred
100 // over ERROR_TOO_SHORT/ERROR_TOO_LONG
101 // Otherwise "0-45122-5244" passes, but "0-45122_5244" reports
105 // 1. ERROR_INVALID_CHARACTERS
106 // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG
107 // 3. ERROR_CHECKSUM_FAILED
111 for ($i = 0; $i < 10; ++$i) {
112 // If we test the length before the loop, we get an ERROR_TOO_SHORT
113 // when actually an ERROR_INVALID_CHARACTERS is wanted, e.g. for
114 // "0-45122_5244" (typo)
115 if (!isset($isbn[$i])) {
116 return Isbn::TOO_SHORT_ERROR;
119 if ('X' === $isbn[$i]) {
121 } elseif (ctype_digit($isbn[$i])) {
124 return Isbn::INVALID_CHARACTERS_ERROR;
127 $checkSum += $digit * (10 - $i);
130 if (isset($isbn[$i])) {
131 return Isbn::TOO_LONG_ERROR;
134 return 0 === $checkSum % 11 ? true : Isbn::CHECKSUM_FAILED_ERROR;
137 protected function validateIsbn13($isbn)
140 // 1. ERROR_INVALID_CHARACTERS
141 // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG
142 // 3. ERROR_CHECKSUM_FAILED
144 if (!ctype_digit($isbn)) {
145 return Isbn::INVALID_CHARACTERS_ERROR;
148 $length = strlen($isbn);
151 return Isbn::TOO_SHORT_ERROR;
155 return Isbn::TOO_LONG_ERROR;
160 for ($i = 0; $i < 13; $i += 2) {
161 $checkSum += $isbn[$i];
164 for ($i = 1; $i < 12; $i += 2) {
165 $checkSum += $isbn[$i]
169 return 0 === $checkSum % 10 ? true : Isbn::CHECKSUM_FAILED_ERROR;
172 protected function getMessage($constraint, $type = null)
174 if (null !== $constraint->message) {
175 return $constraint->message;
176 } elseif ('isbn10' === $type) {
177 return $constraint->isbn10Message;
178 } elseif ('isbn13' === $type) {
179 return $constraint->isbn13Message;
182 return $constraint->bothIsbnMessage;