CoreFiles by mySites.guru
RegexValidator.php58 lines, 1,754 bytes, from Joomla_5.2.0-Stable-Full_Package.zip
Raw History Compare with 5.1.4
md5
9c1f6ac9610d8a316fe430079b14a43a
sha1
0a499df0ccb7dd01b97191dd4c6798a573cb4276
sha256
9ac4fd7aace470be5e78665bf34983e1223b69378f8ae35b3b12c22b26914f87
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
 * Validates whether a value match or not given regexp pattern.
 *
 * @author Bernhard Schussek <[email protected]>
 * @author Joseph Bielawski <[email protected]>
 */
class RegexValidator extends ConstraintValidator
{
    /**
     * @return void
     */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Regex) {
            throw new UnexpectedTypeException($constraint, Regex::class);
        }

        if (null === $value || '' === $value) {
            return;
        }

        if (!\is_scalar($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }

        $value = (string) $value;

        if (null !== $constraint->normalizer) {
            $value = ($constraint->normalizer)($value);
        }

        if ($constraint->match xor preg_match($constraint->pattern, $value)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setParameter('{{ pattern }}', $constraint->pattern)
                ->setCode(Regex::REGEX_FAILED_ERROR)
                ->addViolation();
        }
    }
}

The same content is in 107 releases, Joomla 5.1.0-beta1 to 6.1.0-beta2. See them all, or every version of this file.