Drupal investigation

BicValidator.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  14. * @author Michael Hirschler <michael.vhirsch@gmail.com>
  15. *
  16. * @see https://en.wikipedia.org/wiki/ISO_9362#Structure
  17. */
  18. class BicValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (null === $value || '' === $value) {
  26. return;
  27. }
  28. $canonicalize = str_replace(' ', '', $value);
  29. // the bic must be either 8 or 11 characters long
  30. if (!in_array(strlen($canonicalize), array(8, 11))) {
  31. $this->context->buildViolation($constraint->message)
  32. ->setParameter('{{ value }}', $this->formatValue($value))
  33. ->setCode(Bic::INVALID_LENGTH_ERROR)
  34. ->addViolation();
  35. return;
  36. }
  37. // must contain alphanumeric values only
  38. if (!ctype_alnum($canonicalize)) {
  39. $this->context->buildViolation($constraint->message)
  40. ->setParameter('{{ value }}', $this->formatValue($value))
  41. ->setCode(Bic::INVALID_CHARACTERS_ERROR)
  42. ->addViolation();
  43. return;
  44. }
  45. // first 4 letters must be alphabetic (bank code)
  46. if (!ctype_alpha(substr($canonicalize, 0, 4))) {
  47. $this->context->buildViolation($constraint->message)
  48. ->setParameter('{{ value }}', $this->formatValue($value))
  49. ->setCode(Bic::INVALID_BANK_CODE_ERROR)
  50. ->addViolation();
  51. return;
  52. }
  53. // next 2 letters must be alphabetic (country code)
  54. if (!ctype_alpha(substr($canonicalize, 4, 2))) {
  55. $this->context->buildViolation($constraint->message)
  56. ->setParameter('{{ value }}', $this->formatValue($value))
  57. ->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)
  58. ->addViolation();
  59. return;
  60. }
  61. // should contain uppercase characters only
  62. if (strtoupper($canonicalize) !== $canonicalize) {
  63. $this->context->buildViolation($constraint->message)
  64. ->setParameter('{{ value }}', $this->formatValue($value))
  65. ->setCode(Bic::INVALID_CASE_ERROR)
  66. ->addViolation();
  67. return;
  68. }
  69. }
  70. }