Composite.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @author Bernhard Schussek <bschussek@gmail.com>
  24. */
  25. abstract class Composite extends Constraint
  26. {
  27. /**
  28. * {@inheritdoc}
  29. *
  30. * The groups of the composite and its nested constraints are made
  31. * consistent using the following strategy:
  32. *
  33. * - If groups are passed explicitly to the composite constraint, but
  34. * not to the nested constraints, the options of the composite
  35. * constraint are copied to the nested constraints;
  36. *
  37. * - If groups are passed explicitly to the nested constraints, but not
  38. * to the composite constraint, the groups of all nested constraints
  39. * are merged and used as groups for the composite constraint;
  40. *
  41. * - If groups are passed explicitly to both the composite and its nested
  42. * constraints, the groups of the nested constraints must be a subset
  43. * of the groups of the composite constraint. If not, a
  44. * {@link ConstraintDefinitionException} is thrown.
  45. *
  46. * All this is done in the constructor, because constraints can then be
  47. * cached. When constraints are loaded from the cache, no more group
  48. * checks need to be done.
  49. */
  50. public function __construct($options = null)
  51. {
  52. parent::__construct($options);
  53. $this->initializeNestedConstraints();
  54. /* @var Constraint[] $nestedConstraints */
  55. $compositeOption = $this->getCompositeOption();
  56. $nestedConstraints = $this->$compositeOption;
  57. if (!\is_array($nestedConstraints)) {
  58. $nestedConstraints = [$nestedConstraints];
  59. }
  60. foreach ($nestedConstraints as $constraint) {
  61. if (!$constraint instanceof Constraint) {
  62. if (\is_object($constraint)) {
  63. $constraint = \get_class($constraint);
  64. }
  65. throw new ConstraintDefinitionException(sprintf('The value "%s" is not an instance of Constraint in constraint "%s".', $constraint, static::class));
  66. }
  67. if ($constraint instanceof Valid) {
  68. 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.', static::class));
  69. }
  70. }
  71. if (!property_exists($this, 'groups')) {
  72. $mergedGroups = [];
  73. foreach ($nestedConstraints as $constraint) {
  74. foreach ($constraint->groups as $group) {
  75. $mergedGroups[$group] = true;
  76. }
  77. }
  78. // prevent empty composite constraint to have empty groups
  79. $this->groups = array_keys($mergedGroups) ?: [self::DEFAULT_GROUP];
  80. $this->$compositeOption = $nestedConstraints;
  81. return;
  82. }
  83. foreach ($nestedConstraints as $constraint) {
  84. if (property_exists($constraint, 'groups')) {
  85. $excessGroups = array_diff($constraint->groups, $this->groups);
  86. if (\count($excessGroups) > 0) {
  87. throw new ConstraintDefinitionException(sprintf('The group(s) "%s" passed to the constraint "%s" should also be passed to its containing constraint "%s".', implode('", "', $excessGroups), get_debug_type($constraint), static::class));
  88. }
  89. } else {
  90. $constraint->groups = $this->groups;
  91. }
  92. }
  93. $this->$compositeOption = $nestedConstraints;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. *
  98. * Implicit group names are forwarded to nested constraints.
  99. *
  100. * @param string $group
  101. */
  102. public function addImplicitGroupName($group)
  103. {
  104. parent::addImplicitGroupName($group);
  105. /** @var Constraint[] $nestedConstraints */
  106. $nestedConstraints = $this->{$this->getCompositeOption()};
  107. foreach ($nestedConstraints as $constraint) {
  108. $constraint->addImplicitGroupName($group);
  109. }
  110. }
  111. /**
  112. * Returns the name of the property that contains the nested constraints.
  113. *
  114. * @return string The property name
  115. */
  116. abstract protected function getCompositeOption();
  117. /**
  118. * @internal Used by metadata
  119. *
  120. * @return Constraint[]
  121. */
  122. public function getNestedContraints()
  123. {
  124. /* @var Constraint[] $nestedConstraints */
  125. return $this->{$this->getCompositeOption()};
  126. }
  127. /**
  128. * Initializes the nested constraints.
  129. *
  130. * This method can be overwritten in subclasses to clean up the nested
  131. * constraints passed to the constructor.
  132. *
  133. * @see Collection::initializeNestedConstraints()
  134. */
  135. protected function initializeNestedConstraints()
  136. {
  137. }
  138. }