2013-01-10 16:13:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
2014-03-10 12:51:04 +00:00
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2013-01-10 16:13:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Manuel Reinhard <manu@sprain.ch>
|
|
|
|
* @author Michael Schummel
|
2014-04-10 17:24:43 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2014-12-21 17:00:50 +00:00
|
|
|
*
|
2013-01-10 16:13:11 +00:00
|
|
|
* @link http://www.michael-schummel.de/2007/10/05/iban-prufung-mit-php/
|
|
|
|
*/
|
|
|
|
class IbanValidator extends ConstraintValidator
|
|
|
|
{
|
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2013-01-10 16:13:11 +00:00
|
|
|
*/
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
|
|
{
|
2014-03-10 12:51:04 +00:00
|
|
|
if (!$constraint instanceof Iban) {
|
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Iban');
|
|
|
|
}
|
|
|
|
|
2013-04-20 14:26:53 +01:00
|
|
|
if (null === $value || '' === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:24:43 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
2014-07-24 13:14:19 +01:00
|
|
|
$value = (string) $value;
|
|
|
|
|
2014-04-10 17:24:43 +01:00
|
|
|
// Remove spaces
|
|
|
|
$canonicalized = str_replace(' ', '', $value);
|
|
|
|
|
2014-09-24 10:48:52 +01:00
|
|
|
// The IBAN must have at least 4 characters...
|
2014-04-10 17:24:43 +01:00
|
|
|
if (strlen($canonicalized) < 4) {
|
2014-09-24 10:48:52 +01:00
|
|
|
$this->buildViolation($constraint->message)
|
|
|
|
->setParameter('{{ value }}', $this->formatValue($value))
|
2014-09-24 11:08:18 +01:00
|
|
|
->setCode(Iban::TOO_SHORT_ERROR)
|
2014-09-24 10:48:52 +01:00
|
|
|
->addViolation();
|
2013-12-23 18:04:13 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-24 10:48:52 +01:00
|
|
|
// ...start with a country code...
|
|
|
|
if (!ctype_alpha($canonicalized{0}) || !ctype_alpha($canonicalized{1})) {
|
|
|
|
$this->buildViolation($constraint->message)
|
|
|
|
->setParameter('{{ value }}', $this->formatValue($value))
|
2014-09-24 11:08:18 +01:00
|
|
|
->setCode(Iban::INVALID_COUNTRY_CODE_ERROR)
|
2014-09-24 10:48:52 +01:00
|
|
|
->addViolation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...contain only digits and characters...
|
|
|
|
if (!ctype_alnum($canonicalized)) {
|
|
|
|
$this->buildViolation($constraint->message)
|
|
|
|
->setParameter('{{ value }}', $this->formatValue($value))
|
2014-09-24 11:08:18 +01:00
|
|
|
->setCode(Iban::INVALID_CHARACTERS_ERROR)
|
2014-09-24 10:48:52 +01:00
|
|
|
->addViolation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...and contain uppercase characters only
|
|
|
|
if ($canonicalized !== strtoupper($canonicalized)) {
|
|
|
|
$this->buildViolation($constraint->message)
|
|
|
|
->setParameter('{{ value }}', $this->formatValue($value))
|
2014-09-24 11:08:18 +01:00
|
|
|
->setCode(Iban::INVALID_CASE_ERROR)
|
2014-09-24 10:48:52 +01:00
|
|
|
->addViolation();
|
2013-04-20 14:26:53 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:24:43 +01:00
|
|
|
// Move the first four characters to the end
|
|
|
|
// e.g. CH93 0076 2011 6238 5295 7
|
|
|
|
// -> 0076 2011 6238 5295 7 CH93
|
|
|
|
$canonicalized = substr($canonicalized, 4).substr($canonicalized, 0, 4);
|
2013-04-20 14:26:53 +01:00
|
|
|
|
2014-04-10 17:24:43 +01:00
|
|
|
// Convert all remaining letters to their ordinals
|
|
|
|
// The result is an integer, which is too large for PHP's int
|
|
|
|
// data type, so we store it in a string instead.
|
|
|
|
// e.g. 0076 2011 6238 5295 7 CH93
|
|
|
|
// -> 0076 2011 6238 5295 7 121893
|
|
|
|
$checkSum = $this->toBigInt($canonicalized);
|
2013-04-20 14:26:53 +01:00
|
|
|
|
2014-04-10 17:24:43 +01:00
|
|
|
// Do a modulo-97 operation on the large integer
|
|
|
|
// We cannot use PHP's modulo operator, so we calculate the
|
|
|
|
// modulo step-wisely instead
|
|
|
|
if (1 !== $this->bigModulo97($checkSum)) {
|
2014-09-24 10:48:52 +01:00
|
|
|
$this->buildViolation($constraint->message)
|
|
|
|
->setParameter('{{ value }}', $this->formatValue($value))
|
2014-09-24 11:08:18 +01:00
|
|
|
->setCode(Iban::CHECKSUM_FAILED_ERROR)
|
2014-09-24 10:48:52 +01:00
|
|
|
->addViolation();
|
2013-04-20 14:26:53 +01:00
|
|
|
}
|
2013-01-10 16:13:11 +00:00
|
|
|
}
|
2014-04-10 17:24:43 +01:00
|
|
|
|
|
|
|
private function toBigInt($string)
|
|
|
|
{
|
|
|
|
$chars = str_split($string);
|
|
|
|
$bigInt = '';
|
|
|
|
|
|
|
|
foreach ($chars as $char) {
|
|
|
|
// Convert uppercase characters to ordinals, starting with 10 for "A"
|
|
|
|
if (ctype_upper($char)) {
|
|
|
|
$bigInt .= (ord($char) - 55);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simply append digits
|
|
|
|
$bigInt .= $char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bigInt;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bigModulo97($bigInt)
|
|
|
|
{
|
|
|
|
$parts = str_split($bigInt, 7);
|
|
|
|
$rest = 0;
|
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$rest = ($rest.$part) % 97;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rest;
|
|
|
|
}
|
2013-01-10 16:13:11 +00:00
|
|
|
}
|