1 | <?php |
||
17 | class LinksRelation extends AbstractRelation implements RelationInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var UrlGeneratorInterface |
||
21 | */ |
||
22 | private $urlGenerator; |
||
23 | |||
24 | /** |
||
25 | * @var ObjectManager |
||
26 | */ |
||
27 | private $objectManager; |
||
28 | |||
29 | /** |
||
30 | * @var ClassMetadata |
||
31 | */ |
||
32 | private $classMetadata; |
||
33 | |||
34 | /** |
||
35 | * @var \ReflectionClass |
||
36 | */ |
||
37 | private $reflectionClass; |
||
38 | |||
39 | /** |
||
40 | * AbstractRelation constructor. |
||
41 | * |
||
42 | * @param Reader $annotationReader |
||
43 | * @param UrlGeneratorInterface $urlGenerator |
||
44 | * @param ObjectManager $objectManager |
||
45 | */ |
||
46 | public function __construct( |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function getName() |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function getRelation($resource) |
||
85 | |||
86 | /** |
||
87 | * @param \ReflectionProperty $property |
||
88 | * @param object $relationContent |
||
89 | * |
||
90 | * @return string|null |
||
91 | * |
||
92 | * @throws \Doctrine\ORM\Mapping\MappingException |
||
93 | */ |
||
94 | private function getRelationLink(\ReflectionProperty $property, $relationContent) |
||
95 | { |
||
96 | if ($this->classMetadata->hasAssociation($property->getName())) { |
||
97 | $identifier = $this->getIdentifier($relationContent); |
||
98 | |||
99 | return $this->urlGenerator->generate( |
||
100 | $this->getAssociationRouteName($property), |
||
101 | [$identifier => $this->getId($relationContent, $identifier)] |
||
102 | ); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Get the url of an entity based on the 'get_entity' route pattern. |
||
108 | * |
||
109 | * @param $resource |
||
110 | * |
||
111 | * @return array|null |
||
112 | */ |
||
113 | private function getSelfLink($resource) |
||
114 | { |
||
115 | if ($resource instanceof \Traversable) { |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | $identifier = $this->getIdentifier($resource); |
||
120 | |||
121 | return [ |
||
122 | 'self' => $this->urlGenerator->generate( |
||
123 | $this->getResourceRouteName($this->reflectionClass), |
||
124 | [$identifier => $this->getId($resource, $identifier)] |
||
125 | ), |
||
126 | ]; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get the links of a collection. |
||
131 | * |
||
132 | * @param \ReflectionProperty $property |
||
133 | * @param $relationContent |
||
134 | * |
||
135 | * @return array|void |
||
136 | */ |
||
137 | private function getRelationLinks(\ReflectionProperty $property, $relationContent) |
||
150 | |||
151 | /** |
||
152 | * Return the configured route name for an embeddable relation. |
||
153 | * |
||
154 | * @param \ReflectionProperty $property |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | private function getAssociationRouteName(\ReflectionProperty $property) |
||
168 | |||
169 | /** |
||
170 | * Return the configured route name for a resource, or get_*entityShortName* by default. |
||
171 | * |
||
172 | * @param \ReflectionClass $resource |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | private function getResourceRouteName(\ReflectionClass $resource) |
||
184 | |||
185 | /** |
||
186 | * @param $resource |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | private function getIdentifier($resource) |
||
196 | |||
197 | private function getId($resource, $identifier) |
||
212 | } |
||
213 |