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;
18 * Stores all metadata needed for validating a class property.
20 * The method of accessing the property's value must be specified by subclasses
21 * by implementing the {@link newReflectionMember()} method.
23 * This class supports serialization and cloning.
25 * @author Bernhard Schussek <bschussek@gmail.com>
27 * @see PropertyMetadataInterface
29 abstract class MemberMetadata extends GenericMetadata implements PropertyMetadataInterface
32 * @internal This property is public in order to reduce the size of the
33 * class' serialized representation. Do not access it. Use
34 * {@link getClassName()} instead.
39 * @internal This property is public in order to reduce the size of the
40 * class' serialized representation. Do not access it. Use
41 * {@link getName()} instead.
46 * @internal This property is public in order to reduce the size of the
47 * class' serialized representation. Do not access it. Use
48 * {@link getPropertyName()} instead.
53 * @var \ReflectionMethod[]|\ReflectionProperty[]
55 private $reflMember = array();
58 * @param string $class The name of the class this member is defined on
59 * @param string $name The name of the member
60 * @param string $property The property the member belongs to
62 public function __construct($class, $name, $property)
64 $this->class = $class;
66 $this->property = $property;
72 public function addConstraint(Constraint $constraint)
74 if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
75 throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', \get_class($constraint)));
78 parent::addConstraint($constraint);
86 public function __sleep()
88 return array_merge(parent::__sleep(), array(
96 * Returns the name of the member.
100 public function getName()
108 public function getClassName()
116 public function getPropertyName()
118 return $this->property;
122 * Returns whether this member is public.
124 * @param object|string $objectOrClassName The object or the class name
128 public function isPublic($objectOrClassName)
130 return $this->getReflectionMember($objectOrClassName)->isPublic();
134 * Returns whether this member is protected.
136 * @param object|string $objectOrClassName The object or the class name
140 public function isProtected($objectOrClassName)
142 return $this->getReflectionMember($objectOrClassName)->isProtected();
146 * Returns whether this member is private.
148 * @param object|string $objectOrClassName The object or the class name
152 public function isPrivate($objectOrClassName)
154 return $this->getReflectionMember($objectOrClassName)->isPrivate();
158 * Returns the reflection instance for accessing the member's value.
160 * @param object|string $objectOrClassName The object or the class name
162 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
164 public function getReflectionMember($objectOrClassName)
166 $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
167 if (!isset($this->reflMember[$className])) {
168 $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
171 return $this->reflMember[$className];
175 * Creates a new reflection instance for accessing the member's value.
177 * Must be implemented by subclasses.
179 * @param object|string $objectOrClassName The object or the class name
181 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
183 abstract protected function newReflectionMember($objectOrClassName);