8000 [Validator] added BIC (SWIFT-BIC) validation constraint · symfony/symfony@d6471b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6471b3

Browse files
mvhirschfabpot
authored andcommitted
[Validator] added BIC (SWIFT-BIC) validation constraint
1 parent 7f745d7 commit d6471b3

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* added the BIC (SWIFT-Code) validator
8+
49
2.7.0
510
-----
611

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* @Annotation
9+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
10+
*
11+
* @author Michael Hirschler <michael.vhirsch@gmail.com>
12+
*
13+
* @api
14+
*/
15+
class Bic extends Constraint
16+
{
17+
const INVALID_LENGTH_ERROR = '66dad313-af0b-4214-8566-6c799be9789c';
18+
const INVALID_CHARACTERS_ERROR = 'f424c529-7add-4417-8f2d-4b656e4833e2';
19+
const INVALID_BANK_CODE_ERROR = '00559357-6170-4f29-aebd-d19330aa19cf';
20+
const INVALID_COUNTRY_CODE_ERROR = '1ce76f8d-3c1f-451c-9e62-fe9c3ed486ae';
21+
const INVALID_CASE_ERROR = '11884038-3312-4ae5-9d04-699f782130c7';
22+
23+
protected static $errorNames = array(
24+
self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR',
25+
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
26+
self::INVALID_BANK_CODE_ERROR => 'INVALID_BANK_CODE_ERROR',
27+
self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
28+
self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
29+
);
30+
31+
public $message = 'This is not a valid Business Identifier Code (BIC).';
32+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
use Symfony\Component\Validator\ConstraintValidator;
7+
8+
/**
9+
* @author Michael Hirschler <michael.vhirsch@gmail.com>
10+
*
11+
* @link https://en.wikipedia.org/wiki/ISO_9362#Structure
12+
*
13+
* @api
14+
*/
15+
class BicValidator extends ConstraintValidator
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function validate($value, Constraint $constraint)
21+
{
22+
if (null === $value || '' === $value) {
23+
return;
24+
}
25+
26+
$canonicalize = str_replace(' ', '', $value);
27+
28+
// the bic must be either 8 or 11 characters long
29+
if (!in_array(strlen($canonicalize), array(8, 11))) {
30+
$this->context->buildViolation($constraint->message)
31+
->setParameter('{{ value }}', $this->formatValue($value))
32+
->setCode(Bic::INVALID_LENGTH_ERROR)
33+
->addViolation();
34+
}
35+
36+
// must contain alphanumeric values only
37+
if (!ctype_alnum($canonicalize)) {
38+
$this->context->buildViolation($constraint->message)
39+
->setParameter('{{ value }}', $this->formatValue($value))
40+
->setCode(Bic::INVALID_CHARACTERS_ERROR)
41+
->addViolation();
42+
}
43+
44+
// first 4 letters must be alphabetic (bank code)
45+
if (!ctype_alpha(substr($canonicalize, 0, 4))) {
46+
$this->context->buildViolation($constraint->message)
47+
->setParameter('{{ value }}', $this->formatValue($value))
48+
->setCode(Bic::INVALID_BANK_CODE_ERROR)
49+
->addViolation();
50+
}
51+
52+
// next 2 letters must be alphabetic (country code)
53+
if (!ctype_alpha(substr($canonicalize, 4, 2))) {
54+
$this->context->buildViolation($constraint->message)
55+
->setParameter('{{ value }}', $this->formatValue($value))
56+
->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)
57+
->addViolation();
58+
}
59+
60+
// should contain uppercase characters only
61+
if (strtoupper($canonicalize) !== $canonicalize) {
62+
$this->context->buildViolation($constraint->message)
63+
->setParameter('{{ value }}', $this->formatValue($value))
64+
->setCode(Bic::INVALID_CASE_ERROR)
65+
->addViolation();
66+
}
67+
}
68+
}

