AbstractComparison.php
1 year ago
AbstractComparisonValidator.php
1 year ago
All.php
1 year ago
AllValidator.php
4 years ago
AtLeastOneOf.php
1 year ago
AtLeastOneOfValidator.php
1 year ago
Bic.php
1 year ago
BicValidator.php
1 year ago
Blank.php
1 year ago
BlankValidator.php
4 years ago
Callback.php
1 year ago
CallbackValidator.php
4 years ago
CardScheme.php
1 year ago
CardSchemeValidator.php
1 year ago
Cascade.php
1 year ago
Choice.php
1 year ago
ChoiceValidator.php
1 year ago
Cidr.php
1 year ago
CidrValidator.php
4 years ago
Collection.php
1 year ago
CollectionValidator.php
4 years ago
Composite.php
1 year ago
Compound.php
4 years ago
CompoundValidator.php
4 years ago
Count.php
1 year ago
CountValidator.php
4 years ago
Country.php
1 year ago
CountryValidator.php
4 years ago
CssColor.php
1 year ago
CssColorValidator.php
1 year ago
Currency.php
1 year ago
CurrencyValidator.php
4 years ago
Date.php
1 year ago
DateTime.php
1 year ago
DateTimeValidator.php
2 years ago
DateValidator.php
1 year ago
DisableAutoMapping.php
1 year ago
DivisibleBy.php
4 years ago
DivisibleByValidator.php
4 years ago
Email.php
1 year ago
EmailValidator.php
1 year ago
EnableAutoMapping.php
1 year ago
EqualTo.php
4 years ago
EqualToValidator.php
4 years ago
Existence.php
4 years ago
Expression.php
1 year ago
ExpressionLanguageSyntax.php
1 year ago
ExpressionLanguageSyntaxValidator.php
1 year ago
ExpressionValidator.php
1 year ago
File.php
1 year ago
FileValidator.php
4 years ago
GreaterThan.php
4 years ago
GreaterThanOrEqual.php
4 years ago
GreaterThanOrEqualValidator.php
4 years ago
GreaterThanValidator.php
4 years ago
GroupSequence.php
4 years ago
GroupSequenceProvider.php
4 years ago
Hostname.php
1 year ago
HostnameValidator.php
4 years ago
Iban.php
1 year ago
IbanValidator.php
1 year ago
IdenticalTo.php
4 years ago
IdenticalToValidator.php
4 years ago
Image.php
1 year ago
ImageValidator.php
4 years ago
Ip.php
1 year ago
IpValidator.php
4 years ago
IsFalse.php
1 year ago
IsFalseValidator.php
4 years ago
IsNull.php
1 year ago
IsNullValidator.php
4 years ago
IsTrue.php
1 year ago
IsTrueValidator.php
4 years ago
Isbn.php
1 year ago
IsbnValidator.php
1 year ago
Isin.php
1 year ago
IsinValidator.php
4 years ago
Issn.php
1 year ago
IssnValidator.php
4 years ago
Json.php
1 year ago
JsonValidator.php
1 year ago
Language.php
1 year ago
LanguageValidator.php
4 years ago
Length.php
1 year ago
LengthValidator.php
4 years ago
LessThan.php
4 years ago
LessThanOrEqual.php
4 years ago
LessThanOrEqualValidator.php
4 years ago
LessThanValidator.php
4 years ago
Locale.php
1 year ago
LocaleValidator.php
4 years ago
Luhn.php
1 year ago
LuhnValidator.php
4 years ago
Negative.php
4 years ago
NegativeOrZero.php
4 years ago
NotBlank.php
1 year ago
NotBlankValidator.php
4 years ago
NotCompromisedPassword.php
1 year ago
NotCompromisedPasswordValidator.php
1 year ago
NotEqualTo.php
4 years ago
NotEqualToValidator.php
4 years ago
NotIdenticalTo.php
4 years ago
NotIdenticalToValidator.php
4 years ago
NotNull.php
1 year ago
NotNullValidator.php
4 years ago
NumberConstraintTrait.php
4 years ago
Optional.php
4 years ago
Positive.php
4 years ago
PositiveOrZero.php
4 years ago
Range.php
1 year ago
RangeValidator.php
1 year ago
Regex.php
1 year ago
RegexValidator.php
4 years ago
Required.php
4 years ago
Sequentially.php
1 year ago
SequentiallyValidator.php
4 years ago
Time.php
1 year ago
TimeValidator.php
1 year ago
Timezone.php
1 year ago
TimezoneValidator.php
1 year ago
Traverse.php
4 years ago
Type.php
1 year ago
TypeValidator.php
4 years ago
Ulid.php
1 year ago
UlidValidator.php
2 years ago
Unique.php
1 year ago
UniqueValidator.php
1 year ago
Url.php
1 year ago
UrlValidator.php
1 year ago
Uuid.php
1 year ago
UuidValidator.php
4 years ago
Valid.php
1 year ago
ValidValidator.php
4 years ago
ZeroComparisonConstraintTrait.php
1 year ago
index.php
3 years ago
BicValidator.php
133 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Constraints; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Intl\Countries; |
| 5 | use MailPoetVendor\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
| 6 | use MailPoetVendor\Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException; |
| 7 | use MailPoetVendor\Symfony\Component\PropertyAccess\PropertyAccess; |
| 8 | use MailPoetVendor\Symfony\Component\PropertyAccess\PropertyAccessor; |
| 9 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 10 | use MailPoetVendor\Symfony\Component\Validator\ConstraintValidator; |
| 11 | use MailPoetVendor\Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 12 | use MailPoetVendor\Symfony\Component\Validator\Exception\LogicException; |
| 13 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 14 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 15 | class BicValidator extends ConstraintValidator |
| 16 | { |
| 17 | // Reference: https://www.iban.com/structure |
| 18 | private const BIC_COUNTRY_TO_IBAN_COUNTRY_MAP = [ |
| 19 | // FR includes: |
| 20 | 'GF' => 'FR', |
| 21 | // French Guiana |
| 22 | 'PF' => 'FR', |
| 23 | // French Polynesia |
| 24 | 'TF' => 'FR', |
| 25 | // French Southern Territories |
| 26 | 'GP' => 'FR', |
| 27 | // Guadeloupe |
| 28 | 'MQ' => 'FR', |
| 29 | // Martinique |
| 30 | 'YT' => 'FR', |
| 31 | // Mayotte |
| 32 | 'NC' => 'FR', |
| 33 | // New Caledonia |
| 34 | 'RE' => 'FR', |
| 35 | // Reunion |
| 36 | 'BL' => 'FR', |
| 37 | // Saint Barthelemy |
| 38 | 'MF' => 'FR', |
| 39 | // Saint Martin (French part) |
| 40 | 'PM' => 'FR', |
| 41 | // Saint Pierre and Miquelon |
| 42 | 'WF' => 'FR', |
| 43 | // Wallis and Futuna Islands |
| 44 | // GB includes: |
| 45 | 'JE' => 'GB', |
| 46 | // Jersey |
| 47 | 'IM' => 'GB', |
| 48 | // Isle of Man |
| 49 | 'GG' => 'GB', |
| 50 | // Guernsey |
| 51 | 'VG' => 'GB', |
| 52 | // British Virgin Islands |
| 53 | // FI includes: |
| 54 | 'AX' => 'FI', |
| 55 | // Aland Islands |
| 56 | // ES includes: |
| 57 | 'IC' => 'ES', |
| 58 | // Canary Islands |
| 59 | 'EA' => 'ES', |
| 60 | ]; |
| 61 | private $propertyAccessor; |
| 62 | public function __construct(?PropertyAccessor $propertyAccessor = null) |
| 63 | { |
| 64 | $this->propertyAccessor = $propertyAccessor; |
| 65 | } |
| 66 | public function validate($value, Constraint $constraint) |
| 67 | { |
| 68 | if (!$constraint instanceof Bic) { |
| 69 | throw new UnexpectedTypeException($constraint, Bic::class); |
| 70 | } |
| 71 | if (null === $value || '' === $value) { |
| 72 | return; |
| 73 | } |
| 74 | if (!\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) { |
| 75 | throw new UnexpectedValueException($value, 'string'); |
| 76 | } |
| 77 | $canonicalize = \str_replace(' ', '', $value); |
| 78 | // the bic must be either 8 or 11 characters long |
| 79 | if (!\in_array(\strlen($canonicalize), [8, 11])) { |
| 80 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Bic::INVALID_LENGTH_ERROR)->addViolation(); |
| 81 | return; |
| 82 | } |
| 83 | // must contain alphanumeric values only |
| 84 | if (!\ctype_alnum($canonicalize)) { |
| 85 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Bic::INVALID_CHARACTERS_ERROR)->addViolation(); |
| 86 | return; |
| 87 | } |
| 88 | $bicCountryCode = \substr($canonicalize, 4, 2); |
| 89 | if (!isset(self::BIC_COUNTRY_TO_IBAN_COUNTRY_MAP[$bicCountryCode]) && !Countries::exists($bicCountryCode)) { |
| 90 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)->addViolation(); |
| 91 | return; |
| 92 | } |
| 93 | // should contain uppercase characters only |
| 94 | if (\strtoupper($canonicalize) !== $canonicalize) { |
| 95 | $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Bic::INVALID_CASE_ERROR)->addViolation(); |
| 96 | return; |
| 97 | } |
| 98 | // check against an IBAN |
| 99 | $iban = $constraint->iban; |
| 100 | $path = $constraint->ibanPropertyPath; |
| 101 | if ($path && null !== ($object = $this->context->getObject())) { |
| 102 | try { |
| 103 | $iban = $this->getPropertyAccessor()->getValue($object, $path); |
| 104 | } catch (NoSuchPropertyException $e) { |
| 105 | throw new ConstraintDefinitionException(\sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, \get_debug_type($constraint)) . $e->getMessage(), 0, $e); |
| 106 | } catch (UninitializedPropertyException $e) { |
| 107 | $iban = null; |
| 108 | } |
| 109 | } |
| 110 | if (!$iban) { |
| 111 | return; |
| 112 | } |
| 113 | $ibanCountryCode = \substr($iban, 0, 2); |
| 114 | if (\ctype_alpha($ibanCountryCode) && !$this->bicAndIbanCountriesMatch($bicCountryCode, $ibanCountryCode)) { |
| 115 | $this->context->buildViolation($constraint->ibanMessage)->setParameter('{{ value }}', $this->formatValue($value))->setParameter('{{ iban }}', $iban)->setCode(Bic::INVALID_IBAN_COUNTRY_CODE_ERROR)->addViolation(); |
| 116 | } |
| 117 | } |
| 118 | private function getPropertyAccessor() : PropertyAccessor |
| 119 | { |
| 120 | if (null === $this->propertyAccessor) { |
| 121 | if (!\class_exists(PropertyAccess::class)) { |
| 122 | throw new LogicException('Unable to use property path as the Symfony PropertyAccess component is not installed.'); |
| 123 | } |
| 124 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
| 125 | } |
| 126 | return $this->propertyAccessor; |
| 127 | } |
| 128 | private function bicAndIbanCountriesMatch(string $bicCountryCode, string $ibanCountryCode) : bool |
| 129 | { |
| 130 | return $ibanCountryCode === $bicCountryCode || $ibanCountryCode === (self::BIC_COUNTRY_TO_IBAN_COUNTRY_MAP[$bicCountryCode] ?? null); |
| 131 | } |
| 132 | } |
| 133 |