Skip to content

Commit

Permalink
Add relation links and meta to JSON API array
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed Aug 5, 2020
1 parent 609c49a commit c5a85fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
46 changes: 27 additions & 19 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,36 @@ public function getRelationships(): array
$relationships = [];

foreach ($this->getRelations() as $name => $relation) {
if (!$relation->hasIncluded()) {
continue;
if ($relation->hasIncluded()) {
if ($relation instanceof OneRelationInterface) {
$relationships[$name]['data'] = null;

if ($relation->getIncluded() !== null) {
$relationships[$name]['data'] = [
'type' => $relation->getIncluded()->getType(),
'id' => $relation->getIncluded()->getId(),
];
}
} elseif ($relation instanceof ManyRelationInterface) {
$relationships[$name]['data'] = [];

foreach ($relation->getIncluded() as $item) {
$relationships[$name]['data'][] = [
'type' => $item->getType(),
'id' => $item->getId(),
];
}
}
}

if ($relation instanceof OneRelationInterface) {
$relationships[$name]['data'] = null;
$links = $relation->getLinks();
if ($links !== null) {
$relationships[$name]['links'] = $links->toArray();
}

if ($relation->getIncluded() !== null) {
$relationships[$name]['data'] = [
'type' => $relation->getIncluded()->getType(),
'id' => $relation->getIncluded()->getId(),
];
}
} elseif ($relation instanceof ManyRelationInterface) {
$relationships[$name]['data'] = [];

foreach ($relation->getIncluded() as $item) {
$relationships[$name]['data'][] = [
'type' => $item->getType(),
'id' => $item->getId(),
];
}
$meta = $relation->getMeta();
if ($meta !== null) {
$relationships[$name]['meta'] = $meta->toArray();
}
}

Expand Down
48 changes: 44 additions & 4 deletions tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function is_adds_hasone_relation_in_to_json_api_array()
$item = new WithRelationshipItem();
$item->setId('1234');
$item->hasoneRelation()->associate((new RelatedItem())->setId('5678'));
$item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
$item->hasoneRelation()->setMeta(new Meta(['foo' => 'bar']));

$this->assertSame(
[
Expand All @@ -89,6 +91,14 @@ public function is_adds_hasone_relation_in_to_json_api_array()
'type' => 'related-item',
'id' => '5678',
],
'links' => [
'self' => [
'href' => 'http://example.com/articles',
],
],
'meta' => [
'foo' => 'bar',
],
],
],
],
Expand Down Expand Up @@ -122,7 +132,7 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array()
/**
* @test
*/
public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array()
public function is_does_not_add_hasone_relation_without_data_links_and_meta_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
Expand All @@ -145,6 +155,8 @@ public function is_adds_hasmany_relation_in_to_json_api_array()
$item = new WithRelationshipItem();
$item->setId('1234');
$item->hasmanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')]));
$item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
$item->hasmanyRelation()->setMeta(new Meta(['foo' => 'bar']));

$this->assertSame(
[
Expand All @@ -158,6 +170,14 @@ public function is_adds_hasmany_relation_in_to_json_api_array()
'id' => '5678',
],
],
'links' => [
'self' => [
'href' => 'http://example.com/articles',
],
],
'meta' => [
'foo' => 'bar',
],
],
],
],
Expand Down Expand Up @@ -191,7 +211,7 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array()
/**
* @test
*/
public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array()
public function is_does_not_add_hasmany_relation_without_data_links_and_meta_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
Expand All @@ -214,6 +234,8 @@ public function is_adds_morphto_relation_in_to_json_api_array()
$item = new WithRelationshipItem();
$item->setId('1234');
$item->morphtoRelation()->associate((new RelatedItem())->setId('5678'));
$item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
$item->morphtoRelation()->setMeta(new Meta(['foo' => 'bar']));

$this->assertSame(
[
Expand All @@ -225,6 +247,14 @@ public function is_adds_morphto_relation_in_to_json_api_array()
'type' => 'related-item',
'id' => '5678',
],
'links' => [
'self' => [
'href' => 'http://example.com/articles',
],
],
'meta' => [
'foo' => 'bar',
],
],
],
],
Expand Down Expand Up @@ -258,7 +288,7 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array()
/**
* @test
*/
public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array()
public function is_does_not_add_morphto_relation_without_data_links_and_meta_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
Expand All @@ -281,6 +311,8 @@ public function is_adds_morphtomany_relation_in_to_json_api_array()
$item = new WithRelationshipItem();
$item->setId('1234');
$item->morphtomanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')]));
$item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')]));
$item->morphtomanyRelation()->setMeta(new Meta(['foo' => 'bar']));

$this->assertSame(
[
Expand All @@ -294,6 +326,14 @@ public function is_adds_morphtomany_relation_in_to_json_api_array()
'id' => '5678',
],
],
'links' => [
'self' => [
'href' => 'http://example.com/articles',
],
],
'meta' => [
'foo' => 'bar',
],
],
],
],
Expand Down Expand Up @@ -327,7 +367,7 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array()
/**
* @test
*/
public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array()
public function is_does_not_add_morphtomany_relation_without_data_links_and_meta_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
Expand Down

0 comments on commit c5a85fa

Please sign in to comment.