You are here

public function CardSchemeValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/CardSchemeValidator.php \Symfony\Component\Validator\Constraints\CardSchemeValidator::validate()

Validates a creditcard belongs to a specified scheme.

Parameters

mixed $value:

Constraint $constraint:

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/CardSchemeValidator.php, line 92

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)) {
    if ($this->context instanceof ExecutionContextInterface) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(CardScheme::NOT_NUMERIC_ERROR)
        ->addViolation();
    }
    else {
      $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;
      }
    }
  }
  if ($this->context instanceof ExecutionContextInterface) {
    $this->context
      ->buildViolation($constraint->message)
      ->setParameter('{{ value }}', $this
      ->formatValue($value))
      ->setCode(CardScheme::INVALID_FORMAT_ERROR)
      ->addViolation();
  }
  else {
    $this
      ->buildViolation($constraint->message)
      ->setParameter('{{ value }}', $this
      ->formatValue($value))
      ->setCode(CardScheme::INVALID_FORMAT_ERROR)
      ->addViolation();
  }
}