Drupal investigation

Composite.php 5.1KB

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