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\Exception\ValidatorException;
17 * Stores all metadata needed for validating a class property.
19 * The value of the property is obtained by directly accessing the property.
20 * The property will be accessed by reflection, so the access of private and
21 * protected properties is supported.
23 * This class supports serialization and cloning.
25 * @author Bernhard Schussek <bschussek@gmail.com>
27 * @see PropertyMetadataInterface
29 class PropertyMetadata extends MemberMetadata
34 * @param string $class The class this property is defined on
35 * @param string $name The name of this property
37 * @throws ValidatorException
39 public function __construct($class, $name)
41 if (!property_exists($class, $name)) {
42 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));
45 parent::__construct($class, $name, $name);
51 public function getPropertyValue($object)
53 return $this->getReflectionMember($object)->getValue($object);
59 protected function newReflectionMember($objectOrClassName)
61 $originalClass = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
63 while (!property_exists($objectOrClassName, $this->getName())) {
64 $objectOrClassName = get_parent_class($objectOrClassName);
66 if (false === $objectOrClassName) {
67 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
71 $member = new \ReflectionProperty($objectOrClassName, $this->getName());
72 $member->setAccessible(true);