You are here

public function ClassMetadata::addConstraint in Plug 7

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 lib/Symfony/validator/Symfony/Component/Validator/Mapping/ClassMetadata.php
Merges the constraints of the given metadata into this object.

File

lib/Symfony/validator/Symfony/Component/Validator/Mapping/ClassMetadata.php, line 220

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;
}