1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Annotation\Relationship as RelationshipAnnotation; |
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Definition; |
8
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Relationship; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Processor of relationships |
12
|
|
|
* |
13
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor |
14
|
|
|
*/ |
15
|
|
|
class RelationshipProcessor extends AbstractProcessor |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Process properties of class |
19
|
|
|
* |
20
|
|
|
* @param \ReflectionClass $reflection |
21
|
|
|
* @param Definition $definition |
22
|
|
|
*/ |
23
|
4 |
|
public function process(\ReflectionClass $reflection, Definition $definition) |
24
|
|
|
{ |
25
|
4 |
|
foreach ($reflection->getProperties() as $property) |
26
|
|
|
{ |
27
|
4 |
|
$this->processProperty($property, $definition); |
28
|
|
|
} |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Process property of class |
33
|
|
|
* |
34
|
|
|
* @param \ReflectionProperty $property |
35
|
|
|
* @param Definition $definition |
36
|
|
|
*/ |
37
|
4 |
|
protected function processProperty(\ReflectionProperty $property, Definition $definition) |
38
|
|
|
{ |
39
|
4 |
|
$annotations = $this->reader->getPropertyAnnotations($property); |
40
|
|
|
|
41
|
4 |
|
foreach ($annotations as $annotation) |
42
|
|
|
{ |
43
|
4 |
|
if ($annotation instanceof RelationshipAnnotation) { |
44
|
4 |
|
$relationship = $this->createRelationship($annotation, $property); |
45
|
|
|
|
46
|
4 |
|
$definition->addRelationship($relationship); |
47
|
|
|
} |
48
|
|
|
} |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Process relationship |
53
|
|
|
* |
54
|
|
|
* @param RelationshipAnnotation $annotation |
55
|
|
|
* @param \ReflectionProperty $property |
56
|
|
|
* @return Relationship |
57
|
|
|
*/ |
58
|
4 |
|
protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
59
|
|
|
{ |
60
|
4 |
|
$name = ($annotation->name === null) |
61
|
3 |
|
? $property->getName() |
62
|
4 |
|
: $annotation->name; |
63
|
|
|
|
64
|
4 |
|
$type = $this->resolveType($annotation); |
65
|
|
|
|
66
|
4 |
|
$getter = ($annotation->getter === null) |
67
|
4 |
|
? $this->resolveGetter($property) |
68
|
4 |
|
: $annotation->getter; |
69
|
|
|
|
70
|
4 |
|
$relationship = new Relationship($name, $type, $getter); |
71
|
4 |
|
$relationship->setPropertyName($property->getName()); |
72
|
|
|
|
73
|
4 |
|
$this->handleLinks($annotation, $relationship); |
74
|
4 |
|
$this->handleDataControl($annotation, $relationship); |
75
|
|
|
|
76
|
4 |
|
return $relationship; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Handle links |
81
|
|
|
* |
82
|
|
|
* @param RelationshipAnnotation $annotation |
83
|
|
|
* @param Relationship $relationship |
84
|
|
|
*/ |
85
|
4 |
|
protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
86
|
|
|
{ |
87
|
4 |
|
foreach ($annotation->links as $linkAnnotation) |
88
|
|
|
{ |
89
|
3 |
|
$link = $this->createLink($linkAnnotation); |
90
|
|
|
|
91
|
3 |
|
$relationship->addLink($link); |
92
|
|
|
} |
93
|
4 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Handle control of data-section |
97
|
|
|
* |
98
|
|
|
* @param RelationshipAnnotation $annotation |
99
|
|
|
* @param Relationship $relationship |
100
|
|
|
*/ |
101
|
4 |
|
protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
102
|
|
|
{ |
103
|
4 |
|
$relationship->setIncludeData($annotation->dataAllowed); |
104
|
4 |
|
$relationship->setDataLimit($annotation->dataLimit); |
105
|
4 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Resolve type of relationship |
109
|
|
|
* |
110
|
|
|
* @param RelationshipAnnotation $annotation |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
4 |
|
protected function resolveType(RelationshipAnnotation $annotation): int |
114
|
|
|
{ |
115
|
4 |
|
if ($annotation->type === RelationshipAnnotation::TYPE_ONE) { |
116
|
1 |
|
return Relationship::TYPE_X_TO_ONE; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
if ($annotation->type === RelationshipAnnotation::TYPE_MANY) { |
120
|
3 |
|
return Relationship::TYPE_X_TO_MANY; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new \LogicException(sprintf('Invalid type of relation "%s" defined.', $annotation->type)); |
124
|
|
|
} |
125
|
|
|
} |