1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) Sergey Revenko <[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
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Decoders\Mapping\Loader; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
15
|
|
|
use Reva2\JsonApi\Annotations\Attribute; |
16
|
|
|
use Reva2\JsonApi\Annotations\Document as ApiDocument; |
17
|
|
|
use Reva2\JsonApi\Annotations\Property; |
18
|
|
|
use Reva2\JsonApi\Annotations\Relationship; |
19
|
|
|
use Reva2\JsonApi\Annotations\Resource as ApiResource; |
20
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ClassMetadataInterface; |
21
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface; |
22
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ObjectMetadataInterface; |
23
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface; |
24
|
|
|
use Reva2\JsonApi\Decoders\Mapping\DocumentMetadata; |
25
|
|
|
use Reva2\JsonApi\Decoders\Mapping\ObjectMetadata; |
26
|
|
|
use Reva2\JsonApi\Decoders\Mapping\PropertyMetadata; |
27
|
|
|
use Reva2\JsonApi\Decoders\Mapping\ResourceMetadata; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Loads JSON API metadata using a Doctrine annotations |
31
|
|
|
* |
32
|
|
|
* @package Reva2\JsonApi\Decoders\Mapping\Loader |
33
|
|
|
* @author Sergey Revenko <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class AnnotationLoader implements LoaderInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Reader |
39
|
|
|
*/ |
40
|
|
|
protected $reader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param Reader $reader |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(Reader $reader) |
48
|
|
|
{ |
49
|
1 |
|
$this->reader = $reader; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
|
public function loadClassMetadata(\ReflectionClass $class) |
56
|
|
|
{ |
57
|
1 |
|
if (null !== ($resource = $this->reader->getClassAnnotation($class, ApiResource::class))) { |
58
|
|
|
return $this->loadResourceMetadata($resource, $class); |
59
|
1 |
|
} elseif (null !== ($document = $this->reader->getClassAnnotation($class, ApiDocument::class))) { |
60
|
|
|
return $this->loadDocumentMetadata($document, $class); |
61
|
|
|
} else { |
62
|
1 |
|
return $this->loadObjectMetadata($class); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Parse JSON API resource metadata |
68
|
|
|
* |
69
|
|
|
* @param ApiResource $resource |
70
|
|
|
* @param \ReflectionClass $class |
71
|
|
|
* @return ResourceMetadata |
72
|
|
|
*/ |
73
|
|
|
private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
74
|
|
|
{ |
75
|
|
|
$metadata = new ResourceMetadata($resource->name, $class->getName()); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$properties = $class->getProperties(); |
78
|
|
|
foreach ($properties as $property) { |
79
|
|
|
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
80
|
|
|
if ($annotation instanceof Attribute) { |
81
|
|
|
$metadata->addAttribute($this->loadPropertyMetadata($annotation, $property)); |
82
|
|
|
} elseif ($annotation instanceof Relationship) { |
83
|
|
|
$metadata->addRelationship($this->loadPropertyMetadata($annotation, $property)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->loadParentMetadata($metadata, $class); |
89
|
|
|
|
90
|
|
|
return $metadata; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \ReflectionClass $class |
95
|
|
|
* @return ObjectMetadata |
96
|
|
|
*/ |
97
|
1 |
|
private function loadObjectMetadata(\ReflectionClass $class) |
98
|
|
|
{ |
99
|
1 |
|
$metadata = new ObjectMetadata($class->name); |
100
|
|
|
|
101
|
1 |
|
$properties = $class->getProperties(); |
102
|
1 |
|
foreach ($properties as $property) { |
103
|
1 |
|
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
104
|
1 |
|
if ($annotation instanceof Property) { |
105
|
1 |
|
$metadata->addProperty($this->loadPropertyMetadata($annotation, $property)); |
106
|
1 |
|
} |
107
|
1 |
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
|
$this->loadParentMetadata($metadata, $class); |
111
|
|
|
|
112
|
1 |
|
return $metadata; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Parse JSON API document metadata |
117
|
|
|
* |
118
|
|
|
* @param ApiDocument $document |
119
|
|
|
* @param \ReflectionClass $class |
120
|
|
|
* @return DocumentMetadata |
121
|
|
|
*/ |
122
|
|
|
private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$metadata = new DocumentMetadata($class->getName()); |
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
return $metadata; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Parse property metadata |
133
|
|
|
* |
134
|
|
|
* @param Property $annotation |
135
|
|
|
* @param \ReflectionProperty $property |
136
|
|
|
* @return PropertyMetadata |
137
|
|
|
*/ |
138
|
1 |
|
private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property) |
139
|
|
|
{ |
140
|
1 |
|
$metadata = new PropertyMetadata($property->name, $property->class); |
141
|
|
|
|
142
|
1 |
|
list($dataType, $dataTypeParams) = $this->parseDataType($annotation, $property); |
143
|
|
|
$metadata |
144
|
1 |
|
->setDataType($dataType) |
145
|
1 |
|
->setDataTypeParams($dataTypeParams); |
146
|
|
|
|
147
|
1 |
|
if ($annotation->setter) { |
148
|
|
|
$metadata->setSetter($annotation->setter); |
149
|
1 |
|
} elseif (false === $property->isPublic()) { |
150
|
1 |
|
$setter = 'set' . ucfirst($property->name); |
151
|
1 |
|
if (false == $property->getDeclaringClass()->hasMethod($setter)) { |
|
|
|
|
152
|
|
|
throw new \RuntimeException(sprintf( |
153
|
|
|
"Couldn't find setter for non public property: %s:%s", |
154
|
|
|
$property->class, |
155
|
|
|
$property->name |
156
|
|
|
)); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$metadata->setSetter($setter); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
1 |
|
return $metadata; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Parse property data type |
167
|
|
|
* |
168
|
|
|
* @param Property $annotation |
169
|
|
|
* @param \ReflectionProperty $property |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
1 |
|
private function parseDataType(Property $annotation, \ReflectionProperty $property) |
173
|
|
|
{ |
174
|
1 |
|
if (!empty($annotation->parser)) { |
175
|
1 |
|
if (!$property->getDeclaringClass()->hasMethod($annotation->parser)) { |
176
|
|
|
throw new \InvalidArgumentException(sprintf( |
177
|
|
|
"Custom parser function %s:%s() for property '%s' does not exist", |
178
|
|
|
$property->class, |
179
|
|
|
$annotation->parser, |
180
|
|
|
$property->name |
181
|
|
|
)); |
182
|
|
|
} |
183
|
1 |
|
return ['custom', $annotation->parser]; |
184
|
1 |
|
} elseif (!empty($annotation->type)) { |
185
|
1 |
|
return $this->parseDataTypeString($annotation->type); |
186
|
1 |
|
} elseif (preg_match('~@var\s(.*?)\s~si', $property->getDocComment(), $matches)) { |
187
|
1 |
|
return $this->parseDataTypeString($matches[1]); |
188
|
|
|
} else { |
189
|
1 |
|
return ['raw', null]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Parse data type string |
195
|
|
|
* |
196
|
|
|
* @param string $type |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
1 |
|
private function parseDataTypeString($type) |
200
|
|
|
{ |
201
|
1 |
|
$params = null; |
202
|
|
|
|
203
|
1 |
|
if ($this->isScalarDataType($type)) { |
204
|
1 |
|
$dataType = 'scalar'; |
205
|
1 |
|
$params = $type; |
206
|
1 |
|
} elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) { |
207
|
1 |
|
$dataType = 'datetime'; |
208
|
1 |
|
if (3 === count($matches)) { |
209
|
1 |
|
$params = $matches[2]; |
210
|
1 |
|
} |
211
|
1 |
|
} elseif ( |
212
|
1 |
|
(preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) || |
213
|
1 |
|
(preg_match('~^(.*?)\[\]$~si', $type, $matches)) |
214
|
1 |
|
) { |
215
|
1 |
|
$dataType = 'array'; |
216
|
1 |
|
if (3 === count($matches)) { |
217
|
1 |
|
$params = $this->parseDataTypeString($matches[2]); |
218
|
1 |
|
} elseif (2 === count($matches)) { |
219
|
1 |
|
$params = $this->parseDataTypeString($matches[1]); |
220
|
1 |
|
} else { |
221
|
1 |
|
$params = ['raw', null]; |
222
|
|
|
} |
223
|
1 |
|
} else { |
224
|
1 |
|
$type = ltrim($type, '\\'); |
225
|
|
|
|
226
|
1 |
|
if (!class_exists($type) && !interface_exists($type)) { |
227
|
|
|
throw new \InvalidArgumentException(sprintf( |
228
|
|
|
"Unknown object type '%s' specified", |
229
|
|
|
$type |
230
|
|
|
)); |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
$dataType = 'object'; |
234
|
1 |
|
$params = $type; |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
return [$dataType, $params]; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns true if specified type scalar. False otherwise. |
242
|
|
|
* |
243
|
|
|
* @param string $type |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
1 |
|
private function isScalarDataType($type) |
247
|
|
|
{ |
248
|
1 |
|
return in_array($type, ['string', 'bool', 'boolean', 'int', 'integer', 'float', 'double']); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Load and merge metadata from parent class |
253
|
|
|
* |
254
|
|
|
* @param ClassMetadataInterface $metadata |
255
|
|
|
* @param \ReflectionClass $class |
256
|
|
|
*/ |
257
|
1 |
|
private function loadParentMetadata(ClassMetadataInterface $metadata, \ReflectionClass $class) |
258
|
|
|
{ |
259
|
1 |
|
$parentClass = $class->getParentClass(); |
260
|
1 |
|
if (false === $parentClass) { |
261
|
1 |
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
$parentMetadata = $this->loadClassMetadata($parentClass); |
265
|
|
|
|
266
|
1 |
|
$metadata->mergeMetadata($parentMetadata); |
267
|
|
|
} |
268
|
|
|
} |