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

Breadcrumb

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

function NoSuspiciousCharactersValidator::validate

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/NoSuspiciousCharactersValidator.php, line 59

Class

NoSuspiciousCharactersValidator
@author Mathieu Lechat <math.lechat@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate(mixed $value, Constraint $constraint) : void {
    if (!$constraint instanceof NoSuspiciousCharacters) {
        throw new UnexpectedTypeException($constraint, NoSuspiciousCharacters::class);
    }
    if (null === $value || '' === $value) {
        return;
    }
    if (!\is_scalar($value) && !$value instanceof \Stringable) {
        throw new UnexpectedValueException($value, 'string');
    }
    if ('' === ($value = (string) $value)) {
        return;
    }
    $checker = new \Spoofchecker();
    $checks = $constraint->checks;
    if (method_exists($checker, 'setRestrictionLevel')) {
        $checks |= self::CHECK_RESTRICTION_LEVEL;
        $checker->setRestrictionLevel($constraint->restrictionLevel ?? NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE);
    }
    elseif (NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL === $constraint->restrictionLevel) {
        $checks |= self::CHECK_CHAR_LIMIT;
    }
    elseif (NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT === $constraint->restrictionLevel) {
        $checks |= self::CHECK_SINGLE_SCRIPT | self::CHECK_CHAR_LIMIT;
    }
    elseif ($constraint->restrictionLevel) {
        throw new LogicException('You can only use one of RESTRICTION_LEVEL_NONE, RESTRICTION_LEVEL_MINIMAL or RESTRICTION_LEVEL_SINGLE_SCRIPT with intl compiled against ICU < 58.');
    }
    else {
        $checks |= self::CHECK_SINGLE_SCRIPT;
    }
    $checker->setAllowedLocales(implode(',', $constraint->locales ?? $this->defaultLocales));
    $checker->setChecks($checks);
    if (!$checker->isSuspicious($value, $errorCode)) {
        return;
    }
    foreach (self::CHECK_ERROR as $check => $error) {
        if (\PHP_VERSION_ID < 80204) {
            if (!($checks & $check)) {
                continue;
            }
            $checker->setChecks($check);
            if (!$checker->isSuspicious($value)) {
                continue;
            }
        }
        elseif (!($errorCode & $check)) {
            continue;
        }
        $this->context
            ->buildViolation($constraint->{$error['messageProperty']})
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode($error['code'])
            ->addViolation();
    }
}

API Navigation

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