diff --git a/src/V1/ResourceItem.php b/src/V1/ResourceItem.php index 570a210..bfaeb40 100644 --- a/src/V1/ResourceItem.php +++ b/src/V1/ResourceItem.php @@ -39,8 +39,6 @@ protected function parse(mixed $object): void throw new ValidationException('A resource type MUST be a string'); } - $this->set('type', $object->type); - if ( $this->getManager()->getParam('optional_item_id', false) === false 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 if (!is_string($object->id)) { throw new ValidationException('A resource id MUST be a string'); } - - $this->set('id', $object->id); - } - - if (property_exists($object, 'meta')) { - $this->set('meta', $this->create('Meta', $object->meta)); } - if (property_exists($object, 'attributes')) { - $this->set('attributes', $this->create('Attributes', $object->attributes)); - } - - if (property_exists($object, 'relationships')) { - $this->set('relationships', $this->create('RelationshipCollection', $object->relationships)); - } + foreach (get_object_vars($object) as $key => $value) { + $value = match ($key) { + 'meta' => $this->create('Meta', $value), + 'attributes' => $this->create('Attributes', $value), + 'relationships' => $this->create('RelationshipCollection', $value), + 'links' => $this->create('ResourceItemLink', $value), + default => $value, + }; - if (property_exists($object, 'links')) { - $this->set('links', $this->create('ResourceItemLink', $object->links)); + $this->set($key, $value); } } diff --git a/tests/Unit/V1/ResourceItemTest.php b/tests/Unit/V1/ResourceItemTest.php index 44030be..6836333 100644 --- a/tests/Unit/V1/ResourceItemTest.php +++ b/tests/Unit/V1/ResourceItemTest.php @@ -78,13 +78,17 @@ public function testCreateWithFullObject(): void $object->attributes = new \stdClass(); $object->relationships = new \stdClass(); $object->links = new \stdClass(); + $object->fc = 'test property for forward compatability'; $item = new ResourceItem($object, $this->manager, $this->parent); $this->assertInstanceOf(ResourceItem::class, $item); - $this->assertSame($item->get('type'), 'type'); - $this->assertSame($item->get('id'), '789'); + $this->assertSame(['type', 'id', 'meta', 'attributes', 'relationships', 'links', 'fc'], $item->getKeys()); + $this->assertTrue($item->has('type')); + $this->assertSame('type', $item->get('type')); + $this->assertTrue($item->has('id')); + $this->assertSame('789', $item->get('id')); $this->assertTrue($item->has('meta')); $this->assertInstanceOf(Accessable::class, $item->get('meta')); $this->assertTrue($item->has('attributes')); @@ -93,7 +97,8 @@ public function testCreateWithFullObject(): void $this->assertInstanceOf(Accessable::class, $item->get('relationships')); $this->assertTrue($item->has('links')); $this->assertInstanceOf(Accessable::class, $item->get('links')); - $this->assertSame($item->getKeys(), ['type', 'id', 'meta', 'attributes', 'relationships', 'links']); + $this->assertTrue($item->has('fc')); + $this->assertSame('test property for forward compatability', $item->get('fc')); } /**