c4e5b11b87cbd614332a6fba942e288eb2d26d55
[yaffs-website] / vendor / symfony / validator / Mapping / ClassMetadata.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\Constraints\GroupSequence;
16 use Symfony\Component\Validator\Constraints\Traverse;
17 use Symfony\Component\Validator\Constraints\Valid;
18 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
19 use Symfony\Component\Validator\Exception\GroupDefinitionException;
20
21 /**
22  * Default implementation of {@link ClassMetadataInterface}.
23  *
24  * This class supports serialization and cloning.
25  *
26  * @author Bernhard Schussek <bschussek@gmail.com>
27  * @author Fabien Potencier <fabien@symfony.com>
28  */
29 class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
30 {
31     /**
32      * @var string
33      *
34      * @internal This property is public in order to reduce the size of the
35      *           class' serialized representation. Do not access it. Use
36      *           {@link getClassName()} instead.
37      */
38     public $name;
39
40     /**
41      * @var string
42      *
43      * @internal This property is public in order to reduce the size of the
44      *           class' serialized representation. Do not access it. Use
45      *           {@link getDefaultGroup()} instead.
46      */
47     public $defaultGroup;
48
49     /**
50      * @var MemberMetadata[][]
51      *
52      * @internal This property is public in order to reduce the size of the
53      *           class' serialized representation. Do not access it. Use
54      *           {@link getPropertyMetadata()} instead.
55      */
56     public $members = array();
57
58     /**
59      * @var PropertyMetadata[]
60      *
61      * @internal This property is public in order to reduce the size of the
62      *           class' serialized representation. Do not access it. Use
63      *           {@link getPropertyMetadata()} instead.
64      */
65     public $properties = array();
66
67     /**
68      * @var GetterMetadata[]
69      *
70      * @internal This property is public in order to reduce the size of the
71      *           class' serialized representation. Do not access it. Use
72      *           {@link getPropertyMetadata()} instead.
73      */
74     public $getters = array();
75
76     /**
77      * @var array
78      *
79      * @internal This property is public in order to reduce the size of the
80      *           class' serialized representation. Do not access it. Use
81      *           {@link getGroupSequence()} instead.
82      */
83     public $groupSequence = array();
84
85     /**
86      * @var bool
87      *
88      * @internal This property is public in order to reduce the size of the
89      *           class' serialized representation. Do not access it. Use
90      *           {@link isGroupSequenceProvider()} instead.
91      */
92     public $groupSequenceProvider = false;
93
94     /**
95      * The strategy for traversing traversable objects.
96      *
97      * By default, only instances of {@link \Traversable} are traversed.
98      *
99      * @var int
100      *
101      * @internal This property is public in order to reduce the size of the
102      *           class' serialized representation. Do not access it. Use
103      *           {@link getTraversalStrategy()} instead.
104      */
105     public $traversalStrategy = TraversalStrategy::IMPLICIT;
106
107     /**
108      * @var \ReflectionClass
109      */
110     private $reflClass;
111
112     /**
113      * Constructs a metadata for the given class.
114      *
115      * @param string $class
116      */
117     public function __construct($class)
118     {
119         $this->name = $class;
120         // class name without namespace
121         if (false !== $nsSep = strrpos($class, '\\')) {
122             $this->defaultGroup = substr($class, $nsSep + 1);
123         } else {
124             $this->defaultGroup = $class;
125         }
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     public function __sleep()
132     {
133         $parentProperties = parent::__sleep();
134
135         // Don't store the cascading strategy. Classes never cascade.
136         unset($parentProperties[array_search('cascadingStrategy', $parentProperties)]);
137
138         return array_merge($parentProperties, array(
139             'getters',
140             'groupSequence',
141             'groupSequenceProvider',
142             'members',
143             'name',
144             'properties',
145             'defaultGroup',
146         ));
147     }
148
149     /**
150      * {@inheritdoc}
151      */
152     public function getClassName()
153     {
154         return $this->name;
155     }
156
157     /**
158      * Returns the name of the default group for this class.
159      *
160      * For each class, the group "Default" is an alias for the group
161      * "<ClassName>", where <ClassName> is the non-namespaced name of the
162      * class. All constraints implicitly or explicitly assigned to group
163      * "Default" belong to both of these groups, unless the class defines
164      * a group sequence.
165      *
166      * If a class defines a group sequence, validating the class in "Default"
167      * will validate the group sequence. The constraints assigned to "Default"
168      * can still be validated by validating the class in "<ClassName>".
169      *
170      * @return string The name of the default group
171      */
172     public function getDefaultGroup()
173     {
174         return $this->defaultGroup;
175     }
176
177     /**
178      * {@inheritdoc}
179      */
180     public function addConstraint(Constraint $constraint)
181     {
182         if (!\in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
183             throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', \get_class($constraint)));
184         }
185
186         if ($constraint instanceof Valid) {
187             throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', \get_class($constraint)));
188         }
189
190         if ($constraint instanceof Traverse) {
191             if ($constraint->traverse) {
192                 // If traverse is true, traversal should be explicitly enabled
193                 $this->traversalStrategy = TraversalStrategy::TRAVERSE;
194             } else {
195                 // If traverse is false, traversal should be explicitly disabled
196                 $this->traversalStrategy = TraversalStrategy::NONE;
197             }
198
199             // The constraint is not added
200             return $this;
201         }
202
203         $constraint->addImplicitGroupName($this->getDefaultGroup());
204
205         parent::addConstraint($constraint);
206
207         return $this;
208     }
209
210     /**
211      * Adds a constraint to the given property.
212      *
213      * @param string     $property   The name of the property
214      * @param Constraint $constraint The constraint
215      *
216      * @return $this
217      */
218     public function addPropertyConstraint($property, Constraint $constraint)
219     {
220         if (!isset($this->properties[$property])) {
221             $this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
222
223             $this->addPropertyMetadata($this->properties[$property]);
224         }
225
226         $constraint->addImplicitGroupName($this->getDefaultGroup());
227
228         $this->properties[$property]->addConstraint($constraint);
229
230         return $this;
231     }
232
233     /**
234      * @param string       $property
235      * @param Constraint[] $constraints
236      *
237      * @return $this
238      */
239     public function addPropertyConstraints($property, array $constraints)
240     {
241         foreach ($constraints as $constraint) {
242             $this->addPropertyConstraint($property, $constraint);
243         }
244
245         return $this;
246     }
247
248     /**
249      * Adds a constraint to the getter of the given property.
250      *
251      * The name of the getter is assumed to be the name of the property with an
252      * uppercased first letter and either the prefix "get" or "is".
253      *
254      * @param string     $property   The name of the property
255      * @param Constraint $constraint The constraint
256      *
257      * @return $this
258      */
259     public function addGetterConstraint($property, Constraint $constraint)
260     {
261         if (!isset($this->getters[$property])) {
262             $this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
263
264             $this->addPropertyMetadata($this->getters[$property]);
265         }
266
267         $constraint->addImplicitGroupName($this->getDefaultGroup());
268
269         $this->getters[$property]->addConstraint($constraint);
270
271         return $this;
272     }
273
274     /**
275      * Adds a constraint to the getter of the given property.
276      *
277      * @param string     $property   The name of the property
278      * @param string     $method     The name of the getter method
279      * @param Constraint $constraint The constraint
280      *
281      * @return $this
282      */
283     public function addGetterMethodConstraint($property, $method, Constraint $constraint)
284     {
285         if (!isset($this->getters[$property])) {
286             $this->getters[$property] = new GetterMetadata($this->getClassName(), $property, $method);
287
288             $this->addPropertyMetadata($this->getters[$property]);
289         }
290
291         $constraint->addImplicitGroupName($this->getDefaultGroup());
292
293         $this->getters[$property]->addConstraint($constraint);
294
295         return $this;
296     }
297
298     /**
299      * @param string       $property
300      * @param Constraint[] $constraints
301      *
302      * @return $this
303      */
304     public function addGetterConstraints($property, array $constraints)
305     {
306         foreach ($constraints as $constraint) {
307             $this->addGetterConstraint($property, $constraint);
308         }
309
310         return $this;
311     }
312
313     /**
314      * @param string       $property
315      * @param string       $method
316      * @param Constraint[] $constraints
317      *
318      * @return $this
319      */
320     public function addGetterMethodConstraints($property, $method, array $constraints)
321     {
322         foreach ($constraints as $constraint) {
323             $this->addGetterMethodConstraint($property, $method, $constraint);
324         }
325
326         return $this;
327     }
328
329     /**
330      * Merges the constraints of the given metadata into this object.
331      */
332     public function mergeConstraints(self $source)
333     {
334         if ($source->isGroupSequenceProvider()) {
335             $this->setGroupSequenceProvider(true);
336         }
337
338         foreach ($source->getConstraints() as $constraint) {
339             $this->addConstraint(clone $constraint);
340         }
341
342         foreach ($source->getConstrainedProperties() as $property) {
343             foreach ($source->getPropertyMetadata($property) as $member) {
344                 $member = clone $member;
345
346                 foreach ($member->getConstraints() as $constraint) {
347                     if (\in_array($constraint::DEFAULT_GROUP, $constraint->groups, true)) {
348                         $member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
349                     }
350
351                     $constraint->addImplicitGroupName($this->getDefaultGroup());
352                 }
353
354                 $this->addPropertyMetadata($member);
355
356                 if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
357                     $property = $member->getPropertyName();
358
359                     if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
360                         $this->properties[$property] = $member;
361                     } elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
362                         $this->getters[$property] = $member;
363                     }
364                 }
365             }
366         }
367     }
368
369     /**
370      * {@inheritdoc}
371      */
372     public function hasPropertyMetadata($property)
373     {
374         return array_key_exists($property, $this->members);
375     }
376
377     /**
378      * {@inheritdoc}
379      */
380     public function getPropertyMetadata($property)
381     {
382         if (!isset($this->members[$property])) {
383             return array();
384         }
385
386         return $this->members[$property];
387     }
388
389     /**
390      * {@inheritdoc}
391      */
392     public function getConstrainedProperties()
393     {
394         return array_keys($this->members);
395     }
396
397     /**
398      * Sets the default group sequence for this class.
399      *
400      * @param string[]|GroupSequence $groupSequence An array of group names
401      *
402      * @return $this
403      *
404      * @throws GroupDefinitionException
405      */
406     public function setGroupSequence($groupSequence)
407     {
408         if ($this->isGroupSequenceProvider()) {
409             throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
410         }
411
412         if (\is_array($groupSequence)) {
413             $groupSequence = new GroupSequence($groupSequence);
414         }
415
416         if (\in_array(Constraint::DEFAULT_GROUP, $groupSequence->groups, true)) {
417             throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
418         }
419
420         if (!\in_array($this->getDefaultGroup(), $groupSequence->groups, true)) {
421             throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
422         }
423
424         $this->groupSequence = $groupSequence;
425
426         return $this;
427     }
428
429     /**
430      * {@inheritdoc}
431      */
432     public function hasGroupSequence()
433     {
434         return $this->groupSequence && \count($this->groupSequence->groups) > 0;
435     }
436
437     /**
438      * {@inheritdoc}
439      */
440     public function getGroupSequence()
441     {
442         return $this->groupSequence;
443     }
444
445     /**
446      * Returns a ReflectionClass instance for this class.
447      *
448      * @return \ReflectionClass
449      */
450     public function getReflectionClass()
451     {
452         if (!$this->reflClass) {
453             $this->reflClass = new \ReflectionClass($this->getClassName());
454         }
455
456         return $this->reflClass;
457     }
458
459     /**
460      * Sets whether a group sequence provider should be used.
461      *
462      * @param bool $active
463      *
464      * @throws GroupDefinitionException
465      */
466     public function setGroupSequenceProvider($active)
467     {
468         if ($this->hasGroupSequence()) {
469             throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
470         }
471
472         if (!$this->getReflectionClass()->implementsInterface('Symfony\Component\Validator\GroupSequenceProviderInterface')) {
473             throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface', $this->name));
474         }
475
476         $this->groupSequenceProvider = $active;
477     }
478
479     /**
480      * {@inheritdoc}
481      */
482     public function isGroupSequenceProvider()
483     {
484         return $this->groupSequenceProvider;
485     }
486
487     /**
488      * Class nodes are never cascaded.
489      *
490      * {@inheritdoc}
491      */
492     public function getCascadingStrategy()
493     {
494         return CascadingStrategy::NONE;
495     }
496
497     private function addPropertyMetadata(PropertyMetadataInterface $metadata)
498     {
499         $property = $metadata->getPropertyName();
500
501         $this->members[$property][] = $metadata;
502     }
503 }