2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Mapping;
|
|
|
|
|
2014-03-17 15:44:19 +00:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2014-02-12 11:38:50 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\GroupSequence;
|
2014-02-20 18:10:26 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Traverse;
|
2014-02-18 17:03:59 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Valid;
|
2010-11-16 22:41:46 +00:00
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
2010-11-18 22:10:21 +00:00
|
|
|
use Symfony\Component\Validator\Exception\GroupDefinitionException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* Default implementation of {@link ClassMetadataInterface}.
|
|
|
|
*
|
|
|
|
* This class supports serialization and cloning.
|
2011-01-16 04:41:23 +00:00
|
|
|
*
|
2012-05-26 08:48:33 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2015-09-30 14:39:58 +01:00
|
|
|
class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getClassName()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $name;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getDefaultGroup()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-11-18 22:10:21 +00:00
|
|
|
public $defaultGroup;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-17 22:42:44 +00:00
|
|
|
* @var MemberMetadata[][]
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getPropertyMetadata()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $members = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PropertyMetadata[]
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getPropertyMetadata()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $properties = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GetterMetadata[]
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getPropertyMetadata()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $getters = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getGroupSequence()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $groupSequence = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var bool
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link isGroupSequenceProvider()} instead.
|
2012-11-01 15:08:59 +00:00
|
|
|
*/
|
2012-01-28 21:10:33 +00:00
|
|
|
public $groupSequenceProvider = false;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
2014-02-21 13:44:01 +00:00
|
|
|
/**
|
|
|
|
* The strategy for traversing traversable objects.
|
|
|
|
*
|
|
|
|
* By default, only instances of {@link \Traversable} are traversed.
|
|
|
|
*
|
2014-04-16 11:36:34 +01:00
|
|
|
* @var int
|
2014-02-21 17:00:47 +00:00
|
|
|
*
|
|
|
|
* @internal This property is public in order to reduce the size of the
|
|
|
|
* class' serialized representation. Do not access it. Use
|
|
|
|
* {@link getTraversalStrategy()} instead.
|
2014-02-21 13:44:01 +00:00
|
|
|
*/
|
|
|
|
public $traversalStrategy = TraversalStrategy::IMPLICIT;
|
|
|
|
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var \ReflectionClass
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
private $reflClass;
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Constructs a metadata for the given class.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
|
|
|
public function __construct($class)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->name = $class;
|
2010-11-18 22:10:21 +00:00
|
|
|
// class name without namespace
|
2011-05-13 15:38:01 +01:00
|
|
|
if (false !== $nsSep = strrpos($class, '\\')) {
|
|
|
|
$this->defaultGroup = substr($class, $nsSep + 1);
|
|
|
|
} else {
|
|
|
|
$this->defaultGroup = $class;
|
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2012-12-11 10:49:22 +00:00
|
|
|
|
2010-06-29 16:20:29 +01:00
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2010-06-29 16:20:29 +01:00
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
2014-02-18 17:03:59 +00:00
|
|
|
$parentProperties = parent::__sleep();
|
|
|
|
|
|
|
|
// Don't store the cascading strategy. Classes never cascade.
|
|
|
|
unset($parentProperties[array_search('cascadingStrategy', $parentProperties)]);
|
|
|
|
|
|
|
|
return array_merge($parentProperties, array(
|
2010-06-29 16:20:29 +01:00
|
|
|
'getters',
|
|
|
|
'groupSequence',
|
2012-01-28 21:10:33 +00:00
|
|
|
'groupSequenceProvider',
|
2010-06-29 16:20:29 +01:00
|
|
|
'members',
|
|
|
|
'name',
|
2010-10-15 22:49:09 +01:00
|
|
|
'properties',
|
2014-09-13 00:52:47 +01:00
|
|
|
'defaultGroup',
|
2010-06-29 16:20:29 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Returns the name of the default group for this class.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2010-11-18 22:10:21 +00:00
|
|
|
* For each class, the group "Default" is an alias for the group
|
|
|
|
* "<ClassName>", where <ClassName> is the non-namespaced name of the
|
2011-01-16 04:41:23 +00:00
|
|
|
* class. All constraints implicitly or explicitly assigned to group
|
2010-11-18 22:10:21 +00:00
|
|
|
* "Default" belong to both of these groups, unless the class defines
|
|
|
|
* a group sequence.
|
|
|
|
*
|
|
|
|
* If a class defines a group sequence, validating the class in "Default"
|
2011-01-16 04:41:23 +00:00
|
|
|
* will validate the group sequence. The constraints assigned to "Default"
|
2010-11-18 22:10:21 +00:00
|
|
|
* can still be validated by validating the class in "<ClassName>".
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return string The name of the default group
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2010-11-18 22:10:21 +00:00
|
|
|
public function getDefaultGroup()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-11-18 22:10:21 +00:00
|
|
|
return $this->defaultGroup;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function addConstraint(Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2011-03-31 13:02:00 +01:00
|
|
|
if (!in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
|
2011-01-19 14:30:43 +00:00
|
|
|
throw new ConstraintDefinitionException(sprintf(
|
2014-02-18 17:03:59 +00:00
|
|
|
'The constraint "%s" cannot be put on classes.',
|
|
|
|
get_class($constraint)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint instanceof Valid) {
|
|
|
|
throw new ConstraintDefinitionException(sprintf(
|
|
|
|
'The constraint "%s" cannot be put on classes.',
|
2011-01-26 08:54:11 +00:00
|
|
|
get_class($constraint)
|
|
|
|
));
|
2010-11-16 22:41:46 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 18:10:26 +00:00
|
|
|
if ($constraint instanceof Traverse) {
|
2014-02-21 16:10:39 +00:00
|
|
|
if ($constraint->traverse) {
|
2014-02-20 18:10:26 +00:00
|
|
|
// If traverse is true, traversal should be explicitly enabled
|
|
|
|
$this->traversalStrategy = TraversalStrategy::TRAVERSE;
|
2014-02-21 16:10:39 +00:00
|
|
|
} else {
|
2014-02-20 18:10:26 +00:00
|
|
|
// If traverse is false, traversal should be explicitly disabled
|
|
|
|
$this->traversalStrategy = TraversalStrategy::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The constraint is not added
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:10:21 +00:00
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
parent::addConstraint($constraint);
|
2014-02-21 13:44:01 +00:00
|
|
|
|
|
|
|
return $this;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Adds a constraint to the given property.
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param Constraint $constraint The constraint
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function addPropertyConstraint($property, Constraint $constraint)
|
|
|
|
{
|
|
|
|
if (!isset($this->properties[$property])) {
|
|
|
|
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2014-08-19 15:41:47 +01:00
|
|
|
$this->addPropertyMetadata($this->properties[$property]);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-18 22:10:21 +00:00
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->properties[$property]->addConstraint($constraint);
|
|
|
|
|
|
|
|
return $this;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2014-09-13 00:52:47 +01:00
|
|
|
/**
|
|
|
|
* @param string $property
|
|
|
|
* @param Constraint[] $constraints
|
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2014-09-13 00:52:47 +01:00
|
|
|
*/
|
|
|
|
public function addPropertyConstraints($property, array $constraints)
|
|
|
|
{
|
|
|
|
foreach ($constraints as $constraint) {
|
|
|
|
$this->addPropertyConstraint($property, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Adds a constraint to the getter of the given property.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
|
|
|
* The name of the getter is assumed to be the name of the property with an
|
|
|
|
* uppercased first letter and either the prefix "get" or "is".
|
|
|
|
*
|
2017-03-14 08:04:19 +00:00
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param Constraint $constraint The constraint
|
2010-10-15 22:49:09 +01:00
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function addGetterConstraint($property, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!isset($this->getters[$property])) {
|
|
|
|
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
|
|
|
|
|
2014-08-19 15:41:47 +01:00
|
|
|
$this->addPropertyMetadata($this->getters[$property]);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2010-11-18 22:10:21 +00:00
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->getters[$property]->addConstraint($constraint);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2016-12-31 15:58:15 +00:00
|
|
|
/**
|
|
|
|
* Adds a constraint to the getter of the given property.
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param string $method The name of the getter method
|
|
|
|
* @param Constraint $constraint The constraint
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addGetterMethodConstraint($property, $method, Constraint $constraint)
|
|
|
|
{
|
|
|
|
if (!isset($this->getters[$property])) {
|
|
|
|
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property, $method);
|
|
|
|
|
|
|
|
$this->addPropertyMetadata($this->getters[$property]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
|
|
|
|
|
|
|
$this->getters[$property]->addConstraint($constraint);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-13 00:52:47 +01:00
|
|
|
/**
|
|
|
|
* @param string $property
|
|
|
|
* @param Constraint[] $constraints
|
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2014-09-13 00:52:47 +01:00
|
|
|
*/
|
|
|
|
public function addGetterConstraints($property, array $constraints)
|
|
|
|
{
|
|
|
|
foreach ($constraints as $constraint) {
|
|
|
|
$this->addGetterConstraint($property, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-12-31 15:58:15 +00:00
|
|
|
/**
|
|
|
|
* @param string $property
|
|
|
|
* @param string $method
|
|
|
|
* @param Constraint[] $constraints
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addGetterMethodConstraints($property, $method, array $constraints)
|
|
|
|
{
|
|
|
|
foreach ($constraints as $constraint) {
|
|
|
|
$this->addGetterMethodConstraint($property, $method, $constraint);
|
|
|
|
}
|
|
|
|
|
2014-09-13 00:52:47 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Merges the constraints of the given metadata into this object.
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function mergeConstraints(ClassMetadata $source)
|
|
|
|
{
|
2017-08-17 22:27:11 +01:00
|
|
|
if ($source->isGroupSequenceProvider()) {
|
|
|
|
$this->setGroupSequenceProvider(true);
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($source->getConstraints() as $constraint) {
|
|
|
|
$this->addConstraint(clone $constraint);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($source->getConstrainedProperties() as $property) {
|
2014-08-19 15:41:47 +01:00
|
|
|
foreach ($source->getPropertyMetadata($property) as $member) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$member = clone $member;
|
|
|
|
|
|
|
|
foreach ($member->getConstraints() as $constraint) {
|
2017-01-06 13:26:32 +00:00
|
|
|
if (in_array($constraint::DEFAULT_GROUP, $constraint->groups, true)) {
|
|
|
|
$member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:10:21 +00:00
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2014-08-19 15:41:47 +01:00
|
|
|
$this->addPropertyMetadata($member);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2014-08-19 15:41:47 +01:00
|
|
|
if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$property = $member->getPropertyName();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
|
|
|
|
$this->properties[$property] = $member;
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->getters[$property] = $member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2013-02-05 10:07:10 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function hasPropertyMetadata($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->members);
|
|
|
|
}
|
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getPropertyMetadata($property)
|
|
|
|
{
|
2014-08-08 09:52:31 +01:00
|
|
|
if (!isset($this->members[$property])) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
return $this->members[$property];
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getConstrainedProperties()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return array_keys($this->members);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Sets the default group sequence for this class.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2014-02-12 11:38:50 +00:00
|
|
|
* @param array $groupSequence An array of group names
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @throws GroupDefinitionException
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2014-02-12 11:38:50 +00:00
|
|
|
public function setGroupSequence($groupSequence)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-02-02 14:59:37 +00:00
|
|
|
if ($this->isGroupSequenceProvider()) {
|
2012-01-28 21:10:33 +00:00
|
|
|
throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:38:50 +00:00
|
|
|
if (is_array($groupSequence)) {
|
|
|
|
$groupSequence = new GroupSequence($groupSequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array(Constraint::DEFAULT_GROUP, $groupSequence->groups, true)) {
|
2010-11-18 22:10:21 +00:00
|
|
|
throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:38:50 +00:00
|
|
|
if (!in_array($this->getDefaultGroup(), $groupSequence->groups, true)) {
|
2010-11-18 22:10:21 +00:00
|
|
|
throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:38:50 +00:00
|
|
|
$this->groupSequence = $groupSequence;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function hasGroupSequence()
|
|
|
|
{
|
2015-01-09 06:49:26 +00:00
|
|
|
return $this->groupSequence && count($this->groupSequence->groups) > 0;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getGroupSequence()
|
|
|
|
{
|
|
|
|
return $this->groupSequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Returns a ReflectionClass instance for this class.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return \ReflectionClass
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getReflectionClass()
|
|
|
|
{
|
|
|
|
if (!$this->reflClass) {
|
|
|
|
$this->reflClass = new \ReflectionClass($this->getClassName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reflClass;
|
|
|
|
}
|
2012-01-27 01:33:45 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-01 15:08:59 +00:00
|
|
|
* Sets whether a group sequence provider should be used.
|
2012-01-27 01:33:45 +00:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param bool $active
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @throws GroupDefinitionException
|
2012-01-27 01:33:45 +00:00
|
|
|
*/
|
2012-01-28 21:10:33 +00:00
|
|
|
public function setGroupSequenceProvider($active)
|
2012-01-27 01:33:45 +00:00
|
|
|
{
|
2012-01-30 22:51:45 +00:00
|
|
|
if ($this->hasGroupSequence()) {
|
2012-01-28 21:10:33 +00:00
|
|
|
throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
|
|
|
|
}
|
2012-01-27 01:33:45 +00:00
|
|
|
|
2012-01-28 21:10:33 +00:00
|
|
|
if (!$this->getReflectionClass()->implementsInterface('Symfony\Component\Validator\GroupSequenceProviderInterface')) {
|
|
|
|
throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface', $this->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->groupSequenceProvider = $active;
|
2012-01-27 01:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-21 17:00:47 +00:00
|
|
|
* {@inheritdoc}
|
2012-01-27 01:33:45 +00:00
|
|
|
*/
|
2012-02-02 14:59:37 +00:00
|
|
|
public function isGroupSequenceProvider()
|
2012-01-27 01:33:45 +00:00
|
|
|
{
|
|
|
|
return $this->groupSequenceProvider;
|
|
|
|
}
|
2014-02-18 12:03:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class nodes are never cascaded.
|
|
|
|
*
|
2014-08-19 16:01:40 +01:00
|
|
|
* {@inheritdoc}
|
2014-02-18 12:03:34 +00:00
|
|
|
*/
|
|
|
|
public function getCascadingStrategy()
|
|
|
|
{
|
|
|
|
return CascadingStrategy::NONE;
|
|
|
|
}
|
2014-08-19 15:41:47 +01:00
|
|
|
|
|
|
|
private function addPropertyMetadata(PropertyMetadataInterface $metadata)
|
|
|
|
{
|
|
|
|
$property = $metadata->getPropertyName();
|
|
|
|
|
|
|
|
$this->members[$property][] = $metadata;
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|