[Validator] fixed CS

This commit is contained in:
Fabien Potencier 2013-04-20 15:26:53 +02:00
parent 3a507e0d86
commit 44be949045
5 changed files with 66 additions and 80 deletions

View File

@ -18,49 +18,45 @@ use Symfony\Component\Validator\ConstraintValidator;
* @author Manuel Reinhard <manu@sprain.ch>
* @author Michael Schummel
* @link http://www.michael-schummel.de/2007/10/05/iban-prufung-mit-php/
*
* @api
*/
class IbanValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}
if (null === $value || '' === $value) {
return;
}
$teststring = preg_replace('/\s+/', '', $value);
$teststring = preg_replace('/\s+/', '', $value);
if (strlen($teststring) < 4) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
return;
}
if (strlen($teststring) < 4) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
$teststring = substr($teststring, 4)
. strval(ord($teststring{0}) - 55)
. strval(ord($teststring{1}) - 55)
. substr($teststring, 2, 2);
return;
}
$letterToInt = function ($letter) {
return intval(ord(strtolower($letter[0])) - 87);
};
$teststring = substr($teststring, 4)
.strval(ord($teststring{0}) - 55)
.strval(ord($teststring{1}) - 55)
.substr($teststring, 2, 2);
$teststring = preg_replace_callback('/[A-Za-z]/', $letterToInt, $teststring);
$teststring = preg_replace_callback('/[A-Za-z]/', function ($letter) {
return intval(ord(strtolower($letter[0])) - 87);
}, $teststring);
$rest=0;
$strlen = strlen($teststring);
for ($pos = 0; $pos < $strlen; $pos += 7) {
$part = strval($rest) . substr($teststring, $pos, 7);
$rest = intval($part) % 97;
}
$rest = 0;
$strlen = strlen($teststring);
for ($pos = 0; $pos < $strlen; $pos += 7) {
$part = strval($rest).substr($teststring, $pos, 7);
$rest = intval($part) % 97;
}
if ($rest != 1) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
return;
}
if ($rest != 1) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
return;
}
}
}

View File

@ -222,10 +222,10 @@
<source>Unsupported card type or invalid card number.</source>
<target>Nicht unterstützer Kartentyp oder ungültige Kartennummer.</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Dieser Wert ist keine gültige IBAN-Kontonummer.</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Dieser Wert ist keine gültige IBAN-Kontonummer.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@ -222,10 +222,10 @@
<source>Unsupported card type or invalid card number.</source>
<target>Unsupported card type or invalid card number.</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>This is not a valid International Bank Account Number (IBAN).</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>This is not a valid International Bank Account Number (IBAN).</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@ -222,10 +222,10 @@
<source>Unsupported card type or invalid card number.</source>
<target>Type de carte non supporté ou numéro invalide.</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Le numéro IBAN saisi n'est pas valide (International Bank Account Number).</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Le numéro IBAN (International Bank Account Number) saisi n'est pas valide.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@ -21,31 +21,23 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
$this->validator = new IbanValidator();
$this->validator->initialize($this->context);
}
protected function tearDown()
{
$this->context = null;
$this->validator = null;
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
$this->validator = new IbanValidator();
$this->validator->initialize($this->context);
}
public function testNullIsValid()
{
$this->context->expects($this->never())
->method('addViolation');
$this->context->expects($this->never())->method('addViolation');
$this->validator->validate(null, new Iban());
$this->validator->validate(null, new Iban());
}
public function testEmptyStringIsValid()
{
$this->context->expects($this->never())
->method('addViolation');
$this->context->expects($this->never())->method('addViolation');
$this->validator->validate('', new Iban());
$this->validator->validate('', new Iban());
}
/**
@ -53,10 +45,9 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase
*/
public function testValidIbans($iban)
{
$this->context->expects($this->never())
->method('addViolation');
$this->context->expects($this->never())->method('addViolation');
$this->validator->validate($iban, new Iban());
$this->validator->validate($iban, new Iban());
}
public function getValidIbans()
@ -158,7 +149,6 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase
array('TN5914207207100707129648'), //Tunisia
array('TR330006100519786457841326'), //Turkey
array('AE260211000000230064016'), //United Arab Emirates
);
}
@ -167,31 +157,31 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidIbans($iban)
{
$constraint = new Iban(array(
'message' => 'myMessage'
));
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $iban,
$constraint = new Iban(array(
'message' => 'myMessage'
));
$this->validator->validate($iban, $constraint);
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $iban,
));
$this->validator->validate($iban, $constraint);
}
public function getInvalidIbans()
{
return array(
array('CH93 0076 2011 6238 5295'),
array('CH930076201162385295'),
array('GB29 RBOS 6016 1331 9268 19'),
array('CH930072011623852957'),
array('NL39 RASO 0300 0652 64'),
array('NO93 8601117 947'),
array('CY170020 128 0000 0012 0052 7600'),
array('foo'),
array('123'),
);
return array(
array('CH93 0076 2011 6238 5295'),
array('CH930076201162385295'),
array('GB29 RBOS 6016 1331 9268 19'),
array('CH930072011623852957'),
array('NL39 RASO 0300 0652 64'),
array('NO93 8601117 947'),
array('CY170020 128 0000 0012 0052 7600'),
array('foo'),
array('123'),
);
}
}