1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of AppBundle the package. |
4
|
|
|
* |
5
|
|
|
* (c) Ruslan Muriev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace RonteLtd\JsonApiBundle\Serializer\Mapping; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ClassMetadata |
15
|
|
|
* |
16
|
|
|
* @package RonteLtd\JsonApiBundle\Serializer\Mapping |
17
|
|
|
* @author Ruslan Muriev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ClassMetadata implements ClassMetadataInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
* |
24
|
|
|
* @internal This property is public in order to reduce the size of the |
25
|
|
|
* class' serialized representation. Do not access it. Use |
26
|
|
|
* {@link getName()} instead. |
27
|
|
|
*/ |
28
|
|
|
public $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AttributeMetadataInterface[] |
32
|
|
|
* |
33
|
|
|
* @internal This property is public in order to reduce the size of the |
34
|
|
|
* class' serialized representation. Do not access it. Use |
35
|
|
|
* {@link getAttributesMetadata()} instead. |
36
|
|
|
*/ |
37
|
|
|
public $attributesMetadata = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \ReflectionClass |
41
|
|
|
*/ |
42
|
|
|
private $reflClass; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructs a metadata for the given class. |
46
|
|
|
* |
47
|
|
|
* @param string $class |
48
|
|
|
*/ |
49
|
|
|
public function __construct($class) |
50
|
|
|
{ |
51
|
|
|
$this->name = $class; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getName() |
58
|
|
|
{ |
59
|
|
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function addAttributeMetadata(AttributeMetadataInterface $attributeMetadata) |
66
|
|
|
{ |
67
|
|
|
$this->attributesMetadata[$attributeMetadata->getName()] = $attributeMetadata; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getAttributesMetadata() |
74
|
|
|
{ |
75
|
|
|
return $this->attributesMetadata; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private $classAnnotations = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ClassAnnotationInterface $classAnnotation |
82
|
|
|
*/ |
83
|
|
|
public function addClassAnnotation(ClassAnnotationInterface $classAnnotation) |
84
|
|
|
{ |
85
|
|
|
$annotationClass = new \ReflectionClass($classAnnotation); |
86
|
|
|
|
87
|
|
|
$this->classAnnotations[$annotationClass->getShortName()] = $classAnnotation; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getClassAnnotations() |
94
|
|
|
{ |
95
|
|
|
return $this->classAnnotations; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function merge(ClassMetadataInterface $classMetadata) |
102
|
|
|
{ |
103
|
|
|
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) { |
104
|
|
|
if (isset($this->attributesMetadata[$attributeMetadata->getName()])) { |
105
|
|
|
$this->attributesMetadata[$attributeMetadata->getName()] = $attributeMetadata; |
106
|
|
|
} else { |
107
|
|
|
$this->addAttributeMetadata($attributeMetadata); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($classMetadata->getClassAnnotations() as $classAnnotation) { |
112
|
|
|
if (isset($this->classAnnotations[$classAnnotation->getName()])) { |
113
|
|
|
$this->classAnnotations[$classAnnotation->getName()] = $classAnnotation; |
114
|
|
|
} else { |
115
|
|
|
$this->addClassAnnotation($classAnnotation); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getReflectionClass() |
124
|
|
|
{ |
125
|
|
|
if (!$this->reflClass) { |
126
|
|
|
$this->reflClass = new \ReflectionClass($this->getName()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->reflClass; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the names of the properties that should be serialized. |
134
|
|
|
* |
135
|
|
|
* @return string[] |
136
|
|
|
*/ |
137
|
|
|
public function __sleep() |
138
|
|
|
{ |
139
|
|
|
return array( |
140
|
|
|
'name', |
141
|
|
|
'classAnnotations', |
142
|
|
|
'attributesMetadata', |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |