7fc19dd2f702099522cc95067452e3f0404e117f
[yaffs-website] / vendor / symfony / validator / Mapping / PropertyMetadata.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Mapping;
13
14 use Symfony\Component\Validator\Exception\ValidatorException;
15
16 /**
17  * Stores all metadata needed for validating a class property.
18  *
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.
22  *
23  * This class supports serialization and cloning.
24  *
25  * @author Bernhard Schussek <bschussek@gmail.com>
26  *
27  * @see PropertyMetadataInterface
28  */
29 class PropertyMetadata extends MemberMetadata
30 {
31     /**
32      * @param string $class The class this property is defined on
33      * @param string $name  The name of this property
34      *
35      * @throws ValidatorException
36      */
37     public function __construct($class, $name)
38     {
39         if (!property_exists($class, $name)) {
40             throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));
41         }
42
43         parent::__construct($class, $name, $name);
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     public function getPropertyValue($object)
50     {
51         return $this->getReflectionMember($object)->getValue($object);
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function newReflectionMember($objectOrClassName)
58     {
59         $originalClass = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
60
61         while (!property_exists($objectOrClassName, $this->getName())) {
62             $objectOrClassName = get_parent_class($objectOrClassName);
63
64             if (false === $objectOrClassName) {
65                 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
66             }
67         }
68
69         $member = new \ReflectionProperty($objectOrClassName, $this->getName());
70         $member->setAccessible(true);
71
72         return $member;
73     }
74 }