BicValidator.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Intl\Countries;
  12. use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. use Symfony\Component\PropertyAccess\PropertyAccessor;
  15. use Symfony\Component\Validator\Constraint;
  16. use Symfony\Component\Validator\ConstraintValidator;
  17. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  18. use Symfony\Component\Validator\Exception\LogicException;
  19. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  20. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  21. /**
  22. * @author Michael Hirschler <michael.vhirsch@gmail.com>
  23. *
  24. * @see https://en.wikipedia.org/wiki/ISO_9362#Structure
  25. */
  26. class BicValidator extends ConstraintValidator
  27. {
  28. private const BIC_COUNTRY_TO_IBAN_COUNTRY_MAP = [
  29. // Reference: https://www.ecbs.org/iban/france-bank-account-number.html
  30. 'GF' => 'FR', // French Guiana
  31. 'PF' => 'FR', // French Polynesia
  32. 'TF' => 'FR', // French Southern Territories
  33. 'GP' => 'FR', // Guadeloupe
  34. 'MQ' => 'FR', // Martinique
  35. 'YT' => 'FR', // Mayotte
  36. 'NC' => 'FR', // New Caledonia
  37. 'RE' => 'FR', // Reunion
  38. 'PM' => 'FR', // Saint Pierre and Miquelon
  39. 'WF' => 'FR', // Wallis and Futuna Islands
  40. // Reference: https://www.ecbs.org/iban/united-kingdom-uk-bank-account-number.html
  41. 'JE' => 'GB', // Jersey
  42. 'IM' => 'GB', // Isle of Man
  43. 'GG' => 'GB', // Guernsey
  44. 'VG' => 'GB', // British Virgin Islands
  45. ];
  46. private $propertyAccessor;
  47. public function __construct(PropertyAccessor $propertyAccessor = null)
  48. {
  49. $this->propertyAccessor = $propertyAccessor;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function validate($value, Constraint $constraint)
  55. {
  56. if (!$constraint instanceof Bic) {
  57. throw new UnexpectedTypeException($constraint, Bic::class);
  58. }
  59. if (null === $value || '' === $value) {
  60. return;
  61. }
  62. if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  63. throw new UnexpectedValueException($value, 'string');
  64. }
  65. $canonicalize = str_replace(' ', '', $value);
  66. // the bic must be either 8 or 11 characters long
  67. if (!\in_array(\strlen($canonicalize), [8, 11])) {
  68. $this->context->buildViolation($constraint->message)
  69. ->setParameter('{{ value }}', $this->formatValue($value))
  70. ->setCode(Bic::INVALID_LENGTH_ERROR)
  71. ->addViolation();
  72. return;
  73. }
  74. // must contain alphanumeric values only
  75. if (!ctype_alnum($canonicalize)) {
  76. $this->context->buildViolation($constraint->message)
  77. ->setParameter('{{ value }}', $this->formatValue($value))
  78. ->setCode(Bic::INVALID_CHARACTERS_ERROR)
  79. ->addViolation();
  80. return;
  81. }
  82. // first 4 letters must be alphabetic (bank code)
  83. if (!ctype_alpha(substr($canonicalize, 0, 4))) {
  84. $this->context->buildViolation($constraint->message)
  85. ->setParameter('{{ value }}', $this->formatValue($value))
  86. ->setCode(Bic::INVALID_BANK_CODE_ERROR)
  87. ->addViolation();
  88. return;
  89. }
  90. if (!Countries::exists(substr($canonicalize, 4, 2))) {
  91. $this->context->buildViolation($constraint->message)
  92. ->setParameter('{{ value }}', $this->formatValue($value))
  93. ->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)
  94. ->addViolation();
  95. return;
  96. }
  97. // should contain uppercase characters only
  98. if (strtoupper($canonicalize) !== $canonicalize) {
  99. $this->context->buildViolation($constraint->message)
  100. ->setParameter('{{ value }}', $this->formatValue($value))
  101. ->setCode(Bic::INVALID_CASE_ERROR)
  102. ->addViolation();
  103. return;
  104. }
  105. // check against an IBAN
  106. $iban = $constraint->iban;
  107. $path = $constraint->ibanPropertyPath;
  108. if ($path && null !== $object = $this->context->getObject()) {
  109. try {
  110. $iban = $this->getPropertyAccessor()->getValue($object, $path);
  111. } catch (NoSuchPropertyException $e) {
  112. throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
  113. }
  114. }
  115. if (!$iban) {
  116. return;
  117. }
  118. $ibanCountryCode = substr($iban, 0, 2);
  119. if (ctype_alpha($ibanCountryCode) && !$this->bicAndIbanCountriesMatch(substr($canonicalize, 4, 2), $ibanCountryCode)) {
  120. $this->context->buildViolation($constraint->ibanMessage)
  121. ->setParameter('{{ value }}', $this->formatValue($value))
  122. ->setParameter('{{ iban }}', $iban)
  123. ->setCode(Bic::INVALID_IBAN_COUNTRY_CODE_ERROR)
  124. ->addViolation();
  125. }
  126. }
  127. private function getPropertyAccessor(): PropertyAccessor
  128. {
  129. if (null === $this->propertyAccessor) {
  130. if (!class_exists(PropertyAccess::class)) {
  131. throw new LogicException('Unable to use property path as the Symfony PropertyAccess component is not installed.');
  132. }
  133. $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
  134. }
  135. return $this->propertyAccessor;
  136. }
  137. private function bicAndIbanCountriesMatch(string $bicCountryCode, string $ibanCountryCode): bool
  138. {
  139. return $ibanCountryCode === $bicCountryCode || $ibanCountryCode === (self::BIC_COUNTRY_TO_IBAN_COUNTRY_MAP[$bicCountryCode] ?? null);
  140. }
  141. }