You are here

public function GenericMetadata::addConstraint in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Mapping/GenericMetadata.php \Symfony\Component\Validator\Mapping\GenericMetadata::addConstraint()

Adds a constraint.

If the constraint {@link Valid} is added, the cascading strategy will be changed to {@link CascadingStrategy::CASCADE}. Depending on the properties $traverse and $deep of that constraint, the traversal strategy will be set to one of the following:

  • {@link TraversalStrategy::IMPLICIT} if $traverse is enabled and $deep is enabled
  • {@link TraversalStrategy::IMPLICIT} | {@link TraversalStrategy::STOP_RECURSION} if $traverse is enabled, but $deep is disabled
  • {@link TraversalStrategy::NONE} if $traverse is disabled

Parameters

Constraint $constraint The constraint to add:

Return value

GenericMetadata This object

Throws

ConstraintDefinitionException When trying to add the {@link Traverse} constraint

4 calls to GenericMetadata::addConstraint()
ClassMetadata::addConstraint in vendor/symfony/validator/Mapping/ClassMetadata.php
Adds a constraint.
GenericMetadata::addConstraints in vendor/symfony/validator/Mapping/GenericMetadata.php
Adds an list of constraints.
GenericMetadata::__clone in vendor/symfony/validator/Mapping/GenericMetadata.php
Clones this object.
MemberMetadata::addConstraint in vendor/symfony/validator/Mapping/MemberMetadata.php
Adds a constraint.
2 methods override GenericMetadata::addConstraint()
ClassMetadata::addConstraint in vendor/symfony/validator/Mapping/ClassMetadata.php
Adds a constraint.
MemberMetadata::addConstraint in vendor/symfony/validator/Mapping/MemberMetadata.php
Adds a constraint.

File

vendor/symfony/validator/Mapping/GenericMetadata.php, line 131

Class

GenericMetadata
A generic container of {@link Constraint} objects.

Namespace

Symfony\Component\Validator\Mapping

Code

public function addConstraint(Constraint $constraint) {
  if ($constraint instanceof Traverse) {
    throw new ConstraintDefinitionException(sprintf('The constraint "%s" can only be put on classes. Please use ' . '"Symfony\\Component\\Validator\\Constraints\\Valid" instead.', get_class($constraint)));
  }
  if ($constraint instanceof Valid) {
    $this->cascadingStrategy = CascadingStrategy::CASCADE;
    if ($constraint->traverse) {

      // Traverse unless the value is not traversable
      $this->traversalStrategy = TraversalStrategy::IMPLICIT;
      if (!$constraint->deep) {
        $this->traversalStrategy |= TraversalStrategy::STOP_RECURSION;
      }
    }
    else {
      $this->traversalStrategy = TraversalStrategy::NONE;
    }
    return $this;
  }
  $this->constraints[] = $constraint;
  foreach ($constraint->groups as $group) {
    $this->constraintsByGroup[$group][] = $constraint;
  }
  return $this;
}