d2188071fabf144aaa3952d952830ea0fac098cb
[yaffs-website] / vendor / symfony / validator / Constraints / BicValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16
17 /**
18  * @author Michael Hirschler <michael.vhirsch@gmail.com>
19  *
20  * @see https://en.wikipedia.org/wiki/ISO_9362#Structure
21  */
22 class BicValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (null === $value || '' === $value) {
30             return;
31         }
32
33         $canonicalize = str_replace(' ', '', $value);
34
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)
40                 ->addViolation();
41
42             return;
43         }
44
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)
50                 ->addViolation();
51
52             return;
53         }
54
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)
60                 ->addViolation();
61
62             return;
63         }
64
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)
70                 ->addViolation();
71
72             return;
73         }
74
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)
80                 ->addViolation();
81
82             return;
83         }
84     }
85 }