You are here

class GroupSequence in Plug 7

A sequence of validation groups.

When validating a group sequence, each group will only be validated if all of the previous groups in the sequence succeeded. For example:

$validator->validate($address, null, new GroupSequence('Basic', 'Strict'));

In the first step, all constraints that belong to the group "Basic" will be validated. If none of the constraints fail, the validator will then validate the constraints in group "Strict". This is useful, for example, if "Strict" contains expensive checks that require a lot of CPU or slow, external services. You usually don't want to run expensive checks if any of the cheap checks fail.

When adding metadata to a class, you can override the "Default" group of that class with a group sequence:

  • @GroupSequence({"Address", "Strict"})

*\/ class Address { // ... }

Whenever you validate that object in the "Default" group, the group sequence will be validated:

$validator->validate($address);

If you want to execute the constraints of the "Default" group for a class with an overridden default group, pass the class name as group name instead:

$validator->validate($address, null, "Address")

@Target({"CLASS", "ANNOTATION"})

@author Bernhard Schussek <bschussek@gmail.com>

@api

Implementing \ArrayAccess, \IteratorAggregate and \Countable is

Hierarchy

  • class \Symfony\Component\Validator\Constraints\GroupSequence implements \Symfony\Component\Validator\Constraints\ArrayAccess, \Symfony\Component\Validator\Constraints\IteratorAggregate, \Symfony\Component\Validator\Constraints\Countable

Expanded class hierarchy of GroupSequence

Deprecated

since 2.5 and will be removed in 3.0.

7 files declare their use of GroupSequence
Abstract2Dot5ApiTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php
AbstractValidatorTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php
AnnotationLoader.php in lib/Symfony/validator/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
ClassMetadata.php in lib/Symfony/validator/Symfony/Component/Validator/Mapping/ClassMetadata.php
GroupSequenceTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Constraints/GroupSequenceTest.php

... See full list

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/GroupSequence.php, line 61

Namespace

Symfony\Component\Validator\Constraints
View source
class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable {

  /**
   * The groups in the sequence.
   *
   * @var string[]|GroupSequence[]
   */
  public $groups;

  /**
   * The group in which cascaded objects are validated when validating
   * this sequence.
   *
   * By default, cascaded objects are validated in each of the groups of
   * the sequence.
   *
   * If a class has a group sequence attached, that sequence replaces the
   * "Default" group. When validating that class in the "Default" group, the
   * group sequence is used instead, but still the "Default" group should be
   * cascaded to other objects.
   *
   * @var string|GroupSequence
   */
  public $cascadedGroup;

  /**
   * Creates a new group sequence.
   *
   * @param string[] $groups The groups in the sequence
   */
  public function __construct(array $groups) {

    // Support for Doctrine annotations
    $this->groups = isset($groups['value']) ? $groups['value'] : $groups;
  }

  /**
   * Returns an iterator for this group.
   *
   * @return \Traversable The iterator
   *
   * @see \IteratorAggregate::getIterator()
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function getIterator() {
    return new \ArrayIterator($this->groups);
  }

  /**
   * Returns whether the given offset exists in the sequence.
   *
   * @param int $offset The offset
   *
   * @return bool Whether the offset exists
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function offsetExists($offset) {
    return isset($this->groups[$offset]);
  }

  /**
   * Returns the group at the given offset.
   *
   * @param int $offset The offset
   *
   * @return string The group a the given offset
   *
   * @throws OutOfBoundsException If the object does not exist
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function offsetGet($offset) {
    if (!isset($this->groups[$offset])) {
      throw new OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
    }
    return $this->groups[$offset];
  }

  /**
   * Sets the group at the given offset.
   *
   * @param int    $offset The offset
   * @param string $value  The group name
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function offsetSet($offset, $value) {
    if (null !== $offset) {
      $this->groups[$offset] = $value;
      return;
    }
    $this->groups[] = $value;
  }

  /**
   * Removes the group at the given offset.
   *
   * @param int $offset The offset
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function offsetUnset($offset) {
    unset($this->groups[$offset]);
  }

  /**
   * Returns the number of groups in the sequence.
   *
   * @return int The number of groups
   *
   * @deprecated Implemented for backwards compatibility with Symfony < 2.5.
   *             To be removed in Symfony 3.0.
   */
  public function count() {
    return count($this->groups);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupSequence::$cascadedGroup public property The group in which cascaded objects are validated when validating this sequence.
GroupSequence::$groups public property The groups in the sequence.
GroupSequence::count Deprecated public function Returns the number of groups in the sequence.
GroupSequence::getIterator Deprecated public function Returns an iterator for this group.
GroupSequence::offsetExists Deprecated public function Returns whether the given offset exists in the sequence.
GroupSequence::offsetGet Deprecated public function Returns the group at the given offset.
GroupSequence::offsetSet Deprecated public function Sets the group at the given offset.
GroupSequence::offsetUnset Deprecated public function Removes the group at the given offset.
GroupSequence::__construct public function Creates a new group sequence.