You are here

public function CardSchemeValidator::validate in Plug 7

Validates a creditcard belongs to a specified scheme.

Parameters

mixed $value:

Constraint $constraint:

Overrides ConstraintValidatorInterface::validate

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/CardSchemeValidator.php, line 90

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 (!$constraint instanceof CardScheme) {
    throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\CardScheme');
  }
  if (null === $value || '' === $value) {
    return;
  }
  if (!is_numeric($value)) {
    $this
      ->buildViolation($constraint->message)
      ->setParameter('{{ value }}', $this
      ->formatValue($value))
      ->setCode(CardScheme::NOT_NUMERIC_ERROR)
      ->addViolation();
    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
    ->buildViolation($constraint->message)
    ->setParameter('{{ value }}', $this
    ->formatValue($value))
    ->setCode(CardScheme::INVALID_FORMAT_ERROR)
    ->addViolation();
}