Skip to content

Commit ca0b501

Browse files
committed
Add FC in Relationship object
1 parent f5e37be commit ca0b501

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Diff for: src/V1/Relationship.php

+14-15
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,23 @@ protected function parse(mixed $object): void
3535
throw new ValidationException('A Relationship object MUST contain at least one of the following properties: links, data, meta');
3636
}
3737

38-
if (property_exists($object, 'data')) {
39-
if ($object->data === null) {
40-
$this->set('data', $object->data);
41-
} elseif (is_array($object->data)) {
42-
$this->set('data', $this->create('ResourceIdentifierCollection', $object->data));
38+
foreach (get_object_vars($object) as $key => $value) {
39+
if ($key === 'data') {
40+
if ($value === null) {
41+
$this->set('data', $value);
42+
} elseif (is_array($value)) {
43+
$this->set('data', $this->create('ResourceIdentifierCollection', $value));
44+
} else {
45+
$this->set('data', $this->create('ResourceIdentifier', $value));
46+
}
47+
} elseif ($key === 'meta') {
48+
$this->set('meta', $this->create('Meta', $value));
49+
} elseif ($key === 'links') {
50+
$this->set('links', $this->create('RelationshipLink', $value));
4351
} else {
44-
$this->set('data', $this->create('ResourceIdentifier', $object->data));
52+
$this->set($key, $value);
4553
}
4654
}
47-
48-
if (property_exists($object, 'meta')) {
49-
$this->set('meta', $this->create('Meta', $object->meta));
50-
}
51-
52-
// Parse 'links' after 'data'
53-
if (property_exists($object, 'links')) {
54-
$this->set('links', $this->create('RelationshipLink', $object->links));
55-
}
5655
}
5756

5857
/**

Diff for: tests/Unit/V1/RelationshipTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ public function testCreateWithObjectReturnsSelf(): void
3838
{
3939
$object = new \stdClass();
4040
$object->meta = new \stdClass();
41+
$object->fc = 'test property for forward compatability';
4142

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

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

48+
$this->assertSame(['meta', 'fc'], $relationship->getKeys());
4749
$this->assertTrue($relationship->has('meta'));
48-
49-
$meta = $relationship->get('meta');
50+
$this->assertInstanceOf(Accessable::class, $relationship->get('meta'));
51+
$this->assertTrue($relationship->has('fc'));
52+
$this->assertSame('test property for forward compatability', $relationship->get('fc'));
5053

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

0 commit comments

Comments
 (0)