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

Breadcrumb

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

class NoSuspiciousCharactersValidator

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

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\NoSuspiciousCharactersValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of NoSuspiciousCharactersValidator

File

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

Namespace

Symfony\Component\Validator\Constraints
View source
class NoSuspiciousCharactersValidator extends ConstraintValidator {
    private const CHECK_RESTRICTION_LEVEL = 16;
    private const CHECK_SINGLE_SCRIPT = 16;
    private const CHECK_CHAR_LIMIT = 64;
    private const CHECK_ERROR = [
        self::CHECK_RESTRICTION_LEVEL => [
            'code' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
            'messageProperty' => 'restrictionLevelMessage',
        ],
        NoSuspiciousCharacters::CHECK_INVISIBLE => [
            'code' => NoSuspiciousCharacters::INVISIBLE_ERROR,
            'messageProperty' => 'invisibleMessage',
        ],
        self::CHECK_CHAR_LIMIT => [
            'code' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
            'messageProperty' => 'restrictionLevelMessage',
        ],
        NoSuspiciousCharacters::CHECK_MIXED_NUMBERS => [
            'code' => NoSuspiciousCharacters::MIXED_NUMBERS_ERROR,
            'messageProperty' => 'mixedNumbersMessage',
        ],
        NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY => [
            'code' => NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR,
            'messageProperty' => 'hiddenOverlayMessage',
        ],
    ];
    
    /**
     * @param string[] $defaultLocales
     */
    public function __construct(array $defaultLocales = []) {
    }
    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();
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a &quot;__toString()&quot; method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates (&quot;Y-m-d H:i:s&quot;).
NoSuspiciousCharactersValidator::CHECK_CHAR_LIMIT private constant
NoSuspiciousCharactersValidator::CHECK_ERROR private constant
NoSuspiciousCharactersValidator::CHECK_RESTRICTION_LEVEL private constant
NoSuspiciousCharactersValidator::CHECK_SINGLE_SCRIPT private constant
NoSuspiciousCharactersValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
NoSuspiciousCharactersValidator::__construct public function
RSS feed
Powered by Drupal