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\ClassBasedInterface;
|
|
|
|
use Symfony\Component\Validator\PropertyMetadataInterface;
|
2010-11-16 22:41:46 +00:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Constraints\Valid;
|
2011-01-19 14:30:43 +00:00
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
abstract class MemberMetadata extends ElementMetadata implements PropertyMetadataInterface, ClassBasedInterface
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-29 16:20:29 +01:00
|
|
|
public $class;
|
|
|
|
public $name;
|
|
|
|
public $property;
|
2010-11-16 22:41:46 +00:00
|
|
|
public $cascaded = false;
|
2011-01-27 15:25:25 +00:00
|
|
|
public $collectionCascaded = false;
|
2012-05-18 11:22:33 +01:00
|
|
|
public $collectionCascadedDeeply = false;
|
2013-03-05 15:41:51 +00:00
|
|
|
private $reflMember = array();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @param string $class The name of the class this member is defined on
|
|
|
|
* @param string $name The name of the member
|
|
|
|
* @param string $property The property the member belongs to
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function __construct($class, $name, $property)
|
|
|
|
{
|
|
|
|
$this->class = $class;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->property = $property;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
|
|
|
|
{
|
|
|
|
$visitor->visit($this, $value, $group, $propertyPath);
|
|
|
|
|
|
|
|
if ($this->isCascaded()) {
|
|
|
|
$visitor->validate($value, $propagatedGroup ?: $group, $propertyPath, $this->isCollectionCascaded(), $this->isCollectionCascadedDeeply());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-16 22:41:46 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2010-11-16 22:41:46 +00:00
|
|
|
*/
|
|
|
|
public function addConstraint(Constraint $constraint)
|
|
|
|
{
|
2011-03-31 13:02:00 +01:00
|
|
|
if (!in_array(Constraint::PROPERTY_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 properties or getters',
|
|
|
|
get_class($constraint)
|
|
|
|
));
|
2011-01-19 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 22:41:46 +00:00
|
|
|
if ($constraint instanceof Valid) {
|
|
|
|
$this->cascaded = true;
|
2012-05-18 11:22:33 +01:00
|
|
|
/* @var Valid $constraint */
|
2011-01-27 15:25:25 +00:00
|
|
|
$this->collectionCascaded = $constraint->traverse;
|
2012-05-18 11:22:33 +01:00
|
|
|
$this->collectionCascadedDeeply = $constraint->deep;
|
2010-11-16 22:41:46 +00:00
|
|
|
} else {
|
|
|
|
parent::addConstraint($constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-06-29 16:20:29 +01:00
|
|
|
/**
|
|
|
|
* Returns the names of the properties that should be serialized
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
return array_merge(parent::__sleep(), array(
|
|
|
|
'class',
|
|
|
|
'name',
|
2010-11-16 22:41:46 +00:00
|
|
|
'property',
|
2013-07-02 10:24:50 +01:00
|
|
|
'cascaded',
|
|
|
|
'collectionCascaded',
|
2013-08-06 06:58:11 +01:00
|
|
|
'collectionCascadedDeeply',
|
2010-06-29 16:20:29 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the name of the member
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the class this member is defined on
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->class;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the name of the property this member belongs to
|
|
|
|
*
|
2010-10-15 22:49:09 +01:00
|
|
|
* @return string The property name
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
public function getPropertyName()
|
|
|
|
{
|
|
|
|
return $this->property;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this member is public
|
|
|
|
*
|
2013-03-05 15:41:51 +00:00
|
|
|
* @param object|string $objectOrClassName The object or the class name
|
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2013-03-05 15:41:51 +00:00
|
|
|
public function isPublic($objectOrClassName)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2013-03-05 15:41:51 +00:00
|
|
|
return $this->getReflectionMember($objectOrClassName)->isPublic();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this member is protected
|
|
|
|
*
|
2013-03-05 15:41:51 +00:00
|
|
|
* @param object|string $objectOrClassName The object or the class name
|
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2013-03-05 15:41:51 +00:00
|
|
|
public function isProtected($objectOrClassName)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2013-03-05 15:41:51 +00:00
|
|
|
return $this->getReflectionMember($objectOrClassName)->isProtected();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this member is private
|
|
|
|
*
|
2013-03-05 15:41:51 +00:00
|
|
|
* @param object|string $objectOrClassName The object or the class name
|
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2013-03-05 15:41:51 +00:00
|
|
|
public function isPrivate($objectOrClassName)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2013-03-05 15:41:51 +00:00
|
|
|
return $this->getReflectionMember($objectOrClassName)->isPrivate();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 22:41:46 +00:00
|
|
|
/**
|
|
|
|
* Returns whether objects stored in this member should be validated
|
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-11-16 22:41:46 +00:00
|
|
|
*/
|
|
|
|
public function isCascaded()
|
|
|
|
{
|
|
|
|
return $this->cascaded;
|
|
|
|
}
|
|
|
|
|
2011-01-27 15:25:25 +00:00
|
|
|
/**
|
|
|
|
* Returns whether arrays or traversable objects stored in this member
|
|
|
|
* should be traversed and validated in each entry
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isCollectionCascaded()
|
|
|
|
{
|
|
|
|
return $this->collectionCascaded;
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:22:33 +01:00
|
|
|
/**
|
|
|
|
* Returns whether arrays or traversable objects stored in this member
|
|
|
|
* should be traversed recursively for inner arrays/traversable objects
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isCollectionCascadedDeeply()
|
|
|
|
{
|
|
|
|
return $this->collectionCascadedDeeply;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the Reflection instance of the member
|
|
|
|
*
|
2013-03-05 15:41:51 +00:00
|
|
|
* @param object|string $objectOrClassName The object or the class name
|
|
|
|
*
|
2010-06-24 10:24:08 +01:00
|
|
|
* @return object
|
|
|
|
*/
|
2013-03-05 15:41:51 +00:00
|
|
|
public function getReflectionMember($objectOrClassName)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2013-03-05 15:41:51 +00:00
|
|
|
$className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
|
|
|
|
if (!isset($this->reflMember[$className])) {
|
|
|
|
$this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2013-03-05 15:41:51 +00:00
|
|
|
return $this->reflMember[$className];
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Creates a new Reflection instance for the member
|
|
|
|
*
|
2013-03-05 15:41:51 +00:00
|
|
|
* @param object|string $objectOrClassName The object or the class name
|
|
|
|
*
|
|
|
|
* @return mixed Reflection class
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2013-03-05 15:41:51 +00:00
|
|
|
abstract protected function newReflectionMember($objectOrClassName);
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|