4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
18 * @author Michael Hirschler <michael.vhirsch@gmail.com>
20 * @see https://en.wikipedia.org/wiki/ISO_9362#Structure
22 class BicValidator extends ConstraintValidator
27 public function validate($value, Constraint $constraint)
29 if (null === $value || '' === $value) {
33 $canonicalize = str_replace(' ', '', $value);
35 // the bic must be either 8 or 11 characters long
36 if (!in_array(strlen($canonicalize), array(8, 11))) {
37 $this->context->buildViolation($constraint->message)
38 ->setParameter('{{ value }}', $this->formatValue($value))
39 ->setCode(Bic::INVALID_LENGTH_ERROR)
45 // must contain alphanumeric values only
46 if (!ctype_alnum($canonicalize)) {
47 $this->context->buildViolation($constraint->message)
48 ->setParameter('{{ value }}', $this->formatValue($value))
49 ->setCode(Bic::INVALID_CHARACTERS_ERROR)
55 // first 4 letters must be alphabetic (bank code)
56 if (!ctype_alpha(substr($canonicalize, 0, 4))) {
57 $this->context->buildViolation($constraint->message)
58 ->setParameter('{{ value }}', $this->formatValue($value))
59 ->setCode(Bic::INVALID_BANK_CODE_ERROR)
65 // next 2 letters must be alphabetic (country code)
66 if (!ctype_alpha(substr($canonicalize, 4, 2))) {
67 $this->context->buildViolation($constraint->message)
68 ->setParameter('{{ value }}', $this->formatValue($value))
69 ->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)
75 // should contain uppercase characters only
76 if (strtoupper($canonicalize) !== $canonicalize) {
77 $this->context->buildViolation($constraint->message)
78 ->setParameter('{{ value }}', $this->formatValue($value))
79 ->setCode(Bic::INVALID_CASE_ERROR)