mirror of
https://github.com/symfony/validator.git
synced 2026-03-24 01:12:13 +01:00
* 7.4: [Validator][Serializer] Make internal properties private [VarExporter] Fix serializing classes with __serialize() returning unprefixed private properties Remove direct access to internal properties cross-components remove deprecated nullable option from primary key columns [Validator] Optimize serialized metadata and cleanup tests specific fix to avoid 'outag' when inflecting 'outages' Use <info> for options in command description [ObjectMapper] Remove obsolete TODO [HttpFoundation][HttpKernel][WebProfilerBundle] Add support for the `QUERY` HTTP method [DependencyInjection] Don’t autowire excluded services Add bool return type to CustomCredentials callable parameter [Process] Enhance hasSystemCallBeenInterrupted function for non-english locale [FrameworkBundle] Make `cache:warmup` warm up read-only caches
199 lines
5.7 KiB
PHP
199 lines
5.7 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\Mapping;
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\Constraints\Cascade;
|
|
use Symfony\Component\Validator\Constraints\DisableAutoMapping;
|
|
use Symfony\Component\Validator\Constraints\EnableAutoMapping;
|
|
use Symfony\Component\Validator\Constraints\Traverse;
|
|
use Symfony\Component\Validator\Constraints\Valid;
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
|
|
|
/**
|
|
* A generic container of {@link Constraint} objects.
|
|
*
|
|
* This class supports serialization and cloning.
|
|
*
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
*/
|
|
class GenericMetadata implements MetadataInterface
|
|
{
|
|
/**
|
|
* @var Constraint[]
|
|
*/
|
|
private array $constraints = [];
|
|
|
|
/**
|
|
* @var array<string, Constraint[]>
|
|
*/
|
|
private array $constraintsByGroup = [];
|
|
|
|
/**
|
|
* The strategy for cascading objects.
|
|
*
|
|
* By default, objects are not cascaded.
|
|
*
|
|
* @var CascadingStrategy::*
|
|
*/
|
|
private int $cascadingStrategy = CascadingStrategy::NONE;
|
|
|
|
/**
|
|
* The strategy for traversing traversable objects.
|
|
*
|
|
* By default, traversable objects are not traversed.
|
|
*
|
|
* @var TraversalStrategy::*
|
|
*/
|
|
private int $traversalStrategy = TraversalStrategy::NONE;
|
|
|
|
/**
|
|
* Is auto-mapping enabled?
|
|
*
|
|
* @var AutoMappingStrategy::*
|
|
*/
|
|
private int $autoMappingStrategy = AutoMappingStrategy::NONE;
|
|
|
|
public function __serialize(): array
|
|
{
|
|
return array_filter([
|
|
'constraints' => $this->constraints,
|
|
'constraintsByGroup' => $this->constraintsByGroup,
|
|
'cascadingStrategy' => CascadingStrategy::NONE !== $this->cascadingStrategy ? $this->cascadingStrategy : null,
|
|
'traversalStrategy' => TraversalStrategy::NONE !== $this->traversalStrategy ? $this->traversalStrategy : null,
|
|
'autoMappingStrategy' => AutoMappingStrategy::NONE !== $this->autoMappingStrategy ? $this->autoMappingStrategy : null,
|
|
]);
|
|
}
|
|
|
|
public function __clone()
|
|
{
|
|
$constraints = $this->constraints;
|
|
|
|
$this->constraints = [];
|
|
$this->constraintsByGroup = [];
|
|
|
|
foreach ($constraints as $constraint) {
|
|
$this->addConstraint(clone $constraint);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds a constraint.
|
|
*
|
|
* If the constraint {@link Valid} is added, the cascading strategy will be
|
|
* changed to {@link CascadingStrategy::CASCADE}. Depending on the
|
|
* $traverse property of that constraint, the traversal strategy
|
|
* will be set to one of the following:
|
|
*
|
|
* - {@link TraversalStrategy::IMPLICIT} if $traverse is enabled
|
|
* - {@link TraversalStrategy::NONE} if $traverse is disabled
|
|
*
|
|
* @return $this
|
|
*
|
|
* @throws ConstraintDefinitionException When trying to add the {@link Cascade}
|
|
* or {@link Traverse} constraint
|
|
*/
|
|
public function addConstraint(Constraint $constraint): static
|
|
{
|
|
if ($constraint instanceof Traverse || $constraint instanceof Cascade) {
|
|
throw new ConstraintDefinitionException(\sprintf('The constraint "%s" can only be put on classes. Please use "Symfony\Component\Validator\Constraints\Valid" instead.', get_debug_type($constraint)));
|
|
}
|
|
|
|
if ($constraint instanceof Valid && null === $constraint->groups) {
|
|
$this->cascadingStrategy = CascadingStrategy::CASCADE;
|
|
$this->traversalStrategy = $constraint->traverse ? TraversalStrategy::IMPLICIT : TraversalStrategy::NONE;
|
|
|
|
// The constraint is not added
|
|
return $this;
|
|
}
|
|
|
|
if ($constraint instanceof DisableAutoMapping || $constraint instanceof EnableAutoMapping) {
|
|
$this->autoMappingStrategy = $constraint instanceof EnableAutoMapping ? AutoMappingStrategy::ENABLED : AutoMappingStrategy::DISABLED;
|
|
|
|
// The constraint is not added
|
|
return $this;
|
|
}
|
|
|
|
if (!\in_array($constraint, $this->constraints, true)) {
|
|
$this->constraints[] = $constraint;
|
|
}
|
|
|
|
foreach ($constraint->groups as $group) {
|
|
if (!\in_array($constraint, $this->constraintsByGroup[$group] ??= [], true)) {
|
|
$this->constraintsByGroup[$group][] = $constraint;
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Adds a list of constraints.
|
|
*
|
|
* @param Constraint[] $constraints
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function addConstraints(array $constraints): static
|
|
{
|
|
foreach ($constraints as $constraint) {
|
|
$this->addConstraint($constraint);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Constraint[]
|
|
*/
|
|
public function getConstraints(): array
|
|
{
|
|
return $this->constraints;
|
|
}
|
|
|
|
/**
|
|
* Returns whether this element has any constraints.
|
|
*/
|
|
public function hasConstraints(): bool
|
|
{
|
|
return \count($this->constraints) > 0;
|
|
}
|
|
|
|
/**
|
|
* Aware of the global group (* group).
|
|
*
|
|
* @return Constraint[]
|
|
*/
|
|
public function findConstraints(string $group): array
|
|
{
|
|
return $this->constraintsByGroup[$group] ?? [];
|
|
}
|
|
|
|
public function getCascadingStrategy(): int
|
|
{
|
|
return $this->cascadingStrategy;
|
|
}
|
|
|
|
public function getTraversalStrategy(): int
|
|
{
|
|
return $this->traversalStrategy;
|
|
}
|
|
|
|
/**
|
|
* @see AutoMappingStrategy
|
|
*/
|
|
public function getAutoMappingStrategy(): int
|
|
{
|
|
return $this->autoMappingStrategy;
|
|
}
|
|
}
|