Skip to content

Commit 850ca74

Browse files
committed
Add FC for new properties in ResourceItem
1 parent 9331730 commit 850ca74

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/V1/ResourceItem.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ protected function parse(mixed $object): void
3939
throw new ValidationException('A resource type MUST be a string');
4040
}
4141

42-
$this->set('type', $object->type);
43-
4442
if (
4543
$this->getManager()->getParam('optional_item_id', false) === false
4644
or !$this->getParent()->has('data') // If parent has no `data` than parent is a ResourceCollection
@@ -52,24 +50,18 @@ protected function parse(mixed $object): void
5250
if (!is_string($object->id)) {
5351
throw new ValidationException('A resource id MUST be a string');
5452
}
55-
56-
$this->set('id', $object->id);
57-
}
58-
59-
if (property_exists($object, 'meta')) {
60-
$this->set('meta', $this->create('Meta', $object->meta));
6153
}
6254

63-
if (property_exists($object, 'attributes')) {
64-
$this->set('attributes', $this->create('Attributes', $object->attributes));
65-
}
66-
67-
if (property_exists($object, 'relationships')) {
68-
$this->set('relationships', $this->create('RelationshipCollection', $object->relationships));
69-
}
55+
foreach (get_object_vars($object) as $key => $value) {
56+
$value = match ($key) {
57+
'meta' => $this->create('Meta', $value),
58+
'attributes' => $this->create('Attributes', $value),
59+
'relationships' => $this->create('RelationshipCollection', $value),
60+
'links' => $this->create('ResourceItemLink', $value),
61+
default => $value,
62+
};
7063

71-
if (property_exists($object, 'links')) {
72-
$this->set('links', $this->create('ResourceItemLink', $object->links));
64+
$this->set($key, $value);
7365
}
7466
}
7567

tests/Unit/V1/ResourceItemTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ public function testCreateWithFullObject(): void
7878
$object->attributes = new \stdClass();
7979
$object->relationships = new \stdClass();
8080
$object->links = new \stdClass();
81+
$object->fc = 'test property for forward compatability';
8182

8283
$item = new ResourceItem($object, $this->manager, $this->parent);
8384

8485
$this->assertInstanceOf(ResourceItem::class, $item);
8586

86-
$this->assertSame($item->get('type'), 'type');
87-
$this->assertSame($item->get('id'), '789');
87+
$this->assertSame(['type', 'id', 'meta', 'attributes', 'relationships', 'links', 'fc'], $item->getKeys());
88+
$this->assertTrue($item->has('type'));
89+
$this->assertSame('type', $item->get('type'));
90+
$this->assertTrue($item->has('id'));
91+
$this->assertSame('789', $item->get('id'));
8892
$this->assertTrue($item->has('meta'));
8993
$this->assertInstanceOf(Accessable::class, $item->get('meta'));
9094
$this->assertTrue($item->has('attributes'));
@@ -93,7 +97,8 @@ public function testCreateWithFullObject(): void
9397
$this->assertInstanceOf(Accessable::class, $item->get('relationships'));
9498
$this->assertTrue($item->has('links'));
9599
$this->assertInstanceOf(Accessable::class, $item->get('links'));
96-
$this->assertSame($item->getKeys(), ['type', 'id', 'meta', 'attributes', 'relationships', 'links']);
100+
$this->assertTrue($item->has('fc'));
101+
$this->assertSame('test property for forward compatability', $item->get('fc'));
97102
}
98103

99104
/**

0 commit comments

Comments
 (0)