Composite.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14. * A constraint that is composed of other constraints.
  15. *
  16. * You should never use the nested constraint instances anywhere else, because
  17. * their groups are adapted when passed to the constructor of this class.
  18. *
  19. * If you want to create your own composite constraint, extend this class and
  20. * let {@link getCompositeOption()} return the name of the property which
  21. * contains the nested constraints.
  22. *
  23. * @since 2.6
  24. *
  25. * @author Bernhard Schussek <bschussek@gmail.com>
  26. */
  27. abstract class Composite extends Constraint
  28. {
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * The groups of the composite and its nested constraints are made
  33. * consistent using the following strategy:
  34. *
  35. * - If groups are passed explicitly to the composite constraint, but
  36. * not to the nested constraints, the options of the composite
  37. * constraint are copied to the nested constraints;
  38. *
  39. * - If groups are passed explicitly to the nested constraints, but not
  40. * to the composite constraint, the groups of all nested constraints
  41. * are merged and used as groups for the composite constraint;
  42. *
  43. * - If groups are passed explicitly to both the composite and its nested
  44. * constraints, the groups of the nested constraints must be a subset
  45. * of the groups of the composite constraint. If not, a
  46. * {@link ConstraintDefinitionException} is thrown.
  47. *
  48. * All this is done in the constructor, because constraints can then be
  49. * cached. When constraints are loaded from the cache, no more group
  50. * checks need to be done.
  51. */
  52. public function __construct($options = null)
  53. {
  54. parent::__construct($options);
  55. $this->initializeNestedConstraints();
  56. /* @var Constraint[] $nestedConstraints */
  57. $compositeOption = $this->getCompositeOption();
  58. $nestedConstraints = $this->$compositeOption;
  59. if (!is_array($nestedConstraints)) {
  60. $nestedConstraints = array($nestedConstraints);
  61. }
  62. foreach ($nestedConstraints as $constraint) {
  63. if (!$constraint instanceof Constraint) {
  64. throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, get_class($this)));
  65. }
  66. if ($constraint instanceof Valid) {
  67. throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', get_class($this)));
  68. }
  69. }
  70. if (!property_exists($this, 'groups')) {
  71. $mergedGroups = array();
  72. foreach ($nestedConstraints as $constraint) {
  73. foreach ($constraint->groups as $group) {
  74. $mergedGroups[$group] = true;
  75. }
  76. }
  77. $this->groups = array_keys($mergedGroups);
  78. $this->$compositeOption = $nestedConstraints;
  79. return;
  80. }
  81. foreach ($nestedConstraints as $constraint) {
  82. if (property_exists($constraint, 'groups')) {
  83. $excessGroups = array_diff($constraint->groups, $this->groups);
  84. if (count($excessGroups) > 0) {
  85. throw new ConstraintDefinitionException(sprintf(
  86. 'The group(s) "%s" passed to the constraint %s '.
  87. 'should also be passed to its containing constraint %s',
  88. implode('", "', $excessGroups),
  89. get_class($constraint),
  90. get_class($this)
  91. ));
  92. }
  93. } else {
  94. $constraint->groups = $this->groups;
  95. }
  96. }
  97. $this->$compositeOption = $nestedConstraints;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. *
  102. * Implicit group names are forwarded to nested constraints.
  103. *
  104. * @param string $group
  105. */
  106. public function addImplicitGroupName($group)
  107. {
  108. parent::addImplicitGroupName($group);
  109. /** @var Constraint[] $nestedConstraints */
  110. $nestedConstraints = $this->{$this->getCompositeOption()};
  111. foreach ($nestedConstraints as $constraint) {
  112. $constraint->addImplicitGroupName($group);
  113. }
  114. }
  115. /**
  116. * Returns the name of the property that contains the nested constraints.
  117. *
  118. * @return string The property name
  119. */
  120. abstract protected function getCompositeOption();
  121. /**
  122. * Initializes the nested constraints.
  123. *
  124. * This method can be overwritten in subclasses to clean up the nested
  125. * constraints passed to the constructor.
  126. *
  127. * @see Collection::initializeNestedConstraints()
  128. */
  129. protected function initializeNestedConstraints()
  130. {
  131. }
  132. }