|
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\Loader; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
14
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Attribute; |
|
15
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Relationship; |
|
16
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\AttributeMetadata; |
|
17
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\ClassMetadataInterface; |
|
18
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\ClassAnnotationInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AnnotationLoader |
|
22
|
|
|
* |
|
23
|
|
|
* @package RonteLtd\JsonApiBundle\Serializer\Mapping\Loader |
|
24
|
|
|
* @author Ruslan Muriev <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class AnnotationLoader implements LoaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Reader |
|
30
|
|
|
*/ |
|
31
|
|
|
private $reader; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Reader $reader |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Reader $reader) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->reader = $reader; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
|
45
|
|
|
{ |
|
46
|
|
|
$reflectionClass = $classMetadata->getReflectionClass(); |
|
47
|
|
|
$className = $reflectionClass->name; |
|
48
|
|
|
$loaded = false; |
|
49
|
|
|
|
|
50
|
|
|
// Load class annotations |
|
51
|
|
|
foreach ($this->reader->getClassAnnotations($reflectionClass) as $classAnnotation) { |
|
52
|
|
|
if ($classAnnotation instanceof ClassAnnotationInterface) { |
|
53
|
|
|
if (null === $classAnnotation->getName()) { |
|
54
|
|
|
$classAnnotation->setName($reflectionClass->getShortName()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$classMetadata->addClassAnnotation($classAnnotation); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Load class attributes annotations |
|
62
|
|
|
$attributesMetadata = $classMetadata->getAttributesMetadata(); |
|
63
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
|
64
|
|
|
if (!isset($attributesMetadata[$property->name])) { |
|
65
|
|
|
$attributesMetadata[$property->name] = new AttributeMetadata($property->name); |
|
66
|
|
|
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($property->getDeclaringClass()->name === $className) { |
|
70
|
|
|
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
71
|
|
|
if ($annotation instanceof Attribute) { |
|
72
|
|
|
$attributesMetadata[$property->name]->setAttribute($annotation); |
|
|
|
|
|
|
73
|
|
|
} elseif ($annotation instanceof Relationship) { |
|
74
|
|
|
$attributesMetadata[$property->name]->setRelationship($annotation); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$loaded = true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $loaded; |
|
83
|
|
|
} |
|
84
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: