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
34 * @internal This property is public in order to reduce the size of the
35 * class' serialized representation. Do not access it. Use
36 * {@link getClassName()} instead.
43 * @internal This property is public in order to reduce the size of the
44 * class' serialized representation. Do not access it. Use
45 * {@link getName()} instead.
52 * @internal This property is public in order to reduce the size of the
53 * class' serialized representation. Do not access it. Use
54 * {@link getPropertyName()} instead.
59 * @var \ReflectionMethod[]|\ReflectionProperty[]
61 private $reflMember = array();
66 * @param string $class The name of the class this member is defined on
67 * @param string $name The name of the member
68 * @param string $property The property the member belongs to
70 public function __construct($class, $name, $property)
72 $this->class = $class;
74 $this->property = $property;
80 public function addConstraint(Constraint $constraint)
82 if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
83 throw new ConstraintDefinitionException(sprintf(
84 'The constraint %s cannot be put on properties or getters',
85 get_class($constraint)
89 parent::addConstraint($constraint);
97 public function __sleep()
99 return array_merge(parent::__sleep(), array(
107 * Returns the name of the member.
111 public function getName()
119 public function getClassName()
127 public function getPropertyName()
129 return $this->property;
133 * Returns whether this member is public.
135 * @param object|string $objectOrClassName The object or the class name
139 public function isPublic($objectOrClassName)
141 return $this->getReflectionMember($objectOrClassName)->isPublic();
145 * Returns whether this member is protected.
147 * @param object|string $objectOrClassName The object or the class name
151 public function isProtected($objectOrClassName)
153 return $this->getReflectionMember($objectOrClassName)->isProtected();
157 * Returns whether this member is private.
159 * @param object|string $objectOrClassName The object or the class name
163 public function isPrivate($objectOrClassName)
165 return $this->getReflectionMember($objectOrClassName)->isPrivate();
169 * Returns the reflection instance for accessing the member's value.
171 * @param object|string $objectOrClassName The object or the class name
173 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
175 public function getReflectionMember($objectOrClassName)
177 $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
178 if (!isset($this->reflMember[$className])) {
179 $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
182 return $this->reflMember[$className];
186 * Creates a new reflection instance for accessing the member's value.
188 * Must be implemented by subclasses.
190 * @param object|string $objectOrClassName The object or the class name
192 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
194 abstract protected function newReflectionMember($objectOrClassName);