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
32 * @param string $class The class this property is defined on
33 * @param string $name The name of this property
35 * @throws ValidatorException
37 public function __construct($class, $name)
39 if (!property_exists($class, $name)) {
40 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));
43 parent::__construct($class, $name, $name);
49 public function getPropertyValue($object)
51 return $this->getReflectionMember($object)->getValue($object);
57 protected function newReflectionMember($objectOrClassName)
59 $originalClass = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
61 while (!property_exists($objectOrClassName, $this->getName())) {
62 $objectOrClassName = get_parent_class($objectOrClassName);
64 if (false === $objectOrClassName) {
65 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
69 $member = new \ReflectionProperty($objectOrClassName, $this->getName());
70 $member->setAccessible(true);