IsbnValidator.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. /**
  16. * Validates whether the value is a valid ISBN-10 or ISBN-13.
  17. *
  18. * @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
  19. * @author Manuel Reinhard <manu@sprain.ch>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. *
  22. * @see https://en.wikipedia.org/wiki/Isbn
  23. */
  24. class IsbnValidator extends ConstraintValidator
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function validate($value, Constraint $constraint)
  30. {
  31. if (!$constraint instanceof Isbn) {
  32. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
  33. }
  34. if (null === $value || '' === $value) {
  35. return;
  36. }
  37. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  38. throw new UnexpectedTypeException($value, 'string');
  39. }
  40. $value = (string) $value;
  41. $canonical = str_replace('-', '', $value);
  42. if (null === $constraint->type) {
  43. if ($constraint->isbn10 && !$constraint->isbn13) {
  44. @trigger_error('The "isbn10" option of the Isbn constraint is deprecated since version 2.5 and will be removed in 3.0. Use the "type" option instead.', E_USER_DEPRECATED);
  45. $constraint->type = 'isbn10';
  46. } elseif ($constraint->isbn13 && !$constraint->isbn10) {
  47. @trigger_error('The "isbn13" option of the Isbn constraint is deprecated since version 2.5 and will be removed in 3.0. Use the "type" option instead.', E_USER_DEPRECATED);
  48. $constraint->type = 'isbn13';
  49. }
  50. }
  51. // Explicitly validate against ISBN-10
  52. if ('isbn10' === $constraint->type) {
  53. if (true !== ($code = $this->validateIsbn10($canonical))) {
  54. if ($this->context instanceof ExecutionContextInterface) {
  55. $this->context->buildViolation($this->getMessage($constraint, $constraint->type))
  56. ->setParameter('{{ value }}', $this->formatValue($value))
  57. ->setCode($code)
  58. ->addViolation();
  59. } else {
  60. $this->buildViolation($this->getMessage($constraint, $constraint->type))
  61. ->setParameter('{{ value }}', $this->formatValue($value))
  62. ->setCode($code)
  63. ->addViolation();
  64. }
  65. }
  66. return;
  67. }
  68. // Explicitly validate against ISBN-13
  69. if ('isbn13' === $constraint->type) {
  70. if (true !== ($code = $this->validateIsbn13($canonical))) {
  71. if ($this->context instanceof ExecutionContextInterface) {
  72. $this->context->buildViolation($this->getMessage($constraint, $constraint->type))
  73. ->setParameter('{{ value }}', $this->formatValue($value))
  74. ->setCode($code)
  75. ->addViolation();
  76. } else {
  77. $this->buildViolation($this->getMessage($constraint, $constraint->type))
  78. ->setParameter('{{ value }}', $this->formatValue($value))
  79. ->setCode($code)
  80. ->addViolation();
  81. }
  82. }
  83. return;
  84. }
  85. // Try both ISBNs
  86. // First, try ISBN-10
  87. $code = $this->validateIsbn10($canonical);
  88. // The ISBN can only be an ISBN-13 if the value was too long for ISBN-10
  89. if (Isbn::TOO_LONG_ERROR === $code) {
  90. // Try ISBN-13 now
  91. $code = $this->validateIsbn13($canonical);
  92. // If too short, this means we have 11 or 12 digits
  93. if (Isbn::TOO_SHORT_ERROR === $code) {
  94. $code = Isbn::TYPE_NOT_RECOGNIZED_ERROR;
  95. }
  96. }
  97. if (true !== $code) {
  98. if ($this->context instanceof ExecutionContextInterface) {
  99. $this->context->buildViolation($this->getMessage($constraint))
  100. ->setParameter('{{ value }}', $this->formatValue($value))
  101. ->setCode($code)
  102. ->addViolation();
  103. } else {
  104. $this->buildViolation($this->getMessage($constraint))
  105. ->setParameter('{{ value }}', $this->formatValue($value))
  106. ->setCode($code)
  107. ->addViolation();
  108. }
  109. }
  110. }
  111. protected function validateIsbn10($isbn)
  112. {
  113. // Choose an algorithm so that ERROR_INVALID_CHARACTERS is preferred
  114. // over ERROR_TOO_SHORT/ERROR_TOO_LONG
  115. // Otherwise "0-45122-5244" passes, but "0-45122_5244" reports
  116. // "too long"
  117. // Error priority:
  118. // 1. ERROR_INVALID_CHARACTERS
  119. // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG
  120. // 3. ERROR_CHECKSUM_FAILED
  121. $checkSum = 0;
  122. for ($i = 0; $i < 10; ++$i) {
  123. // If we test the length before the loop, we get an ERROR_TOO_SHORT
  124. // when actually an ERROR_INVALID_CHARACTERS is wanted, e.g. for
  125. // "0-45122_5244" (typo)
  126. if (!isset($isbn[$i])) {
  127. return Isbn::TOO_SHORT_ERROR;
  128. }
  129. if ('X' === $isbn[$i]) {
  130. $digit = 10;
  131. } elseif (ctype_digit($isbn[$i])) {
  132. $digit = $isbn[$i];
  133. } else {
  134. return Isbn::INVALID_CHARACTERS_ERROR;
  135. }
  136. $checkSum += $digit * (10 - $i);
  137. }
  138. if (isset($isbn[$i])) {
  139. return Isbn::TOO_LONG_ERROR;
  140. }
  141. return 0 === $checkSum % 11 ? true : Isbn::CHECKSUM_FAILED_ERROR;
  142. }
  143. protected function validateIsbn13($isbn)
  144. {
  145. // Error priority:
  146. // 1. ERROR_INVALID_CHARACTERS
  147. // 2. ERROR_TOO_SHORT/ERROR_TOO_LONG
  148. // 3. ERROR_CHECKSUM_FAILED
  149. if (!ctype_digit($isbn)) {
  150. return Isbn::INVALID_CHARACTERS_ERROR;
  151. }
  152. $length = strlen($isbn);
  153. if ($length < 13) {
  154. return Isbn::TOO_SHORT_ERROR;
  155. }
  156. if ($length > 13) {
  157. return Isbn::TOO_LONG_ERROR;
  158. }
  159. $checkSum = 0;
  160. for ($i = 0; $i < 13; $i += 2) {
  161. $checkSum += $isbn[$i];
  162. }
  163. for ($i = 1; $i < 12; $i += 2) {
  164. $checkSum += $isbn[$i]
  165. * 3;
  166. }
  167. return 0 === $checkSum % 10 ? true : Isbn::CHECKSUM_FAILED_ERROR;
  168. }
  169. protected function getMessage($constraint, $type = null)
  170. {
  171. if (null !== $constraint->message) {
  172. return $constraint->message;
  173. } elseif ('isbn10' === $type) {
  174. return $constraint->isbn10Message;
  175. } elseif ('isbn13' === $type) {
  176. return $constraint->isbn13Message;
  177. }
  178. return $constraint->bothIsbnMessage;
  179. }
  180. }