Drupal investigation

IssnValidator.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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, __NAMESPACE__.'\Issn');
  32. }
  33. if (null === $value || '' === $value) {
  34. return;
  35. }
  36. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  37. throw new UnexpectedTypeException($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. if ($this->context instanceof ExecutionContextInterface) {
  48. $this->context->buildViolation($constraint->message)
  49. ->setParameter('{{ value }}', $this->formatValue($value))
  50. ->setCode(Issn::MISSING_HYPHEN_ERROR)
  51. ->addViolation();
  52. } else {
  53. $this->buildViolation($constraint->message)
  54. ->setParameter('{{ value }}', $this->formatValue($value))
  55. ->setCode(Issn::MISSING_HYPHEN_ERROR)
  56. ->addViolation();
  57. }
  58. return;
  59. }
  60. $length = strlen($canonical);
  61. if ($length < 8) {
  62. if ($this->context instanceof ExecutionContextInterface) {
  63. $this->context->buildViolation($constraint->message)
  64. ->setParameter('{{ value }}', $this->formatValue($value))
  65. ->setCode(Issn::TOO_SHORT_ERROR)
  66. ->addViolation();
  67. } else {
  68. $this->buildViolation($constraint->message)
  69. ->setParameter('{{ value }}', $this->formatValue($value))
  70. ->setCode(Issn::TOO_SHORT_ERROR)
  71. ->addViolation();
  72. }
  73. return;
  74. }
  75. if ($length > 8) {
  76. if ($this->context instanceof ExecutionContextInterface) {
  77. $this->context->buildViolation($constraint->message)
  78. ->setParameter('{{ value }}', $this->formatValue($value))
  79. ->setCode(Issn::TOO_LONG_ERROR)
  80. ->addViolation();
  81. } else {
  82. $this->buildViolation($constraint->message)
  83. ->setParameter('{{ value }}', $this->formatValue($value))
  84. ->setCode(Issn::TOO_LONG_ERROR)
  85. ->addViolation();
  86. }
  87. return;
  88. }
  89. // 1234567X
  90. // ^^^^^^^ digits only
  91. if (!ctype_digit(substr($canonical, 0, 7))) {
  92. if ($this->context instanceof ExecutionContextInterface) {
  93. $this->context->buildViolation($constraint->message)
  94. ->setParameter('{{ value }}', $this->formatValue($value))
  95. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  96. ->addViolation();
  97. } else {
  98. $this->buildViolation($constraint->message)
  99. ->setParameter('{{ value }}', $this->formatValue($value))
  100. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  101. ->addViolation();
  102. }
  103. return;
  104. }
  105. // 1234567X
  106. // ^ digit, x or X
  107. if (!ctype_digit($canonical[7]) && 'x' !== $canonical[7] && 'X' !== $canonical[7]) {
  108. if ($this->context instanceof ExecutionContextInterface) {
  109. $this->context->buildViolation($constraint->message)
  110. ->setParameter('{{ value }}', $this->formatValue($value))
  111. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  112. ->addViolation();
  113. } else {
  114. $this->buildViolation($constraint->message)
  115. ->setParameter('{{ value }}', $this->formatValue($value))
  116. ->setCode(Issn::INVALID_CHARACTERS_ERROR)
  117. ->addViolation();
  118. }
  119. return;
  120. }
  121. // 1234567X
  122. // ^ case-sensitive?
  123. if ($constraint->caseSensitive && 'x' === $canonical[7]) {
  124. if ($this->context instanceof ExecutionContextInterface) {
  125. $this->context->buildViolation($constraint->message)
  126. ->setParameter('{{ value }}', $this->formatValue($value))
  127. ->setCode(Issn::INVALID_CASE_ERROR)
  128. ->addViolation();
  129. } else {
  130. $this->buildViolation($constraint->message)
  131. ->setParameter('{{ value }}', $this->formatValue($value))
  132. ->setCode(Issn::INVALID_CASE_ERROR)
  133. ->addViolation();
  134. }
  135. return;
  136. }
  137. // Calculate a checksum. "X" equals 10.
  138. $checkSum = 'X' === $canonical[7]
  139. || 'x' === $canonical[7]
  140. ? 10
  141. : $canonical[7];
  142. for ($i = 0; $i < 7; ++$i) {
  143. // Multiply the first digit by 8, the second by 7, etc.
  144. $checkSum += (8 - $i) * $canonical[$i];
  145. }
  146. if (0 !== $checkSum % 11) {
  147. if ($this->context instanceof ExecutionContextInterface) {
  148. $this->context->buildViolation($constraint->message)
  149. ->setParameter('{{ value }}', $this->formatValue($value))
  150. ->setCode(Issn::CHECKSUM_FAILED_ERROR)
  151. ->addViolation();
  152. } else {
  153. $this->buildViolation($constraint->message)
  154. ->setParameter('{{ value }}', $this->formatValue($value))
  155. ->setCode(Issn::CHECKSUM_FAILED_ERROR)
  156. ->addViolation();
  157. }
  158. }
  159. }
  160. }