annotate vendor/symfony/validator/Constraints/Composite.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Validator\Constraints;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@0 15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * A constraint that is composed of other constraints.
Chris@0 19 *
Chris@0 20 * You should never use the nested constraint instances anywhere else, because
Chris@0 21 * their groups are adapted when passed to the constructor of this class.
Chris@0 22 *
Chris@0 23 * If you want to create your own composite constraint, extend this class and
Chris@0 24 * let {@link getCompositeOption()} return the name of the property which
Chris@0 25 * contains the nested constraints.
Chris@0 26 *
Chris@0 27 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 28 */
Chris@0 29 abstract class Composite extends Constraint
Chris@0 30 {
Chris@0 31 /**
Chris@0 32 * {@inheritdoc}
Chris@0 33 *
Chris@0 34 * The groups of the composite and its nested constraints are made
Chris@0 35 * consistent using the following strategy:
Chris@0 36 *
Chris@0 37 * - If groups are passed explicitly to the composite constraint, but
Chris@0 38 * not to the nested constraints, the options of the composite
Chris@0 39 * constraint are copied to the nested constraints;
Chris@0 40 *
Chris@0 41 * - If groups are passed explicitly to the nested constraints, but not
Chris@0 42 * to the composite constraint, the groups of all nested constraints
Chris@0 43 * are merged and used as groups for the composite constraint;
Chris@0 44 *
Chris@0 45 * - If groups are passed explicitly to both the composite and its nested
Chris@0 46 * constraints, the groups of the nested constraints must be a subset
Chris@0 47 * of the groups of the composite constraint. If not, a
Chris@0 48 * {@link ConstraintDefinitionException} is thrown.
Chris@0 49 *
Chris@0 50 * All this is done in the constructor, because constraints can then be
Chris@0 51 * cached. When constraints are loaded from the cache, no more group
Chris@0 52 * checks need to be done.
Chris@0 53 */
Chris@0 54 public function __construct($options = null)
Chris@0 55 {
Chris@0 56 parent::__construct($options);
Chris@0 57
Chris@0 58 $this->initializeNestedConstraints();
Chris@0 59
Chris@0 60 /* @var Constraint[] $nestedConstraints */
Chris@0 61 $compositeOption = $this->getCompositeOption();
Chris@0 62 $nestedConstraints = $this->$compositeOption;
Chris@0 63
Chris@0 64 if (!is_array($nestedConstraints)) {
Chris@0 65 $nestedConstraints = array($nestedConstraints);
Chris@0 66 }
Chris@0 67
Chris@0 68 foreach ($nestedConstraints as $constraint) {
Chris@0 69 if (!$constraint instanceof Constraint) {
Chris@0 70 if (is_object($constraint)) {
Chris@0 71 $constraint = get_class($constraint);
Chris@0 72 }
Chris@0 73
Chris@0 74 throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, get_class($this)));
Chris@0 75 }
Chris@0 76
Chris@0 77 if ($constraint instanceof Valid) {
Chris@0 78 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)));
Chris@0 79 }
Chris@0 80 }
Chris@0 81
Chris@0 82 if (!property_exists($this, 'groups')) {
Chris@0 83 $mergedGroups = array();
Chris@0 84
Chris@0 85 foreach ($nestedConstraints as $constraint) {
Chris@0 86 foreach ($constraint->groups as $group) {
Chris@0 87 $mergedGroups[$group] = true;
Chris@0 88 }
Chris@0 89 }
Chris@0 90
Chris@0 91 $this->groups = array_keys($mergedGroups);
Chris@0 92 $this->$compositeOption = $nestedConstraints;
Chris@0 93
Chris@0 94 return;
Chris@0 95 }
Chris@0 96
Chris@0 97 foreach ($nestedConstraints as $constraint) {
Chris@0 98 if (property_exists($constraint, 'groups')) {
Chris@0 99 $excessGroups = array_diff($constraint->groups, $this->groups);
Chris@0 100
Chris@0 101 if (count($excessGroups) > 0) {
Chris@0 102 throw new ConstraintDefinitionException(sprintf(
Chris@0 103 'The group(s) "%s" passed to the constraint %s '.
Chris@0 104 'should also be passed to its containing constraint %s',
Chris@0 105 implode('", "', $excessGroups),
Chris@0 106 get_class($constraint),
Chris@0 107 get_class($this)
Chris@0 108 ));
Chris@0 109 }
Chris@0 110 } else {
Chris@0 111 $constraint->groups = $this->groups;
Chris@0 112 }
Chris@0 113 }
Chris@0 114
Chris@0 115 $this->$compositeOption = $nestedConstraints;
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 *
Chris@0 121 * Implicit group names are forwarded to nested constraints.
Chris@0 122 *
Chris@0 123 * @param string $group
Chris@0 124 */
Chris@0 125 public function addImplicitGroupName($group)
Chris@0 126 {
Chris@0 127 parent::addImplicitGroupName($group);
Chris@0 128
Chris@0 129 /** @var Constraint[] $nestedConstraints */
Chris@0 130 $nestedConstraints = $this->{$this->getCompositeOption()};
Chris@0 131
Chris@0 132 foreach ($nestedConstraints as $constraint) {
Chris@0 133 $constraint->addImplicitGroupName($group);
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Returns the name of the property that contains the nested constraints.
Chris@0 139 *
Chris@0 140 * @return string The property name
Chris@0 141 */
Chris@0 142 abstract protected function getCompositeOption();
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Initializes the nested constraints.
Chris@0 146 *
Chris@0 147 * This method can be overwritten in subclasses to clean up the nested
Chris@0 148 * constraints passed to the constructor.
Chris@0 149 *
Chris@0 150 * @see Collection::initializeNestedConstraints()
Chris@0 151 */
Chris@0 152 protected function initializeNestedConstraints()
Chris@0 153 {
Chris@0 154 }
Chris@0 155 }