You are here

public function ClassMetadata::addConstraint in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Mapping/ClassMetadata.php \Symfony\Component\Validator\Mapping\ClassMetadata::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

Overrides GenericMetadata::addConstraint

1 call to ClassMetadata::addConstraint()
ClassMetadata::mergeConstraints in vendor/symfony/validator/Mapping/ClassMetadata.php
Merges the constraints of the given metadata into this object.

File

vendor/symfony/validator/Mapping/ClassMetadata.php, line 222

Class

ClassMetadata
Default implementation of {@link ClassMetadataInterface}.

Namespace

Symfony\Component\Validator\Mapping

Code

public function addConstraint(Constraint $constraint) {
  if (!in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint
    ->getTargets())) {
    throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', get_class($constraint)));
  }
  if ($constraint instanceof Valid) {
    throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', get_class($constraint)));
  }
  if ($constraint instanceof Traverse) {
    if ($constraint->traverse) {

      // If traverse is true, traversal should be explicitly enabled
      $this->traversalStrategy = TraversalStrategy::TRAVERSE;
    }
    else {

      // If traverse is false, traversal should be explicitly disabled
      $this->traversalStrategy = TraversalStrategy::NONE;
    }

    // The constraint is not added
    return $this;
  }
  $constraint
    ->addImplicitGroupName($this
    ->getDefaultGroup());
  parent::addConstraint($constraint);
  return $this;
}