74a524b194c54d01bed1be24f348fa5f699a2014
[yaffs-website] / vendor / symfony / validator / Mapping / MemberMetadata.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\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16
17 /**
18  * Stores all metadata needed for validating a class property.
19  *
20  * The method of accessing the property's value must be specified by subclasses
21  * by implementing the {@link newReflectionMember()} method.
22  *
23  * This class supports serialization and cloning.
24  *
25  * @author Bernhard Schussek <bschussek@gmail.com>
26  *
27  * @see PropertyMetadataInterface
28  */
29 abstract class MemberMetadata extends GenericMetadata implements PropertyMetadataInterface
30 {
31     /**
32      * @internal This property is public in order to reduce the size of the
33      *           class' serialized representation. Do not access it. Use
34      *           {@link getClassName()} instead.
35      */
36     public $class;
37
38     /**
39      * @internal This property is public in order to reduce the size of the
40      *           class' serialized representation. Do not access it. Use
41      *           {@link getName()} instead.
42      */
43     public $name;
44
45     /**
46      * @internal This property is public in order to reduce the size of the
47      *           class' serialized representation. Do not access it. Use
48      *           {@link getPropertyName()} instead.
49      */
50     public $property;
51
52     /**
53      * @var \ReflectionMethod[]|\ReflectionProperty[]
54      */
55     private $reflMember = array();
56
57     /**
58      * @param string $class    The name of the class this member is defined on
59      * @param string $name     The name of the member
60      * @param string $property The property the member belongs to
61      */
62     public function __construct($class, $name, $property)
63     {
64         $this->class = $class;
65         $this->name = $name;
66         $this->property = $property;
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     public function addConstraint(Constraint $constraint)
73     {
74         if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
75             throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', \get_class($constraint)));
76         }
77
78         parent::addConstraint($constraint);
79
80         return $this;
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     public function __sleep()
87     {
88         return array_merge(parent::__sleep(), array(
89             'class',
90             'name',
91             'property',
92         ));
93     }
94
95     /**
96      * Returns the name of the member.
97      *
98      * @return string
99      */
100     public function getName()
101     {
102         return $this->name;
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     public function getClassName()
109     {
110         return $this->class;
111     }
112
113     /**
114      * {@inheritdoc}
115      */
116     public function getPropertyName()
117     {
118         return $this->property;
119     }
120
121     /**
122      * Returns whether this member is public.
123      *
124      * @param object|string $objectOrClassName The object or the class name
125      *
126      * @return bool
127      */
128     public function isPublic($objectOrClassName)
129     {
130         return $this->getReflectionMember($objectOrClassName)->isPublic();
131     }
132
133     /**
134      * Returns whether this member is protected.
135      *
136      * @param object|string $objectOrClassName The object or the class name
137      *
138      * @return bool
139      */
140     public function isProtected($objectOrClassName)
141     {
142         return $this->getReflectionMember($objectOrClassName)->isProtected();
143     }
144
145     /**
146      * Returns whether this member is private.
147      *
148      * @param object|string $objectOrClassName The object or the class name
149      *
150      * @return bool
151      */
152     public function isPrivate($objectOrClassName)
153     {
154         return $this->getReflectionMember($objectOrClassName)->isPrivate();
155     }
156
157     /**
158      * Returns the reflection instance for accessing the member's value.
159      *
160      * @param object|string $objectOrClassName The object or the class name
161      *
162      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
163      */
164     public function getReflectionMember($objectOrClassName)
165     {
166         $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
167         if (!isset($this->reflMember[$className])) {
168             $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
169         }
170
171         return $this->reflMember[$className];
172     }
173
174     /**
175      * Creates a new reflection instance for accessing the member's value.
176      *
177      * Must be implemented by subclasses.
178      *
179      * @param object|string $objectOrClassName The object or the class name
180      *
181      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
182      */
183     abstract protected function newReflectionMember($objectOrClassName);
184 }