Composite.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = array($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, \get_class($this)));
  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.', \get_class($this)));
  69. }
  70. }
  71. if (!property_exists($this, 'groups')) {
  72. $mergedGroups = array();
  73. foreach ($nestedConstraints as $constraint) {
  74. foreach ($constraint->groups as $group) {
  75. $mergedGroups[$group] = true;
  76. }
  77. }
  78. $this->groups = array_keys($mergedGroups);
  79. $this->$compositeOption = $nestedConstraints;
  80. return;
  81. }
  82. foreach ($nestedConstraints as $constraint) {
  83. if (property_exists($constraint, 'groups')) {
  84. $excessGroups = array_diff($constraint->groups, $this->groups);
  85. if (\count($excessGroups) > 0) {
  86. 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_class($constraint), \get_class($this)));
  87. }
  88. } else {
  89. $constraint->groups = $this->groups;
  90. }
  91. }
  92. $this->$compositeOption = $nestedConstraints;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. *
  97. * Implicit group names are forwarded to nested constraints.
  98. *
  99. * @param string $group
  100. */
  101. public function addImplicitGroupName($group)
  102. {
  103. parent::addImplicitGroupName($group);
  104. /** @var Constraint[] $nestedConstraints */
  105. $nestedConstraints = $this->{$this->getCompositeOption()};
  106. foreach ($nestedConstraints as $constraint) {
  107. $constraint->addImplicitGroupName($group);
  108. }
  109. }
  110. /**
  111. * Returns the name of the property that contains the nested constraints.
  112. *
  113. * @return string The property name
  114. */
  115. abstract protected function getCompositeOption();
  116. /**
  117. * Initializes the nested constraints.
  118. *
  119. * This method can be overwritten in subclasses to clean up the nested
  120. * constraints passed to the constructor.
  121. *
  122. * @see Collection::initializeNestedConstraints()
  123. */
  124. protected function initializeNestedConstraints()
  125. {
  126. }
  127. }