d71a13be71f03f92f79850b32feb64664bad0bbb
[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(
76                 'The constraint %s cannot be put on properties or getters',
77                 get_class($constraint)
78             ));
79         }
80
81         parent::addConstraint($constraint);
82
83         return $this;
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function __sleep()
90     {
91         return array_merge(parent::__sleep(), array(
92             'class',
93             'name',
94             'property',
95         ));
96     }
97
98     /**
99      * Returns the name of the member.
100      *
101      * @return string
102      */
103     public function getName()
104     {
105         return $this->name;
106     }
107
108     /**
109      * {@inheritdoc}
110      */
111     public function getClassName()
112     {
113         return $this->class;
114     }
115
116     /**
117      * {@inheritdoc}
118      */
119     public function getPropertyName()
120     {
121         return $this->property;
122     }
123
124     /**
125      * Returns whether this member is public.
126      *
127      * @param object|string $objectOrClassName The object or the class name
128      *
129      * @return bool
130      */
131     public function isPublic($objectOrClassName)
132     {
133         return $this->getReflectionMember($objectOrClassName)->isPublic();
134     }
135
136     /**
137      * Returns whether this member is protected.
138      *
139      * @param object|string $objectOrClassName The object or the class name
140      *
141      * @return bool
142      */
143     public function isProtected($objectOrClassName)
144     {
145         return $this->getReflectionMember($objectOrClassName)->isProtected();
146     }
147
148     /**
149      * Returns whether this member is private.
150      *
151      * @param object|string $objectOrClassName The object or the class name
152      *
153      * @return bool
154      */
155     public function isPrivate($objectOrClassName)
156     {
157         return $this->getReflectionMember($objectOrClassName)->isPrivate();
158     }
159
160     /**
161      * Returns the reflection instance for accessing the member's value.
162      *
163      * @param object|string $objectOrClassName The object or the class name
164      *
165      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
166      */
167     public function getReflectionMember($objectOrClassName)
168     {
169         $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
170         if (!isset($this->reflMember[$className])) {
171             $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
172         }
173
174         return $this->reflMember[$className];
175     }
176
177     /**
178      * Creates a new reflection instance for accessing the member's value.
179      *
180      * Must be implemented by subclasses.
181      *
182      * @param object|string $objectOrClassName The object or the class name
183      *
184      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
185      */
186     abstract protected function newReflectionMember($objectOrClassName);
187 }