Skip to content

Commit 09ac236

Browse files
committed
Only add relation with data to JSON API array
1 parent 9af3cf8 commit 09ac236

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/Item.php

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

132132
foreach ($this->getRelations() as $name => $relation) {
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-
}
133+
if (!$relation->hasIncluded()) {
134+
continue;
135+
}
136+
137+
if ($relation instanceof OneRelationInterface) {
138+
$relationships[$name]['data'] = null;
139+
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+
];
152154
}
153155
}
154156

tests/ItemTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array()
132132
/**
133133
* @test
134134
*/
135-
public function is_does_not_add_hasone_relation_without_data_links_and_meta_in_to_json_api_array()
135+
public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array()
136136
{
137137
$item = new WithRelationshipItem();
138138
$item->setId('1234');
139139
$item->hasoneRelation();
140+
$item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
141+
$item->hasoneRelation()->setMeta(new Meta(['foo' => 'bar']));
140142

141143
$this->assertSame(
142144
[
@@ -211,11 +213,13 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array()
211213
/**
212214
* @test
213215
*/
214-
public function is_does_not_add_hasmany_relation_without_data_links_and_meta_in_to_json_api_array()
216+
public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array()
215217
{
216218
$item = new WithRelationshipItem();
217219
$item->setId('1234');
218220
$item->hasmanyRelation();
221+
$item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
222+
$item->hasmanyRelation()->setMeta(new Meta(['foo' => 'bar']));
219223

220224
$this->assertSame(
221225
[
@@ -288,11 +292,13 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array()
288292
/**
289293
* @test
290294
*/
291-
public function is_does_not_add_morphto_relation_without_data_links_and_meta_in_to_json_api_array()
295+
public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array()
292296
{
293297
$item = new WithRelationshipItem();
294298
$item->setId('1234');
295299
$item->morphtoRelation();
300+
$item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
301+
$item->morphtoRelation()->setMeta(new Meta(['foo' => 'bar']));
296302

297303
$this->assertSame(
298304
[
@@ -367,11 +373,13 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array()
367373
/**
368374
* @test
369375
*/
370-
public function is_does_not_add_morphtomany_relation_without_data_links_and_meta_in_to_json_api_array()
376+
public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array()
371377
{
372378
$item = new WithRelationshipItem();
373379
$item->setId('1234');
374380
$item->morphtomanyRelation();
381+
$item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
382+
$item->morphtomanyRelation()->setMeta(new Meta(['foo' => 'bar']));
375383

376384
$this->assertSame(
377385
[

0 commit comments

Comments
 (0)