Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CardSchemeValidator.php

function CardSchemeValidator::validate

Validates a creditcard belongs to a specified scheme.

Overrides ConstraintValidatorInterface::validate

File

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

Class

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

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate(mixed $value, Constraint $constraint) : void {
    if (!$constraint instanceof CardScheme) {
        throw new UnexpectedTypeException($constraint, CardScheme::class);
    }
    if (null === $value || '' === $value) {
        return;
    }
    if (!is_numeric($value)) {
        $this->context
            ->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->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this->formatValue($value))
        ->setCode(CardScheme::INVALID_FORMAT_ERROR)
        ->addViolation();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal