4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Mapping;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16 use Symfony\Component\Validator\ValidationVisitorInterface;
19 * Stores all metadata needed for validating a class property.
21 * The method of accessing the property's value must be specified by subclasses
22 * by implementing the {@link newReflectionMember()} method.
24 * This class supports serialization and cloning.
26 * @author Bernhard Schussek <bschussek@gmail.com>
28 * @see PropertyMetadataInterface
30 abstract class MemberMetadata extends ElementMetadata implements PropertyMetadataInterface
35 * @internal This property is public in order to reduce the size of the
36 * class' serialized representation. Do not access it. Use
37 * {@link getClassName()} instead.
44 * @internal This property is public in order to reduce the size of the
45 * class' serialized representation. Do not access it. Use
46 * {@link getName()} instead.
53 * @internal This property is public in order to reduce the size of the
54 * class' serialized representation. Do not access it. Use
55 * {@link getPropertyName()} instead.
60 * @var \ReflectionMethod[]|\ReflectionProperty[]
62 private $reflMember = array();
67 * @param string $class The name of the class this member is defined on
68 * @param string $name The name of the member
69 * @param string $property The property the member belongs to
71 public function __construct($class, $name, $property)
73 $this->class = $class;
75 $this->property = $property;
81 * @deprecated since version 2.5, to be removed in 3.0.
83 public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
85 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
87 $visitor->visit($this, $value, $group, $propertyPath);
89 if ($this->isCascaded()) {
90 $visitor->validate($value, $propagatedGroup ?: $group, $propertyPath, $this->isCollectionCascaded(), $this->isCollectionCascadedDeeply());
97 public function addConstraint(Constraint $constraint)
99 if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
100 throw new ConstraintDefinitionException(sprintf(
101 'The constraint %s cannot be put on properties or getters',
102 get_class($constraint)
106 parent::addConstraint($constraint);
114 public function __sleep()
116 return array_merge(parent::__sleep(), array(
124 * Returns the name of the member.
128 public function getName()
136 public function getClassName()
144 public function getPropertyName()
146 return $this->property;
150 * Returns whether this member is public.
152 * @param object|string $objectOrClassName The object or the class name
156 public function isPublic($objectOrClassName)
158 return $this->getReflectionMember($objectOrClassName)->isPublic();
162 * Returns whether this member is protected.
164 * @param object|string $objectOrClassName The object or the class name
168 public function isProtected($objectOrClassName)
170 return $this->getReflectionMember($objectOrClassName)->isProtected();
174 * Returns whether this member is private.
176 * @param object|string $objectOrClassName The object or the class name
180 public function isPrivate($objectOrClassName)
182 return $this->getReflectionMember($objectOrClassName)->isPrivate();
186 * Returns whether objects stored in this member should be validated.
190 * @deprecated since version 2.5, to be removed in 3.0.
191 * Use {@link getCascadingStrategy()} instead.
193 public function isCascaded()
195 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getCascadingStrategy() method instead.', E_USER_DEPRECATED);
197 return (bool) ($this->cascadingStrategy & CascadingStrategy::CASCADE);
201 * Returns whether arrays or traversable objects stored in this member
202 * should be traversed and validated in each entry.
206 * @deprecated since version 2.5, to be removed in 3.0.
207 * Use {@link getTraversalStrategy()} instead.
209 public function isCollectionCascaded()
211 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
213 return (bool) ($this->traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE));
217 * Returns whether arrays or traversable objects stored in this member
218 * should be traversed recursively for inner arrays/traversable objects.
222 * @deprecated since version 2.5, to be removed in 3.0.
223 * Use {@link getTraversalStrategy()} instead.
225 public function isCollectionCascadedDeeply()
227 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
229 return !($this->traversalStrategy & TraversalStrategy::STOP_RECURSION);
233 * Returns the reflection instance for accessing the member's value.
235 * @param object|string $objectOrClassName The object or the class name
237 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
239 public function getReflectionMember($objectOrClassName)
241 $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
242 if (!isset($this->reflMember[$className])) {
243 $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
246 return $this->reflMember[$className];
250 * Creates a new reflection instance for accessing the member's value.
252 * Must be implemented by subclasses.
254 * @param object|string $objectOrClassName The object or the class name
256 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
258 abstract protected function newReflectionMember($objectOrClassName);