GroupSequence.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Exception\OutOfBoundsException;
  12. /**
  13. * A sequence of validation groups.
  14. *
  15. * When validating a group sequence, each group will only be validated if all
  16. * of the previous groups in the sequence succeeded. For example:
  17. *
  18. * $validator->validate($address, null, new GroupSequence(array('Basic', 'Strict')));
  19. *
  20. * In the first step, all constraints that belong to the group "Basic" will be
  21. * validated. If none of the constraints fail, the validator will then validate
  22. * the constraints in group "Strict". This is useful, for example, if "Strict"
  23. * contains expensive checks that require a lot of CPU or slow, external
  24. * services. You usually don't want to run expensive checks if any of the cheap
  25. * checks fail.
  26. *
  27. * When adding metadata to a class, you can override the "Default" group of
  28. * that class with a group sequence:
  29. *
  30. * /**
  31. * * @GroupSequence({"Address", "Strict"})
  32. * *\/
  33. * class Address
  34. * {
  35. * // ...
  36. * }
  37. *
  38. * Whenever you validate that object in the "Default" group, the group sequence
  39. * will be validated:
  40. *
  41. * $validator->validate($address);
  42. *
  43. * If you want to execute the constraints of the "Default" group for a class
  44. * with an overridden default group, pass the class name as group name instead:
  45. *
  46. * $validator->validate($address, null, "Address")
  47. *
  48. * @Annotation
  49. * @Target({"CLASS", "ANNOTATION"})
  50. *
  51. * @author Bernhard Schussek <bschussek@gmail.com>
  52. *
  53. * Implementing \ArrayAccess, \IteratorAggregate and \Countable is @deprecated since 2.5 and will be removed in 3.0.
  54. */
  55. class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
  56. {
  57. /**
  58. * The groups in the sequence.
  59. *
  60. * @var string[]|GroupSequence[]
  61. */
  62. public $groups;
  63. /**
  64. * The group in which cascaded objects are validated when validating
  65. * this sequence.
  66. *
  67. * By default, cascaded objects are validated in each of the groups of
  68. * the sequence.
  69. *
  70. * If a class has a group sequence attached, that sequence replaces the
  71. * "Default" group. When validating that class in the "Default" group, the
  72. * group sequence is used instead, but still the "Default" group should be
  73. * cascaded to other objects.
  74. *
  75. * @var string|GroupSequence
  76. */
  77. public $cascadedGroup;
  78. /**
  79. * Creates a new group sequence.
  80. *
  81. * @param string[] $groups The groups in the sequence
  82. */
  83. public function __construct(array $groups)
  84. {
  85. // Support for Doctrine annotations
  86. $this->groups = isset($groups['value']) ? $groups['value'] : $groups;
  87. }
  88. /**
  89. * Returns an iterator for this group.
  90. *
  91. * Implemented for backwards compatibility with Symfony < 2.5.
  92. *
  93. * @return \Traversable The iterator
  94. *
  95. * @see \IteratorAggregate::getIterator()
  96. * @deprecated since version 2.5, to be removed in 3.0.
  97. */
  98. public function getIterator()
  99. {
  100. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  101. return new \ArrayIterator($this->groups);
  102. }
  103. /**
  104. * Returns whether the given offset exists in the sequence.
  105. *
  106. * Implemented for backwards compatibility with Symfony < 2.5.
  107. *
  108. * @param int $offset The offset
  109. *
  110. * @return bool Whether the offset exists
  111. *
  112. * @deprecated since version 2.5, to be removed in 3.0.
  113. */
  114. public function offsetExists($offset)
  115. {
  116. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  117. return isset($this->groups[$offset]);
  118. }
  119. /**
  120. * Returns the group at the given offset.
  121. *
  122. * Implemented for backwards compatibility with Symfony < 2.5.
  123. *
  124. * @param int $offset The offset
  125. *
  126. * @return string The group a the given offset
  127. *
  128. * @throws OutOfBoundsException If the object does not exist
  129. *
  130. * @deprecated since version 2.5, to be removed in 3.0.
  131. */
  132. public function offsetGet($offset)
  133. {
  134. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  135. if (!isset($this->groups[$offset])) {
  136. throw new OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
  137. }
  138. return $this->groups[$offset];
  139. }
  140. /**
  141. * Sets the group at the given offset.
  142. *
  143. * Implemented for backwards compatibility with Symfony < 2.5.
  144. *
  145. * @param int $offset The offset
  146. * @param string $value The group name
  147. *
  148. * @deprecated since version 2.5, to be removed in 3.0.
  149. */
  150. public function offsetSet($offset, $value)
  151. {
  152. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  153. if (null !== $offset) {
  154. $this->groups[$offset] = $value;
  155. return;
  156. }
  157. $this->groups[] = $value;
  158. }
  159. /**
  160. * Removes the group at the given offset.
  161. *
  162. * Implemented for backwards compatibility with Symfony < 2.5.
  163. *
  164. * @param int $offset The offset
  165. *
  166. * @deprecated since version 2.5, to be removed in 3.0.
  167. */
  168. public function offsetUnset($offset)
  169. {
  170. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  171. unset($this->groups[$offset]);
  172. }
  173. /**
  174. * Returns the number of groups in the sequence.
  175. *
  176. * Implemented for backwards compatibility with Symfony < 2.5.
  177. *
  178. * @return int The number of groups
  179. *
  180. * @deprecated since version 2.5, to be removed in 3.0.
  181. */
  182. public function count()
  183. {
  184. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  185. return \count($this->groups);
  186. }
  187. }