1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Halapi\Relation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Annotation; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class LinksRelation |
10
|
|
|
* @author Romain Richard |
11
|
|
|
*/ |
12
|
|
|
class LinksRelation extends AbstractRelation implements RelationInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
public function getName() |
18
|
|
|
{ |
19
|
|
|
return '_links'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getRelation($resource) |
26
|
|
|
{ |
27
|
|
|
$reflectionClass = new \ReflectionClass($resource); |
28
|
|
|
$links = $this->getSelfLink($resource, $reflectionClass); |
29
|
|
|
|
30
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
31
|
|
|
if ($this->isEmbeddable($property) && $property->getName()) { |
32
|
|
|
$propertyName = $property->getName(); |
33
|
|
|
$relationContent = $resource->{'get'.ucfirst($propertyName)}(); |
34
|
|
|
$links[$propertyName] = $this->getRelationLinks($property, $relationContent); |
35
|
|
|
|
36
|
|
|
if (!$links[$propertyName]) { |
37
|
|
|
unset($links[$propertyName]); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $links; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \ReflectionProperty $property |
47
|
|
|
* @param object $relationContent |
48
|
|
|
* @return string|null |
49
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
50
|
|
|
*/ |
51
|
|
|
protected function getRelationLink($property, $relationContent) |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var $annotation Annotation |
55
|
|
|
*/ |
56
|
|
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) { |
57
|
|
|
if (isset($annotation->targetEntity)) { |
58
|
|
|
$shortName = strtolower((new \ReflectionClass($annotation->targetEntity))->getShortName()); |
59
|
|
|
return $this->urlGenerator->generate( |
60
|
|
|
'get_'.$shortName, |
61
|
|
|
[$shortName => $this->getEntityId($relationContent)] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the url of an entity based on the 'get_entity' route pattern. |
71
|
|
|
* |
72
|
|
|
* @param $resource |
73
|
|
|
* @param \ReflectionClass $reflectionClass |
74
|
|
|
* |
75
|
|
|
* @return array|null |
76
|
|
|
*/ |
77
|
|
|
private function getSelfLink($resource, $reflectionClass) |
78
|
|
|
{ |
79
|
|
|
if ($resource instanceof \Traversable) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return [ |
84
|
|
|
'self' => $this->urlGenerator->generate( |
85
|
|
|
'get_'.strtolower($reflectionClass->getShortName()), |
86
|
|
|
[strtolower($reflectionClass->getShortName()) => $this->getEntityId($resource)] |
87
|
|
|
), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the links of a collection. |
93
|
|
|
* |
94
|
|
|
* @param \ReflectionProperty $property |
95
|
|
|
* @param $relationContent |
96
|
|
|
* |
97
|
|
|
* @return array|void |
98
|
|
|
*/ |
99
|
|
|
private function getRelationLinks($property, $relationContent) |
100
|
|
|
{ |
101
|
|
|
if ($relationContent instanceof Collection) { |
102
|
|
|
$links = []; |
103
|
|
|
foreach ($relationContent as $relation) { |
104
|
|
|
$links[] = $this->getRelationLink($property, $relation); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $links; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->getRelationLink($property, $relationContent); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns entity single identifier. |
115
|
|
|
* This is a compatibility-limiting feature as it will not be able to get the identity |
116
|
|
|
* of an entity which has multiple identifiers. |
117
|
|
|
* @param $entity |
118
|
|
|
*/ |
119
|
|
|
private function getEntityId($entity) |
120
|
|
|
{ |
121
|
|
|
$meta = $this->entityManager->getClassMetadata(get_class($entity)); |
122
|
|
|
$identifier = $meta->getIdentifier()[0]; |
123
|
|
|
$getter = 'get'.ucfirst($identifier); |
124
|
|
|
|
125
|
|
|
return $entity->$getter(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|