1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
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 Cubiche\Core\Metadata; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap; |
14
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ClassMetadata class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ClassMetadata implements \Serializable, ClassMetadataInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $className; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \ReflectionClass |
30
|
|
|
*/ |
31
|
|
|
protected $reflection; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArrayHashMapInterface |
35
|
|
|
*/ |
36
|
|
|
protected $methodsMetadata; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ArrayHashMapInterface |
40
|
|
|
*/ |
41
|
|
|
protected $propertiesMetadata; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ArrayHashMapInterface |
45
|
|
|
*/ |
46
|
|
|
protected $metadata; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ClassMetadata constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $className |
52
|
|
|
*/ |
53
|
|
|
public function __construct($className) |
54
|
|
|
{ |
55
|
|
|
$this->className = $className; |
56
|
|
|
|
57
|
|
|
$this->reflection = new \ReflectionClass($className); |
58
|
|
|
$this->methodsMetadata = new ArrayHashMap(); |
|
|
|
|
59
|
|
|
$this->propertiesMetadata = new ArrayHashMap(); |
|
|
|
|
60
|
|
|
$this->metadata = new ArrayHashMap(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function className() |
67
|
|
|
{ |
68
|
|
|
return $this->className; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \ReflectionClass |
73
|
|
|
*/ |
74
|
|
|
public function reflection() |
75
|
|
|
{ |
76
|
|
|
return $this->reflection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function methodsMetadata() |
83
|
|
|
{ |
84
|
|
|
return $this->methodsMetadata->toArray(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param MethodMetadata $metadata |
89
|
|
|
*/ |
90
|
|
|
public function addMethodMetadata(MethodMetadata $metadata) |
91
|
|
|
{ |
92
|
|
|
$this->methodsMetadata->set($metadata->methodName(), $metadata); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $methodName |
97
|
|
|
* |
98
|
|
|
* @return MethodMetadata|null |
99
|
|
|
*/ |
100
|
|
|
public function methodMetadata($methodName) |
101
|
|
|
{ |
102
|
|
|
return $this->methodsMetadata->get($methodName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function propertiesMetadata() |
109
|
|
|
{ |
110
|
|
|
return $this->propertiesMetadata->toArray(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param PropertyMetadata $metadata |
115
|
|
|
*/ |
116
|
|
|
public function addPropertyMetadata(PropertyMetadata $metadata) |
117
|
|
|
{ |
118
|
|
|
$this->propertiesMetadata->set($metadata->propertyName(), $metadata); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $propertyName |
123
|
|
|
* |
124
|
|
|
* @return PropertyMetadata|null |
125
|
|
|
*/ |
126
|
|
|
public function propertyMetadata($propertyName) |
127
|
|
|
{ |
128
|
|
|
return $this->propertiesMetadata->get($propertyName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function metadata() |
135
|
|
|
{ |
136
|
|
|
return $this->metadata->toArray(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $key |
141
|
|
|
* @param mixed $value |
142
|
|
|
*/ |
143
|
|
|
public function addMetadata($key, $value) |
144
|
|
|
{ |
145
|
|
|
$this->metadata->set($key, $value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $key |
150
|
|
|
* |
151
|
|
|
* @return mixed|null |
152
|
|
|
*/ |
153
|
|
|
public function getMetadata($key) |
154
|
|
|
{ |
155
|
|
|
return $this->metadata->get($key); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param ClassMetadataInterface $object |
160
|
|
|
* |
161
|
|
|
* @return ClassMetadataInterface |
162
|
|
|
*/ |
163
|
|
|
public function merge(ClassMetadataInterface $object) |
164
|
|
|
{ |
165
|
|
|
$this->className = $object->className(); |
166
|
|
|
$this->reflection = $object->reflection(); |
167
|
|
|
|
168
|
|
|
foreach ($object->methodsMetadata() as $methodName => $metadata) { |
169
|
|
|
$this->addMethodMetadata($metadata); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
foreach ($object->propertiesMetadata() as $propertyName => $metadata) { |
173
|
|
|
$this->addPropertyMetadata($metadata); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($object->metadata() as $key => $value) { |
177
|
|
|
$this->addMetadata($key, $value); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function serialize() |
185
|
|
|
{ |
186
|
|
|
return serialize(array( |
187
|
|
|
$this->className, |
188
|
|
|
$this->methodsMetadata->toArray(), |
189
|
|
|
$this->propertiesMetadata->toArray(), |
190
|
|
|
$this->metadata->toArray(), |
191
|
|
|
)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function unserialize($str) |
198
|
|
|
{ |
199
|
|
|
list( |
200
|
|
|
$this->className, |
201
|
|
|
$methodsMetadata, |
202
|
|
|
$propertiesMetadata, |
203
|
|
|
$metadata |
204
|
|
|
) = unserialize($str); |
205
|
|
|
|
206
|
|
|
$this->reflection = new \ReflectionClass($this->className); |
207
|
|
|
$this->methodsMetadata = new ArrayHashMap($methodsMetadata); |
|
|
|
|
208
|
|
|
$this->propertiesMetadata = new ArrayHashMap($propertiesMetadata); |
|
|
|
|
209
|
|
|
$this->metadata = new ArrayHashMap($metadata); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..