Skip to content

Commit d0e7564

Browse files
authored
Merge pull request #89 from BurningDog/feature/relationships-data-meta
Bug: data.meta ignored in relationships
2 parents 4b45f8e + 8b486ac commit d0e7564

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/Item.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,22 @@ public function getRelationships(): array
142142
'type' => $relation->getIncluded()->getType(),
143143
'id' => $relation->getIncluded()->getId(),
144144
];
145+
if ($relation->getIncluded()->getMeta()) {
146+
$relationships[$name]['data']['meta'] = $relation->getIncluded()->getMeta()->toArray();
147+
}
145148
}
146149
} elseif ($relation instanceof ManyRelationInterface) {
147150
$relationships[$name]['data'] = [];
148151

149152
foreach ($relation->getIncluded() as $item) {
150-
$relationships[$name]['data'][] = [
153+
$data = [
151154
'type' => $item->getType(),
152155
'id' => $item->getId(),
153156
];
157+
if ($item->getMeta()) {
158+
$data['meta'] = $item->getMeta()->toArray();
159+
}
160+
$relationships[$name]['data'][] = $data;
154161
}
155162
}
156163

src/Parsers/ItemParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ function ($identifier) {
191191
throw new ValidationException(sprintf('ResourceIdentifier property "id" MUST be a string, "%s" given.', gettype($data->id)));
192192
}
193193

194-
return $this->getItemInstance($data->type)->setId($data->id);
194+
$item = $this->getItemInstance($data->type)->setId($data->id);
195+
if (property_exists($data, 'meta')) {
196+
$item->setMeta($this->metaParser->parse($data->meta));
197+
}
198+
199+
return $item;
195200
}
196201
}

tests/ItemTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,9 @@ public function itCanGetAllRelationships()
511511
$masterItem = new MasterItem();
512512
$childItem = new ChildItem();
513513
$childItem->setId('1');
514+
$childItem->setMeta(new Meta(['foo' => 'bar']));
514515
$masterItem->child()->associate($childItem);
516+
$masterItem->children()->associate(new Collection([$childItem]));
515517

516518
$relations = $masterItem->getRelationships();
517519

@@ -520,6 +522,20 @@ public function itCanGetAllRelationships()
520522
'data' => [
521523
'type' => 'child',
522524
'id' => '1',
525+
'meta' => [
526+
'foo' => 'bar',
527+
],
528+
],
529+
],
530+
'children' => [
531+
'data' => [
532+
[
533+
'type' => 'child',
534+
'id' => '1',
535+
'meta' => [
536+
'foo' => 'bar',
537+
],
538+
],
523539
],
524540
],
525541
], $relations);

tests/Parsers/ItemParserTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,25 @@ public function itParsesMeta()
645645
static::assertEquals(new Meta(['foo' => 'bar']), $item->getMeta());
646646
}
647647

648+
/**
649+
* @test
650+
*/
651+
public function itParsesMetaInData()
652+
{
653+
$typeMapper = new TypeMapper();
654+
$typeMapper->setMapping('child', ChildItem::class);
655+
$typeMapper->setMapping('master', MasterItem::class);
656+
$parser = $this->getItemParser($typeMapper);
657+
658+
$item = $parser->parse($this->getJsonApiItemMock('master', '1'));
659+
660+
$dataMeta = $item->getRelation('childwithdatameta')->getIncluded()->getMeta();
661+
static::assertInstanceOf(Meta::class, $dataMeta);
662+
663+
$image = $dataMeta->imageDerivatives->links->header->href;
664+
$this->assertEquals('https://example.com/image/header/about-us.jpeg', $image);
665+
}
666+
648667
/**
649668
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
650669
*
@@ -766,6 +785,30 @@ private function getJsonApiItemMock($type, $id)
766785
'foo' => 'bar',
767786
],
768787
],
788+
'childwithdatameta' => [
789+
'data' => [
790+
'type' => 'child',
791+
'id' => '9',
792+
'meta' => [
793+
'alt' => '',
794+
'width' => 1920,
795+
'height' => 1280,
796+
'imageDerivatives' => [
797+
'links' => [
798+
'header' => [
799+
'href' => 'https://example.com/image/header/about-us.jpeg',
800+
'meta' => [
801+
'rel' => 'drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative',
802+
],
803+
],
804+
],
805+
],
806+
],
807+
],
808+
'links' => [
809+
'self' => 'http://example.com/'.$type.'/'.$id.'/relationships/childwithdatameta',
810+
],
811+
],
769812
'empty' => [
770813
'data' => null,
771814
],

0 commit comments

Comments
 (0)