public function CardSchemeValidator::validate

Validates a creditcard belongs to a specified scheme.

Parameters

mixed $value:

Constraint $constraint:

Overrides ConstraintValidatorInterface::validate

File

drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Constraints/CardSchemeValidator.php, line 104

Class

CardSchemeValidator
Validates that a card number belongs to a specified scheme.

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate($value, Constraint $constraint) {
  if (null === $value || '' === $value) {
    return;
  }
  if (!is_numeric($value)) {
    $this->context
      ->addViolation($constraint->message);
    return;
  }
  $schemes = array_flip((array) $constraint->schemes);
  $schemeRegexes = array_intersect_key($this->schemes, $schemes);
  foreach ($schemeRegexes as $regexes) {
    foreach ($regexes as $regex) {
      if (preg_match($regex, $value)) {
        return;
      }
    }
  }
  $this->context
    ->addViolation($constraint->message);
}