Skip to content

Commit

Permalink
Add FC for new properties in ResourceItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 19, 2024
1 parent 9331730 commit 850ca74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
26 changes: 9 additions & 17 deletions src/V1/ResourceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down
11 changes: 8 additions & 3 deletions tests/Unit/V1/ResourceItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
}

/**
Expand Down

0 comments on commit 850ca74

Please sign in to comment.