d12701cb44aba94505011a9b88a51c0ff37f963f
[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      * Constructor.
33      *
34      * @param string $class The class this property is defined on
35      * @param string $name  The name of this property
36      *
37      * @throws ValidatorException
38      */
39     public function __construct($class, $name)
40     {
41         if (!property_exists($class, $name)) {
42             throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));
43         }
44
45         parent::__construct($class, $name, $name);
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function getPropertyValue($object)
52     {
53         return $this->getReflectionMember($object)->getValue($object);
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     protected function newReflectionMember($objectOrClassName)
60     {
61         $originalClass = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
62
63         while (!property_exists($objectOrClassName, $this->getName())) {
64             $objectOrClassName = get_parent_class($objectOrClassName);
65
66             if (false === $objectOrClassName) {
67                 throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
68             }
69         }
70
71         $member = new \ReflectionProperty($objectOrClassName, $this->getName());
72         $member->setAccessible(true);
73
74         return $member;
75     }
76 }