IssnValidator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16. * Validates whether the value is a valid ISSN.
  17. *
  18. * @author Antonio J. García Lagar <aj@garcialagar.es>
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @see https://en.wikipedia.org/wiki/Issn
  22. */
  23. class IssnValidator extends ConstraintValidator
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function validate($value, Constraint $constraint)
  29. {
  30. if (!$constraint instanceof Issn) {
  31. throw new UnexpectedTypeException($constraint, Issn::class);
  32. }
  33. if (null === $value || '' === $value) {
  34. return;
  35. }
  36. if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  37. throw new UnexpectedValueException($value, 'string');
  38. }
  39. $value = (string) $value;
  40. $canonical = $value;
  41. // 1234-567X
  42. // ^
  43. if (isset($canonical[4]) && '-' === $canonical[4]) {
  44. // remove hyphen
  45. $canonical = substr($canonical, 0, 4).substr($canonical, 5);
  46. } elseif ($constraint->requireHyphen) {
  47. $this->context->buildViolation($constraint->message)
  48. ->setParameter('{{ value }}', $this->formatValue($value))
  49. ->setCode(Issn::MISSING_HYPHEN_ERROR)
  50. ->addViolation();
  51. return;
  52. }
  53. $length = \strlen($canonical);
  54. if ($length < 8) {
  55. $this->context->buildViolation($constraint->message)
  56. ->setParameter('{{ value }}', $this->formatValue($value))
  57. ->setCode(Issn::TOO_SHORT_ERROR)
  58. ->addViolation();
  59. return;
  60. }
  61. if ($length > 8) {
  62. $this->context->buildViolation($constraint->message)
  63. ->setParameter('{{ value }}', $this->formatValue($value))
  64. ->setCode(Issn::TOO_LONG_ERROR)
  65. ->addViolation();
  66. return;
  67. }
  68. // 1234567X
  69. // ^^^^^^^ digits only
  70. if (!ctype_digit(substr($canonical, 0, 7))) {
  71. $this->context->buildViolation($constraint->message)
  72. ->setParameter('{{ value }}', $this->formatValue($value))
  73. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  74. ->addViolation();
  75. return;
  76. }
  77. // 1234567X
  78. // ^ digit, x or X
  79. if (!ctype_digit($canonical[7]) && 'x' !== $canonical[7] && 'X' !== $canonical[7]) {
  80. $this->context->buildViolation($constraint->message)
  81. ->setParameter('{{ value }}', $this->formatValue($value))
  82. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  83. ->addViolation();
  84. return;
  85. }
  86. // 1234567X
  87. // ^ case-sensitive?
  88. if ($constraint->caseSensitive && 'x' === $canonical[7]) {
  89. $this->context->buildViolation($constraint->message)
  90. ->setParameter('{{ value }}', $this->formatValue($value))
  91. ->setCode(Issn::INVALID_CASE_ERROR)
  92. ->addViolation();
  93. return;
  94. }
  95. // Calculate a checksum. "X" equals 10.
  96. $checkSum = 'X' === $canonical[7]
  97. || 'x' === $canonical[7]
  98. ? 10
  99. : $canonical[7];
  100. for ($i = 0; $i < 7; ++$i) {
  101. // Multiply the first digit by 8, the second by 7, etc.
  102. $checkSum += (8 - $i) * (int) $canonical[$i];
  103. }
  104. if (0 !== $checkSum % 11) {
  105. $this->context->buildViolation($constraint->message)
  106. ->setParameter('{{ value }}', $this->formatValue($value))
  107. ->setCode(Issn::CHECKSUM_FAILED_ERROR)
  108. ->addViolation();
  109. }
  110. }
  111. }