src/Symfony/Component/Validator/Resources/translations/validators.de.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@
310310
<source>This value does not match the expected {{ charset }} charset.</source>
311311
<target>Dieser Wert entspricht nicht dem erwarteten Zeichensatz {{ charset }}.</target>
312312
</trans-unit>
313+
<trans-unit id="81">
314+
<source>This is not a valid Business Identifier Code (BIC).</source>
315+
<target>Dieser Wert ist kein gültiger BIC.</target>
316+
</trans-unit>
313317
</body>
314318
</file>
315319
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@
310310
<source>This value does not match the expected {{ charset }} charset.</source>
311311
<target>This value does not match the expected {{ charset }} charset.</target>
312312
</trans-unit>
313+
<trans-unit id="81">
314+
<source>This is not a valid Business Identifier Code (BIC).</source>
315+
<target>This is not a valid Business Identifier Code (BIC).</target>
316+
</trans-unit>
313317
</body>
314318
</file>
315319
</xliff>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Constraints;
4+
5+
use Symfony\Component\Validator\Constraints\BicValidator;
6+
use Symfony\Component\Validator\Constraints\Bic;
7+
8+
class BicValidatorTest extends AbstractConstraintValidatorTest
9+
{
10+
protected function createValidator()
11+
{
12+
return new BicValidator();
13+
}
14+
15+
public function testNullIsValid()
16+
{
17+
$this->validator->validate(null, new Bic());
18+
19+
$this->assertNoViolation();
20+
}
21+
22+
public function testEmptyStringIsValid()
23+
{
24+
$this->validator->validate('', new Bic());
25+
26+
$this->assertNoViolation();
27+
}
28+
29+
/**
30+
* @dataProvider getValidBics
31+
*/
32+
public function testValidBics($bic)
33+
{
34+
$this->validator->validate($bic, new Bic());
35+
36+
$this->assertNoViolation();
37+
}
38+
39+
public function getValidBics()
40+
{
41+
// http://formvalidation.io/validators/bic/
42+
return array(
43+
array('ASPKAT2LXXX'),
44+
array('ASPKAT2L'),
45+
array('DSBACNBXSHA'),
46+
array('UNCRIT2B912'),
47+
array('DABADKKK'),
48+
array('RZOOAT2L303'),
49+
);
50+
}
51+
52+
/**
53+
* @dataProvider getInvalidBics
54+
*/
55+
public function testInvalidBics($bic, $code)
56+
{
57+
$constraint = new Bic(array(
58+
'message' => 'myMessage',
59+
));
60+
61+
$this->validator->validate($bic, $constraint);
62+
63+
$this->buildViolation('myMessage')
64+
->setParameter('{{ value }}', '"'.$bic.'"')
65+
->setCode($code)
66+
->assertRaised();
67+
}
68+
69+
public function getInvalidBics()
70+
{
71+
return array(
72+
array('DEUTD', Bic::INVALID_LENGTH_ERROR),
73+
array('ASPKAT2LXX', Bic::INVALID_LENGTH_ERROR),
74+
array('ASPKAT2LX', Bic::INVALID_LENGTH_ERROR),
75+
array('ASPKAT2LXXX1', Bic::INVALID_LENGTH_ERROR),
76+
array('DABADKK', Bic::INVALID_LENGTH_ERROR),
77+
array('1SBACNBXSHA', Bic::INVALID_BANK_CODE_ERROR),
78+
array('RZ00AT2L303', Bic::INVALID_BANK_CODE_ERROR),
79+
array('D2BACNBXSHA', Bic::INVALID_BANK_CODE_ERROR),
80+
array('DS3ACNBXSHA', Bic::INVALID_BANK_CODE_ERROR),
81+
array('DSB4CNBXSHA', Bic::INVALID_BANK_CODE_ERROR),
82+
array('DEUT12HH', Bic::INVALID_COUNTRY_CODE_ERROR),
83+
array('DSBAC6BXSHA', Bic::INVALID_COUNTRY_CODE_ERROR),
84+
array('DSBA5NBXSHA', Bic::INVALID_COUNTRY_CODE_ERROR),
85+
86+
// branch code error
87+
array('THISSVAL1D]', Bic::INVALID_CHARACTERS_ERROR),
88+
89+
// location code error
90+
array('DEUTDEF]', Bic::INVALID_CHARACTERS_ERROR),
91+
92+
// lower case values are invalid
93+
array('DeutAT2LXXX', Bic::INVALID_CASE_ERROR),
94+
array('DEUTAT2lxxx', Bic::INVALID_CASE_ERROR),
95+
);
96+
}
97+
}

0 commit comments

Comments
 (0)
0