Skip to content

[IBAN validator] Fixed and improved handling of lowercase values #10489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
class Iban extends Constraint
{
public $message = 'This is not a valid International Bank Account Number (IBAN).';
public $acceptLowercase = false;
}
12 changes: 9 additions & 3 deletions src/Symfony/Component/Validator/Constraints/IbanValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ public function validate($value, Constraint $constraint)
return;
}

if ($constraint->acceptLowercase) {
$valueToCheck = strtoupper($value);
} else {
$valueToCheck = $value;
}

// An IBAN without a country code is not an IBAN.
if (0 === preg_match('/[A-Za-z]/', $value)) {
if (0 === preg_match('/[A-Z]/', $valueToCheck)) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));

return;
}

$teststring = preg_replace('/\s+/', '', $value);
$teststring = preg_replace('/\s+/', '', $valueToCheck);

if (strlen($teststring) < 4) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
Expand All @@ -50,7 +56,7 @@ public function validate($value, Constraint $constraint)
.strval(ord($teststring{1}) - 55)
.substr($teststring, 2, 2);

$teststring = preg_replace_callback('/[A-Za-z]/', function ($letter) {
$teststring = preg_replace_callback('/[A-Z]/', function ($letter) {
return intval(ord(strtolower($letter[0])) - 87);
}, $teststring);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,62 @@ public function testEmptyStringIsValid()
}

/**
* @dataProvider getValidIbans
* @dataProvider getValidIbansUppercase
*/
public function testValidIbans($iban)
public function testValidIbansUppercaseOnly($iban)
{
$this->context->expects($this->never())->method('addViolation');

$this->validator->validate($iban, new Iban());
}

public function getValidIbans()
/**
* @dataProvider getValidIbansUpperAndLowercase
*/
public function testValidIbansUpperAndLowercase($iban)
{
$this->context->expects($this->never())->method('addViolation');

$this->validator->validate($iban, new Iban(array('acceptLowercase' => true)));
}

/**
* @dataProvider getInvalidIbansWhenUppercaseOnly
*/
public function testInvalidIbansWhenUppercaseOnly($iban)
{
$constraint = new Iban(array(
'message' => 'myMessage'
));

$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $iban,
));

$this->validator->validate($iban, $constraint);
}

/**
* @dataProvider getInvalidIbansWhenUpperAndLowercase
*/
public function testInvalidIbansWhenUpperAndLowercase($iban)
{
$constraint = new Iban(array(
'message' => 'myMessage'
));

$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $iban,
));

$this->validator->validate($iban, $constraint);
}

public function getValidIbansUppercase()
{
return array(
array('CH9300762011623852957'), // Switzerland without spaces
Expand Down Expand Up @@ -151,25 +197,24 @@ public function getValidIbans()
);
}

/**
* @dataProvider getInvalidIbans
*/
public function testInvalidIbans($iban)
public function getValidIbansLowercase()
{
$constraint = new Iban(array(
'message' => 'myMessage'
));
$validIbansLowercase = array();

$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $iban,
));
foreach ($this->getValidIbansUppercase() as $iban) {
$iban[0] = strtolower($iban[0]);
$validIbansLowercase[] = $iban;
}

$this->validator->validate($iban, $constraint);
return $validIbansLowercase;
}

public function getValidIbansUpperAndLowercase()
{
return $this->getValidIbansUppercase() + $this->getValidIbansLowercase();
}

public function getInvalidIbans()
public function getInvalidIbansUppercase()
{
return array(
array('CH93 0076 2011 6238 5295'),
Expand All @@ -184,4 +229,26 @@ public function getInvalidIbans()
array('0750447346'),
);
}

public function getInvalidIbansLowercase()
{
$invalidIbansLowercase = array();

foreach ($this->getInvalidIbansUppercase() as $iban) {
$iban[0] = strtolower($iban[0]);
$invalidIbansLowercase[] = $iban;
}

return $invalidIbansLowercase;
}

public function getInvalidIbansWhenUppercaseOnly()
{
return $this->getInvalidIbansUppercase() + $this->getValidIbansLowercase();
}

public function getInvalidIbansWhenUpperAndLowercase()
{
return $this->getInvalidIbansUppercase() + $this->getInvalidIbansLowercase();
}
}