Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add FC in Relationship object
  • Loading branch information
Art4 committed Jul 19, 2024
commit ca0b5017b997456923f3a070d869b1dc98ca0960
29 changes: 14 additions & 15 deletions src/V1/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ protected function parse(mixed $object): void
throw new ValidationException('A Relationship object MUST contain at least one of the following properties: links, data, meta');
}

if (property_exists($object, 'data')) {
if ($object->data === null) {
$this->set('data', $object->data);
} elseif (is_array($object->data)) {
$this->set('data', $this->create('ResourceIdentifierCollection', $object->data));
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'data') {
if ($value === null) {
$this->set('data', $value);
} elseif (is_array($value)) {
$this->set('data', $this->create('ResourceIdentifierCollection', $value));
} else {
$this->set('data', $this->create('ResourceIdentifier', $value));
}
} elseif ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} elseif ($key === 'links') {
$this->set('links', $this->create('RelationshipLink', $value));
} else {
$this->set('data', $this->create('ResourceIdentifier', $object->data));
$this->set($key, $value);
}
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
}

// Parse 'links' after 'data'
if (property_exists($object, 'links')) {
$this->set('links', $this->create('RelationshipLink', $object->links));
}
}

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/V1/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ public function testCreateWithObjectReturnsSelf(): void
{
$object = new \stdClass();
$object->meta = new \stdClass();
$object->fc = 'test property for forward compatability';

$relationship = new Relationship($object, $this->manager, $this->parent);

$this->assertInstanceOf(Relationship::class, $relationship);
$this->assertInstanceOf(Accessable::class, $relationship);

$this->assertSame(['meta', 'fc'], $relationship->getKeys());
$this->assertTrue($relationship->has('meta'));

$meta = $relationship->get('meta');
$this->assertInstanceOf(Accessable::class, $relationship->get('meta'));
$this->assertTrue($relationship->has('fc'));
$this->assertSame('test property for forward compatability', $relationship->get('fc'));

// test get() with not existing key throws an exception
$this->assertFalse($relationship->has('something'));
Expand Down