CoreFiles by mySites.guru
ExpressionValidator.php65 lines, 2,102 bytes, from Joomla_5.3.4-Stable-Full_Package.zip
Raw History Compare with 5.3.3
md5
d6758fcb3edec0429c11d3e537e975fe
sha1
29c90d5d4c998565e290dd584ee1b1a4a5cdb6d2
sha256
eb76c5c5693c7b5c9328cbbf0e7665562a14600de6f35091e4d50fb8d6ed0df4
<?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\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
 * @author Fabien Potencier <[email protected]>
 * @author Bernhard Schussek <[email protected]>
 */
class ExpressionValidator extends ConstraintValidator
{
    private ExpressionLanguage $expressionLanguage;

    public function __construct(?ExpressionLanguage $expressionLanguage = null)
    {
        if ($expressionLanguage) {
            $this->expressionLanguage = $expressionLanguage;
        }
    }

    /**
     * @return void
     */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Expression) {
            throw new UnexpectedTypeException($constraint, Expression::class);
        }

        $variables = $constraint->values;
        $variables['value'] = $value;
        $variables['this'] = $this->context->getObject();
        $variables['context'] = $this->context;

        if ($constraint->negate xor $this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
                ->setCode(Expression::EXPRESSION_FAILED_ERROR)
                ->addViolation();
        }
    }

    private function getExpressionLanguage(): ExpressionLanguage
    {
        if (!isset($this->expressionLanguage)) {
            $this->expressionLanguage = new ExpressionLanguage();
            $this->expressionLanguage->registerProvider(new ExpressionLanguageProvider());
        }

        return $this->expressionLanguage;
    }
}

The same content is in 136 releases, Joomla 5.1.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.