You are here

class IbanValidator in Plug 7

@author Manuel Reinhard <manu@sprain.ch> @author Michael Schummel @author Bernhard Schussek <bschussek@gmail.com>

@link http://www.michael-schummel.de/2007/10/05/iban-prufung-mit-php/

Hierarchy

Expanded class hierarchy of IbanValidator

1 file declares its use of IbanValidator
IbanValidatorTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/IbanValidator.php, line 25

Namespace

Symfony\Component\Validator\Constraints
View source
class IbanValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (!$constraint instanceof Iban) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Iban');
    }
    if (null === $value || '' === $value) {
      return;
    }
    if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
      throw new UnexpectedTypeException($value, 'string');
    }
    $value = (string) $value;

    // Remove spaces
    $canonicalized = str_replace(' ', '', $value);

    // The IBAN must have at least 4 characters...
    if (strlen($canonicalized) < 4) {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Iban::TOO_SHORT_ERROR)
        ->addViolation();
      return;
    }

    // ...start with a country code...
    if (!ctype_alpha($canonicalized[0]) || !ctype_alpha($canonicalized[1])) {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Iban::INVALID_COUNTRY_CODE_ERROR)
        ->addViolation();
      return;
    }

    // ...contain only digits and characters...
    if (!ctype_alnum($canonicalized)) {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Iban::INVALID_CHARACTERS_ERROR)
        ->addViolation();
      return;
    }

    // ...and contain uppercase characters only
    if ($canonicalized !== strtoupper($canonicalized)) {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Iban::INVALID_CASE_ERROR)
        ->addViolation();
      return;
    }

    // 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);

    // 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);

    // 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)) {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Iban::CHECKSUM_FAILED_ERROR)
        ->addViolation();
    }
  }
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConstraintValidator::$context protected property
ConstraintValidator::buildViolation Deprecated protected function Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API.
ConstraintValidator::buildViolationInContext Deprecated protected function Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API.
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize 1
ConstraintValidator::OBJECT_TO_STRING constant Whether to cast objects with a "__toString()" method to strings.
ConstraintValidator::PRETTY_DATE constant Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s").
IbanValidator::bigModulo97 private function
IbanValidator::toBigInt private function
IbanValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate