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;
|
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
use Symfony\Component\Validator\ValidationVisitorInterface;
|
|
|
|
use Symfony\Component\Validator\PropertyMetadataContainerInterface;
|
|
|
|
use Symfony\Component\Validator\ClassBasedInterface;
|
|
|
|
use Symfony\Component\Validator\MetadataInterface;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
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
|
|
|
/**
|
|
|
|
* Represents all the configured constraints on a given class.
|
|
|
|
*
|
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
|
|
|
*/
|
2012-11-22 14:58:46 +00:00
|
|
|
class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassBasedInterface, PropertyMetadataContainerInterface
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $name;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-11-18 22:10:21 +00:00
|
|
|
public $defaultGroup;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MemberMetadata[]
|
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $members = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PropertyMetadata[]
|
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $properties = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GetterMetadata[]
|
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $getters = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-06-29 16:20:29 +01:00
|
|
|
public $groupSequence = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
2012-01-28 21:10:33 +00:00
|
|
|
public $groupSequenceProvider = false;
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \ReflectionClass
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
private $reflClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a metadata for the given class
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
|
|
|
|
{
|
|
|
|
if (null === $propagatedGroup && Constraint::DEFAULT_GROUP === $group
|
|
|
|
&& ($this->hasGroupSequence() || $this->isGroupSequenceProvider())) {
|
|
|
|
if ($this->hasGroupSequence()) {
|
|
|
|
$groups = $this->getGroupSequence();
|
|
|
|
} else {
|
|
|
|
$groups = $value->getGroupSequence();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$this->accept($visitor, $value, $group, $propertyPath, Constraint::DEFAULT_GROUP);
|
|
|
|
|
|
|
|
if (count($visitor->getViolations()) > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$visitor->visit($this, $value, $group, $propertyPath);
|
|
|
|
|
|
|
|
if (null !== $value) {
|
|
|
|
$pathPrefix = empty($propertyPath) ? '' : $propertyPath.'.';
|
|
|
|
|
|
|
|
foreach ($this->getConstrainedProperties() as $property) {
|
|
|
|
foreach ($this->getMemberMetadatas($property) as $member) {
|
|
|
|
$member->accept($visitor, $member->getValue($value), $group, $pathPrefix.$property, $propagatedGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-29 16:20:29 +01:00
|
|
|
/**
|
|
|
|
* Returns the properties to be serialized
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
return array_merge(parent::__sleep(), array(
|
|
|
|
'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',
|
2010-11-18 22:10:21 +00:00
|
|
|
'defaultGroup'
|
2010-06-29 16:20:29 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the fully qualified name of the class
|
|
|
|
*
|
|
|
|
* @return string The fully qualified class name
|
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-18 22:10:21 +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>".
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
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(
|
2011-01-26 08:54:11 +00:00
|
|
|
'The constraint %s cannot be put on classes',
|
|
|
|
get_class($constraint)
|
|
|
|
));
|
2010-11-16 22:41:46 +00: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
|
|
|
parent::addConstraint($constraint);
|
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
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return ClassMetadata This object
|
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
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->addMemberMetadata($this->properties[$property]);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param Constraint $constraint The constraint
|
|
|
|
*
|
|
|
|
* @return ClassMetadata This object
|
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);
|
|
|
|
|
|
|
|
$this->addMemberMetadata($this->getters[$property]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @param ClassMetadata $source The source metadata
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function mergeConstraints(ClassMetadata $source)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
foreach ($source->getMemberMetadatas($property) as $member) {
|
|
|
|
$member = clone $member;
|
|
|
|
|
|
|
|
foreach ($member->getConstraints() as $constraint) {
|
2010-11-18 22:10:21 +00:00
|
|
|
$constraint->addImplicitGroupName($this->getDefaultGroup());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->addMemberMetadata($member);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$member->isPrivate()) {
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
2012-11-01 15:08:59 +00:00
|
|
|
* Adds a member metadata.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
|
|
|
* @param MemberMetadata $metadata
|
|
|
|
*/
|
|
|
|
protected function addMemberMetadata(MemberMetadata $metadata)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$property = $metadata->getPropertyName();
|
|
|
|
|
|
|
|
$this->members[$property][] = $metadata;
|
|
|
|
}
|
|
|
|
|
2011-02-02 10:10:42 +00:00
|
|
|
/**
|
|
|
|
* Returns true if metadatas of members is present for the given property.
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function hasMemberMetadatas($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->members);
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2012-11-01 15:08:59 +00:00
|
|
|
* Returns all metadatas of members describing the given property.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @param string $property The name of the property
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return MemberMetadata[] An array of MemberMetadata
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getMemberMetadatas($property)
|
|
|
|
{
|
|
|
|
return $this->members[$property];
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getPropertyMetadata($property)
|
|
|
|
{
|
|
|
|
return $this->members[$property];
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Returns all properties for which constraints are defined.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @return array An array of property names
|
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
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @param array $groups An array of group names
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function setGroupSequence(array $groups)
|
|
|
|
{
|
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');
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:10:21 +00:00
|
|
|
if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
|
|
|
|
throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($this->getDefaultGroup(), $groups, true)) {
|
|
|
|
throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->groupSequence = $groups;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Returns whether this class has an overridden default group sequence.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function hasGroupSequence()
|
|
|
|
{
|
|
|
|
return count($this->groupSequence) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-15 22:49:09 +01:00
|
|
|
* Returns the default group sequence for this class.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @return array An array of group names
|
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
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param Boolean $active
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-02 14:59:37 +00:00
|
|
|
* Returns whether the class is a group sequence provider.
|
2012-01-27 01:33:45 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return Boolean
|
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;
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|