Skip to content

Commit c9e6813

Browse files
committed
Add relation links and meta to JSON API array
1 parent e251ad7 commit c9e6813

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

src/Item.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,36 @@ public function getRelationships(): array
130130
$relationships = [];
131131

132132
foreach ($this->getRelations() as $name => $relation) {
133-
if (!$relation->hasIncluded()) {
134-
continue;
133+
if ($relation->hasIncluded()) {
134+
if ($relation instanceof OneRelationInterface) {
135+
$relationships[$name]['data'] = null;
136+
137+
if ($relation->getIncluded() !== null) {
138+
$relationships[$name]['data'] = [
139+
'type' => $relation->getIncluded()->getType(),
140+
'id' => $relation->getIncluded()->getId(),
141+
];
142+
}
143+
} elseif ($relation instanceof ManyRelationInterface) {
144+
$relationships[$name]['data'] = [];
145+
146+
foreach ($relation->getIncluded() as $item) {
147+
$relationships[$name]['data'][] = [
148+
'type' => $item->getType(),
149+
'id' => $item->getId(),
150+
];
151+
}
152+
}
135153
}
136154

137-
if ($relation instanceof OneRelationInterface) {
138-
$relationships[$name]['data'] = null;
155+
$links = $relation->getLinks();
156+
if ($links !== null) {
157+
$relationships[$name]['links'] = $links->toArray();
158+
}
139159

140-
if ($relation->getIncluded() !== null) {
141-
$relationships[$name]['data'] = [
142-
'type' => $relation->getIncluded()->getType(),
143-
'id' => $relation->getIncluded()->getId(),
144-
];
145-
}
146-
} elseif ($relation instanceof ManyRelationInterface) {
147-
$relationships[$name]['data'] = [];
148-
149-
foreach ($relation->getIncluded() as $item) {
150-
$relationships[$name]['data'][] = [
151-
'type' => $item->getType(),
152-
'id' => $item->getId(),
153-
];
154-
}
160+
$meta = $relation->getMeta();
161+
if ($meta !== null) {
162+
$relationships[$name]['meta'] = $meta->toArray();
155163
}
156164
}
157165

tests/ItemTest.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function is_adds_hasone_relation_in_to_json_api_array()
7878
$item = new WithRelationshipItem();
7979
$item->setId('1234');
8080
$item->hasoneRelation()->associate((new RelatedItem())->setId('5678'));
81+
$item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
82+
$item->hasoneRelation()->setMeta(new Meta(['foo' => 'bar']));
8183

8284
$this->assertSame(
8385
[
@@ -89,6 +91,14 @@ public function is_adds_hasone_relation_in_to_json_api_array()
8991
'type' => 'related-item',
9092
'id' => '5678',
9193
],
94+
'links' => [
95+
'self' => [
96+
'href' => 'http://example.com/articles',
97+
],
98+
],
99+
'meta' => [
100+
'foo' => 'bar',
101+
],
92102
],
93103
],
94104
],
@@ -122,7 +132,7 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array()
122132
/**
123133
* @test
124134
*/
125-
public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array()
135+
public function is_does_not_add_hasone_relation_without_data_links_and_meta_in_to_json_api_array()
126136
{
127137
$item = new WithRelationshipItem();
128138
$item->setId('1234');
@@ -145,6 +155,8 @@ public function is_adds_hasmany_relation_in_to_json_api_array()
145155
$item = new WithRelationshipItem();
146156
$item->setId('1234');
147157
$item->hasmanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')]));
158+
$item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
159+
$item->hasmanyRelation()->setMeta(new Meta(['foo' => 'bar']));
148160

149161
$this->assertSame(
150162
[
@@ -158,6 +170,14 @@ public function is_adds_hasmany_relation_in_to_json_api_array()
158170
'id' => '5678',
159171
],
160172
],
173+
'links' => [
174+
'self' => [
175+
'href' => 'http://example.com/articles',
176+
],
177+
],
178+
'meta' => [
179+
'foo' => 'bar',
180+
],
161181
],
162182
],
163183
],
@@ -191,7 +211,7 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array()
191211
/**
192212
* @test
193213
*/
194-
public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array()
214+
public function is_does_not_add_hasmany_relation_without_data_links_and_meta_in_to_json_api_array()
195215
{
196216
$item = new WithRelationshipItem();
197217
$item->setId('1234');
@@ -214,6 +234,8 @@ public function is_adds_morphto_relation_in_to_json_api_array()
214234
$item = new WithRelationshipItem();
215235
$item->setId('1234');
216236
$item->morphtoRelation()->associate((new RelatedItem())->setId('5678'));
237+
$item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
238+
$item->morphtoRelation()->setMeta(new Meta(['foo' => 'bar']));
217239

218240
$this->assertSame(
219241
[
@@ -225,6 +247,14 @@ public function is_adds_morphto_relation_in_to_json_api_array()
225247
'type' => 'related-item',
226248
'id' => '5678',
227249
],
250+
'links' => [
251+
'self' => [
252+
'href' => 'http://example.com/articles',
253+
],
254+
],
255+
'meta' => [
256+
'foo' => 'bar',
257+
],
228258
],
229259
],
230260
],
@@ -258,7 +288,7 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array()
258288
/**
259289
* @test
260290
*/
261-
public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array()
291+
public function is_does_not_add_morphto_relation_without_data_links_and_meta_in_to_json_api_array()
262292
{
263293
$item = new WithRelationshipItem();
264294
$item->setId('1234');
@@ -281,6 +311,8 @@ public function is_adds_morphtomany_relation_in_to_json_api_array()
281311
$item = new WithRelationshipItem();
282312
$item->setId('1234');
283313
$item->morphtomanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')]));
314+
$item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
315+
$item->morphtomanyRelation()->setMeta(new Meta(['foo' => 'bar']));
284316

285317
$this->assertSame(
286318
[
@@ -294,6 +326,14 @@ public function is_adds_morphtomany_relation_in_to_json_api_array()
294326
'id' => '5678',
295327
],
296328
],
329+
'links' => [
330+
'self' => [
331+
'href' => 'http://example.com/articles',
332+
],
333+
],
334+
'meta' => [
335+
'foo' => 'bar',
336+
],
297337
],
298338
],
299339
],
@@ -327,7 +367,7 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array()
327367
/**
328368
* @test
329369
*/
330-
public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array()
370+
public function is_does_not_add_morphtomany_relation_without_data_links_and_meta_in_to_json_api_array()
331371
{
332372
$item = new WithRelationshipItem();
333373
$item->setId('1234');

0 commit comments

Comments
 (0)