Files
Nicolas Grekas 04f7111e6f Merge branch '7.4' into 8.0
* 7.4:
  [HttpFoundation] Fix session cookie_lifetime not applied in mock session storage
  [Validator] Fix test
  [Serializer] Fix denormalization of magic `__set` properties
  [Config] Fix NodeDefinition template to be covariant
  Add 'sms' to hostless schemes
  [Validator] Fix required options check when extending a constraint with a simplified constructor
  [Validator] Skip ExpressionLanguage requirement in When constraint for closure expressions
  [Cache] Add timeout and slot eviction to LockRegistry stampede prevention
  [Console] Fix OUTPUT_RAW corrupting binary content on Windows
  [Form] Fix session data contamination by non-serializable objects in form flow
  [Mime] Use shell_exec() instead of passthru() in FileBinaryMimeTypeGuesser
  [HttpClient] Fix streaming from CachingHttpClient
  [DoctrineBridge] Rename `_schema_subscriber_check` table to `schema_subscriber_check_` for Oracle compatibility
  [HttpClient] Fix CachingHttpClient compatibility with decorator clients on 304 responses
  [FrameworkBundle] Fix stale container after reboot in KernelTestCase
  [Form] Fix duplicate validation errors when ValidatorExtension is instantiated multiple times
2026-03-06 14:17:40 +01:00

74 lines
3.2 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\MissingOptionsException;
/**
* Conditionally apply validation constraints based on an expression using the ExpressionLanguage syntax.
*
* @see https://symfony.com/doc/current/components/expression_language.html
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class When extends Composite
{
public string|Expression|\Closure $expression;
public array|Constraint $constraints = [];
public array $values = [];
public array|Constraint $otherwise = [];
/**
* @param string|Expression|\Closure(object): bool $expression The condition to evaluate, either as a closure or using the ExpressionLanguage syntax
* @param Constraint[]|Constraint|null $constraints One or multiple constraints that are applied if the expression returns true
* @param array<string,mixed>|null $values The values of the custom variables used in the expression (defaults to [])
* @param string[]|null $groups
* @param Constraint[]|Constraint $otherwise One or multiple constraints that are applied if the expression returns false
*/
public function __construct(string|Expression|\Closure $expression, array|Constraint|null $constraints = null, ?array $values = null, ?array $groups = null, $payload = null, ?array $options = null, array|Constraint $otherwise = [])
{
if (!$expression instanceof \Closure && !class_exists(ExpressionLanguage::class)) {
throw new LogicException(\sprintf('The "symfony/expression-language" component is required to use the "%s" constraint. Try running "composer require symfony/expression-language".', __CLASS__));
}
if (null !== $options) {
throw new InvalidArgumentException(\sprintf('Passing an array of options to configure the "%s" constraint is no longer supported.', static::class));
}
if (null === $constraints) {
throw new MissingOptionsException(\sprintf('The options "constraints" must be set for constraint "%s".', self::class), ['constraints']);
}
$this->expression = $expression;
$this->constraints = $constraints;
$this->otherwise = $otherwise;
parent::__construct(null, $groups, $payload);
$this->values = $values ?? $this->values;
}
public function getTargets(): string|array
{
return [self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT];
}
protected function getCompositeOption(): array|string
{
return ['constraints', 'otherwise'];
}
}