Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\ConstraintValidator;
|
Chris@17
|
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @author Michael Hirschler <michael.vhirsch@gmail.com>
|
Chris@0
|
20 *
|
Chris@0
|
21 * @see https://en.wikipedia.org/wiki/ISO_9362#Structure
|
Chris@0
|
22 */
|
Chris@0
|
23 class BicValidator extends ConstraintValidator
|
Chris@0
|
24 {
|
Chris@0
|
25 /**
|
Chris@0
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 public function validate($value, Constraint $constraint)
|
Chris@0
|
29 {
|
Chris@17
|
30 if (!$constraint instanceof Bic) {
|
Chris@17
|
31 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Bic');
|
Chris@17
|
32 }
|
Chris@17
|
33
|
Chris@0
|
34 if (null === $value || '' === $value) {
|
Chris@0
|
35 return;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 $canonicalize = str_replace(' ', '', $value);
|
Chris@0
|
39
|
Chris@0
|
40 // the bic must be either 8 or 11 characters long
|
Chris@17
|
41 if (!\in_array(\strlen($canonicalize), [8, 11])) {
|
Chris@0
|
42 $this->context->buildViolation($constraint->message)
|
Chris@0
|
43 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
44 ->setCode(Bic::INVALID_LENGTH_ERROR)
|
Chris@0
|
45 ->addViolation();
|
Chris@0
|
46
|
Chris@0
|
47 return;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 // must contain alphanumeric values only
|
Chris@0
|
51 if (!ctype_alnum($canonicalize)) {
|
Chris@0
|
52 $this->context->buildViolation($constraint->message)
|
Chris@0
|
53 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
54 ->setCode(Bic::INVALID_CHARACTERS_ERROR)
|
Chris@0
|
55 ->addViolation();
|
Chris@0
|
56
|
Chris@0
|
57 return;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 // first 4 letters must be alphabetic (bank code)
|
Chris@0
|
61 if (!ctype_alpha(substr($canonicalize, 0, 4))) {
|
Chris@0
|
62 $this->context->buildViolation($constraint->message)
|
Chris@0
|
63 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
64 ->setCode(Bic::INVALID_BANK_CODE_ERROR)
|
Chris@0
|
65 ->addViolation();
|
Chris@0
|
66
|
Chris@0
|
67 return;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 // next 2 letters must be alphabetic (country code)
|
Chris@0
|
71 if (!ctype_alpha(substr($canonicalize, 4, 2))) {
|
Chris@0
|
72 $this->context->buildViolation($constraint->message)
|
Chris@0
|
73 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
74 ->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)
|
Chris@0
|
75 ->addViolation();
|
Chris@0
|
76
|
Chris@0
|
77 return;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 // should contain uppercase characters only
|
Chris@0
|
81 if (strtoupper($canonicalize) !== $canonicalize) {
|
Chris@0
|
82 $this->context->buildViolation($constraint->message)
|
Chris@0
|
83 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
84 ->setCode(Bic::INVALID_CASE_ERROR)
|
Chris@0
|
85 ->addViolation();
|
Chris@0
|
86
|
Chris@0
|
87 return;
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|