7858f74dbbe2939e12c7c6311146d1462909c8ab
[yaffs-website] / serializer / 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\Serializer\Mapping;
13
14 /**
15  * {@inheritdoc}
16  *
17  * @author Kévin Dunglas <dunglas@gmail.com>
18  */
19 class ClassMetadata implements ClassMetadataInterface
20 {
21     /**
22      * @internal This property is public in order to reduce the size of the
23      *           class' serialized representation. Do not access it. Use
24      *           {@link getName()} instead.
25      */
26     public $name;
27
28     /**
29      * @var AttributeMetadataInterface[]
30      *
31      * @internal This property is public in order to reduce the size of the
32      *           class' serialized representation. Do not access it. Use
33      *           {@link getAttributesMetadata()} instead.
34      */
35     public $attributesMetadata = array();
36
37     /**
38      * @var \ReflectionClass
39      */
40     private $reflClass;
41
42     /**
43      * Constructs a metadata for the given class.
44      *
45      * @param string $class
46      */
47     public function __construct($class)
48     {
49         $this->name = $class;
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function getName()
56     {
57         return $this->name;
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function addAttributeMetadata(AttributeMetadataInterface $attributeMetadata)
64     {
65         $this->attributesMetadata[$attributeMetadata->getName()] = $attributeMetadata;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function getAttributesMetadata()
72     {
73         return $this->attributesMetadata;
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function merge(ClassMetadataInterface $classMetadata)
80     {
81         foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
82             if (isset($this->attributesMetadata[$attributeMetadata->getName()])) {
83                 $this->attributesMetadata[$attributeMetadata->getName()]->merge($attributeMetadata);
84             } else {
85                 $this->addAttributeMetadata($attributeMetadata);
86             }
87         }
88     }
89
90     /**
91      * {@inheritdoc}
92      */
93     public function getReflectionClass()
94     {
95         if (!$this->reflClass) {
96             $this->reflClass = new \ReflectionClass($this->getName());
97         }
98
99         return $this->reflClass;
100     }
101
102     /**
103      * Returns the names of the properties that should be serialized.
104      *
105      * @return string[]
106      */
107     public function __sleep()
108     {
109         return array(
110             'name',
111             'attributesMetadata',
112         );
113     }
114 }