Version 1
[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 use Symfony\Component\Validator\ValidationVisitorInterface;
17
18 /**
19  * Stores all metadata needed for validating a class property.
20  *
21  * The method of accessing the property's value must be specified by subclasses
22  * by implementing the {@link newReflectionMember()} method.
23  *
24  * This class supports serialization and cloning.
25  *
26  * @author Bernhard Schussek <bschussek@gmail.com>
27  *
28  * @see PropertyMetadataInterface
29  */
30 abstract class MemberMetadata extends ElementMetadata implements PropertyMetadataInterface
31 {
32     /**
33      * @var string
34      *
35      * @internal This property is public in order to reduce the size of the
36      *           class' serialized representation. Do not access it. Use
37      *           {@link getClassName()} instead.
38      */
39     public $class;
40
41     /**
42      * @var string
43      *
44      * @internal This property is public in order to reduce the size of the
45      *           class' serialized representation. Do not access it. Use
46      *           {@link getName()} instead.
47      */
48     public $name;
49
50     /**
51      * @var string
52      *
53      * @internal This property is public in order to reduce the size of the
54      *           class' serialized representation. Do not access it. Use
55      *           {@link getPropertyName()} instead.
56      */
57     public $property;
58
59     /**
60      * @var \ReflectionMethod[]|\ReflectionProperty[]
61      */
62     private $reflMember = array();
63
64     /**
65      * Constructor.
66      *
67      * @param string $class    The name of the class this member is defined on
68      * @param string $name     The name of the member
69      * @param string $property The property the member belongs to
70      */
71     public function __construct($class, $name, $property)
72     {
73         $this->class = $class;
74         $this->name = $name;
75         $this->property = $property;
76     }
77
78     /**
79      * {@inheritdoc}
80      *
81      * @deprecated since version 2.5, to be removed in 3.0.
82      */
83     public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
84     {
85         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
86
87         $visitor->visit($this, $value, $group, $propertyPath);
88
89         if ($this->isCascaded()) {
90             $visitor->validate($value, $propagatedGroup ?: $group, $propertyPath, $this->isCollectionCascaded(), $this->isCollectionCascadedDeeply());
91         }
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function addConstraint(Constraint $constraint)
98     {
99         if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
100             throw new ConstraintDefinitionException(sprintf(
101                 'The constraint %s cannot be put on properties or getters',
102                 get_class($constraint)
103             ));
104         }
105
106         parent::addConstraint($constraint);
107
108         return $this;
109     }
110
111     /**
112      * {@inheritdoc}
113      */
114     public function __sleep()
115     {
116         return array_merge(parent::__sleep(), array(
117             'class',
118             'name',
119             'property',
120         ));
121     }
122
123     /**
124      * Returns the name of the member.
125      *
126      * @return string
127      */
128     public function getName()
129     {
130         return $this->name;
131     }
132
133     /**
134      * {@inheritdoc}
135      */
136     public function getClassName()
137     {
138         return $this->class;
139     }
140
141     /**
142      * {@inheritdoc}
143      */
144     public function getPropertyName()
145     {
146         return $this->property;
147     }
148
149     /**
150      * Returns whether this member is public.
151      *
152      * @param object|string $objectOrClassName The object or the class name
153      *
154      * @return bool
155      */
156     public function isPublic($objectOrClassName)
157     {
158         return $this->getReflectionMember($objectOrClassName)->isPublic();
159     }
160
161     /**
162      * Returns whether this member is protected.
163      *
164      * @param object|string $objectOrClassName The object or the class name
165      *
166      * @return bool
167      */
168     public function isProtected($objectOrClassName)
169     {
170         return $this->getReflectionMember($objectOrClassName)->isProtected();
171     }
172
173     /**
174      * Returns whether this member is private.
175      *
176      * @param object|string $objectOrClassName The object or the class name
177      *
178      * @return bool
179      */
180     public function isPrivate($objectOrClassName)
181     {
182         return $this->getReflectionMember($objectOrClassName)->isPrivate();
183     }
184
185     /**
186      * Returns whether objects stored in this member should be validated.
187      *
188      * @return bool
189      *
190      * @deprecated since version 2.5, to be removed in 3.0.
191      *             Use {@link getCascadingStrategy()} instead.
192      */
193     public function isCascaded()
194     {
195         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getCascadingStrategy() method instead.', E_USER_DEPRECATED);
196
197         return (bool) ($this->cascadingStrategy & CascadingStrategy::CASCADE);
198     }
199
200     /**
201      * Returns whether arrays or traversable objects stored in this member
202      * should be traversed and validated in each entry.
203      *
204      * @return bool
205      *
206      * @deprecated since version 2.5, to be removed in 3.0.
207      *             Use {@link getTraversalStrategy()} instead.
208      */
209     public function isCollectionCascaded()
210     {
211         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
212
213         return (bool) ($this->traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE));
214     }
215
216     /**
217      * Returns whether arrays or traversable objects stored in this member
218      * should be traversed recursively for inner arrays/traversable objects.
219      *
220      * @return bool
221      *
222      * @deprecated since version 2.5, to be removed in 3.0.
223      *             Use {@link getTraversalStrategy()} instead.
224      */
225     public function isCollectionCascadedDeeply()
226     {
227         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
228
229         return !($this->traversalStrategy & TraversalStrategy::STOP_RECURSION);
230     }
231
232     /**
233      * Returns the reflection instance for accessing the member's value.
234      *
235      * @param object|string $objectOrClassName The object or the class name
236      *
237      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
238      */
239     public function getReflectionMember($objectOrClassName)
240     {
241         $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
242         if (!isset($this->reflMember[$className])) {
243             $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
244         }
245
246         return $this->reflMember[$className];
247     }
248
249     /**
250      * Creates a new reflection instance for accessing the member's value.
251      *
252      * Must be implemented by subclasses.
253      *
254      * @param object|string $objectOrClassName The object or the class name
255      *
256      * @return \ReflectionMethod|\ReflectionProperty The reflection instance
257      */
258     abstract protected function newReflectionMember($objectOrClassName);
259 }