From 81dbc7efc2e36d7836e58e4324d280b691e85b02 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sat, 16 Feb 2019 19:15:05 +0100 Subject: [PATCH 1/2] [Validator] Add option allowLowerCase and allowSpaces in BIC constraint --- .../Component/Validator/Constraints/Bic.php | 4 +++ .../Validator/Constraints/BicValidator.php | 11 +++++++- .../Tests/Constraints/BicValidatorTest.php | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Bic.php b/src/Symfony/Component/Validator/Constraints/Bic.php index 9637d9576afa8..3f24fecd9b30e 100644 --- a/src/Symfony/Component/Validator/Constraints/Bic.php +++ b/src/Symfony/Component/Validator/Constraints/Bic.php @@ -31,6 +31,7 @@ class Bic extends Constraint const INVALID_COUNTRY_CODE_ERROR = '1ce76f8d-3c1f-451c-9e62-fe9c3ed486ae'; const INVALID_CASE_ERROR = '11884038-3312-4ae5-9d04-699f782130c7'; const INVALID_IBAN_COUNTRY_CODE_ERROR = '29a2c3bb-587b-4996-b6f5-53081364cea5'; + const INVALID_SPACES_ERROR = 'cf0325f4-5c35-47a8-a360-51dac05b6540'; protected static $errorNames = [ self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR', @@ -38,12 +39,15 @@ class Bic extends Constraint self::INVALID_BANK_CODE_ERROR => 'INVALID_BANK_CODE_ERROR', self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR', self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR', + self::INVALID_SPACES_ERROR => 'INVALID_SPACES_ERROR', ]; public $message = 'This is not a valid Business Identifier Code (BIC).'; public $ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.'; public $iban; public $ibanPropertyPath; + public $allowLowerCase = false; + public $allowSpaces = true; public function __construct($options = null) { diff --git a/src/Symfony/Component/Validator/Constraints/BicValidator.php b/src/Symfony/Component/Validator/Constraints/BicValidator.php index 3036d200e0668..b0fd06babb8a3 100644 --- a/src/Symfony/Component/Validator/Constraints/BicValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BicValidator.php @@ -72,6 +72,15 @@ public function validate($value, Constraint $constraint) throw new UnexpectedValueException($value, 'string'); } + if (!$constraint->allowSpaces && false !== strpos($value, ' ')) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Bic::INVALID_SPACES_ERROR) + ->addViolation(); + + return; + } + $canonicalize = str_replace(' ', '', $value); // the bic must be either 8 or 11 characters long @@ -122,7 +131,7 @@ public function validate($value, Constraint $constraint) } // should contain uppercase characters only - if (strtoupper($canonicalize) !== $canonicalize) { + if (!$constraint->allowLowerCase && strtoupper($canonicalize) !== $canonicalize) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Bic::INVALID_CASE_ERROR) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php index 174bbb2863437..ff2386488e42c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php @@ -256,6 +256,34 @@ public function getValidBicSpecialCases() yield ['BARCGGSA', 'GB12 CPBK 0892 9965 0449 911']; yield ['BARCVGSA', 'GB12 CPBK 0892 9965 0449 911']; } + + public function testValidBicAllowLowerCase() + { + $constraint = new Bic([ + 'allowLowerCase' => true, + ]); + + $this->validator->validate('DeutAT2LXXX', $constraint); + + $this->assertNoViolation(); + } + + public function testInvalidBicDisallowSpaces() + { + $bic = 'DEUTA T2LX XX'; + + $constraint = new Bic([ + 'allowSpaces' => false, + 'message' => 'myMessage', + ]); + + $this->validator->validate($bic, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$bic.'"') + ->setCode(Bic::INVALID_SPACES_ERROR) + ->assertRaised(); + } } class BicComparisonTestClass From 2c61afefb90d42c7884d09e62ffd59e8ea550a23 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sat, 16 Feb 2019 19:39:12 +0100 Subject: [PATCH 2/2] [Validator] Add option allowLowerCase and allowSpaces in IBAN constraint --- .../Component/Validator/Constraints/Iban.php | 6 ++ .../Validator/Constraints/IbanValidator.php | 20 +++++- .../Tests/Constraints/IbanValidatorTest.php | 61 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Iban.php b/src/Symfony/Component/Validator/Constraints/Iban.php index 231f8c838dd57..cea4f419c4bfb 100644 --- a/src/Symfony/Component/Validator/Constraints/Iban.php +++ b/src/Symfony/Component/Validator/Constraints/Iban.php @@ -28,6 +28,8 @@ class Iban extends Constraint const CHECKSUM_FAILED_ERROR = 'b9401321-f9bf-4dcb-83c1-f31094440795'; const INVALID_FORMAT_ERROR = 'c8d318f1-2ecc-41ba-b983-df70d225cf5a'; const NOT_SUPPORTED_COUNTRY_CODE_ERROR = 'e2c259f3-4b46-48e6-b72e-891658158ec8'; + const INVALID_CASE_ERROR = 'd58f8108-22b8-4a4b-a151-09b40fa416a6'; + const INVALID_SPACES_ERROR = 'cf0325f4-5c35-47a8-a360-51dac05b6540'; protected static $errorNames = [ self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR', @@ -35,7 +37,11 @@ class Iban extends Constraint self::CHECKSUM_FAILED_ERROR => 'CHECKSUM_FAILED_ERROR', self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR', self::NOT_SUPPORTED_COUNTRY_CODE_ERROR => 'NOT_SUPPORTED_COUNTRY_CODE_ERROR', + self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR', + self::INVALID_SPACES_ERROR => 'INVALID_SPACES_ERROR', ]; public $message = 'This is not a valid International Bank Account Number (IBAN).'; + public $allowLowerCase = true; + public $allowSpaces = true; } diff --git a/src/Symfony/Component/Validator/Constraints/IbanValidator.php b/src/Symfony/Component/Validator/Constraints/IbanValidator.php index d6530ab101702..5584e0a47ec64 100644 --- a/src/Symfony/Component/Validator/Constraints/IbanValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IbanValidator.php @@ -156,6 +156,24 @@ public function validate($value, Constraint $constraint) $value = (string) $value; + if (!$constraint->allowSpaces && false !== strpos($value, ' ')) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Iban::INVALID_SPACES_ERROR) + ->addViolation(); + + return; + } + + if (!$constraint->allowLowerCase && strtoupper($value) !== $value) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Iban::INVALID_CASE_ERROR) + ->addViolation(); + + return; + } + // Remove spaces and convert to uppercase $canonicalized = str_replace(' ', '', strtoupper($value)); @@ -182,7 +200,7 @@ public function validate($value, Constraint $constraint) } // ...have a format available - if (!array_key_exists($countryCode, self::$formats)) { + if (!\array_key_exists($countryCode, self::$formats)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Iban::NOT_SUPPORTED_COUNTRY_CODE_ERROR) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php index ba426799ca4e1..95840ba470cdf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -432,6 +432,67 @@ public function getIbansWithInvalidCountryCode() ]; } + public function testValidIbanAllowLowerCase() + { + $iban = 'fr14 2004 1010 0505 0001 3M02 606'; + + $constraint = new Iban([ + 'message' => 'myMessage', + 'allowLowerCase' => true, + ]); + + $this->validator->validate($iban, $constraint); + + $this->assertNoViolation(); + } + + public function testInvalidIbanDisallowLowerCase() + { + $iban = 'fr14 2004 1010 0505 0001 3M02 606'; + + $constraint = new Iban([ + 'message' => 'myMessage', + 'allowLowerCase' => false, + ]); + + $this->validator->validate($iban, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$iban.'"') + ->setCode(Iban::INVALID_CASE_ERROR) + ->assertRaised(); + } + + public function testValidIbanAllowSpaces() + { + $iban = 'FR14 2004 1010 0505 0001 3M02 606'; + + $constraint = new Iban([ + 'allowSpaces' => true, + ]); + + $this->validator->validate($iban, $constraint); + + $this->assertNoViolation(); + } + + public function testInvalidIbanDisallowSpaces() + { + $iban = 'FR14 2004 1010 0505 0001 3M02 606'; + + $constraint = new Iban([ + 'message' => 'myMessage', + 'allowSpaces' => false, + ]); + + $this->validator->validate($iban, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$iban.'"') + ->setCode(Iban::INVALID_SPACES_ERROR) + ->assertRaised(); + } + private function assertViolationRaised($iban, $code) { $constraint = new Iban([