Skip to content

Commit 4be2ed1

Browse files
committed
Add FC for new properties in Error object
1 parent a12a403 commit 4be2ed1

File tree

2 files changed

+66
-70
lines changed

2 files changed

+66
-70
lines changed

src/V1/Error.php

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,71 +33,61 @@ protected function parse(mixed $object): void
3333
);
3434
}
3535

36-
if (property_exists($object, 'id')) {
37-
if (!is_string($object->id)) {
38-
throw new ValidationException(
39-
'property "id" has to be a string, "' .
40-
gettype($object->id) . '" given.'
41-
);
36+
foreach (get_object_vars($object) as $key => $value) {
37+
if ($key === 'id') {
38+
if (!is_string($object->id)) {
39+
throw new ValidationException(
40+
'property "id" has to be a string, "' .
41+
gettype($object->id) . '" given.'
42+
);
43+
}
44+
45+
$this->set('id', strval($object->id));
46+
} elseif ($key === 'links') {
47+
$this->set('links', $this->create('ErrorLink', $object->links));
48+
} elseif ($key === 'status') {
49+
if (!is_string($object->status)) {
50+
throw new ValidationException(
51+
'property "status" has to be a string, "' .
52+
gettype($object->status) . '" given.'
53+
);
54+
}
55+
56+
$this->set('status', strval($object->status));
57+
} elseif ($key === 'code') {
58+
if (!is_string($object->code)) {
59+
throw new ValidationException(
60+
'property "code" has to be a string, "' .
61+
gettype($object->code) . '" given.'
62+
);
63+
}
64+
65+
$this->set('code', strval($object->code));
66+
} elseif ($key === 'title') {
67+
if (!is_string($object->title)) {
68+
throw new ValidationException(
69+
'property "title" has to be a string, "' .
70+
gettype($object->title) . '" given.'
71+
);
72+
}
73+
74+
$this->set('title', strval($object->title));
75+
} elseif ($key === 'detail') {
76+
if (!is_string($object->detail)) {
77+
throw new ValidationException(
78+
'property "detail" has to be a string, "' .
79+
gettype($object->detail) . '" given.'
80+
);
81+
}
82+
83+
$this->set('detail', strval($object->detail));
84+
} elseif ($key === 'source') {
85+
$this->set('source', $this->create('ErrorSource', $object->source));
86+
} elseif ($key === 'meta') {
87+
$this->set('meta', $this->create('Meta', $object->meta));
88+
} else {
89+
$this->set($key, $value);
4290
}
43-
44-
$this->set('id', strval($object->id));
45-
}
46-
47-
if (property_exists($object, 'links')) {
48-
$this->set('links', $this->create('ErrorLink', $object->links));
49-
}
50-
51-
if (property_exists($object, 'status')) {
52-
if (!is_string($object->status)) {
53-
throw new ValidationException(
54-
'property "status" has to be a string, "' .
55-
gettype($object->status) . '" given.'
56-
);
57-
}
58-
59-
$this->set('status', strval($object->status));
60-
}
61-
62-
if (property_exists($object, 'code')) {
63-
if (!is_string($object->code)) {
64-
throw new ValidationException(
65-
'property "code" has to be a string, "' .
66-
gettype($object->code) . '" given.'
67-
);
68-
}
69-
70-
$this->set('code', strval($object->code));
71-
}
72-
73-
if (property_exists($object, 'title')) {
74-
if (!is_string($object->title)) {
75-
throw new ValidationException(
76-
'property "title" has to be a string, "' .
77-
gettype($object->title) . '" given.'
78-
);
79-
}
80-
81-
$this->set('title', strval($object->title));
82-
}
83-
84-
if (property_exists($object, 'detail')) {
85-
if (!is_string($object->detail)) {
86-
throw new ValidationException(
87-
'property "detail" has to be a string, "' .
88-
gettype($object->detail) . '" given.'
89-
);
90-
}
91-
92-
$this->set('detail', strval($object->detail));
93-
}
94-
95-
if (property_exists($object, 'source')) {
96-
$this->set('source', $this->create('ErrorSource', $object->source));
97-
}
98-
99-
if (property_exists($object, 'meta')) {
100-
$this->set('meta', $this->create('Meta', $object->meta));
10191
}
10292
}
10393

tests/Unit/V1/ErrorTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,35 @@ public function testCreateWithObjectReturnsSelf(): void
4646
$object->detail = 'detail';
4747
$object->source = new \stdClass();
4848
$object->meta = new \stdClass();
49+
$object->fc = 'test property for forward compatability';
4950

5051
$error = new Error($object, $this->manager, $this->parent);
5152

5253
$this->assertInstanceOf(Error::class, $error);
5354
$this->assertInstanceOf(Accessable::class, $error);
54-
$this->assertSame($error->getKeys(), ['id', 'links', 'status', 'code', 'title', 'detail', 'source', 'meta']);
55+
$this->assertSame(
56+
['id', 'links', 'status', 'code', 'title', 'detail', 'source', 'meta', 'fc'],
57+
$error->getKeys(),
58+
);
5559

5660
$this->assertTrue($error->has('id'));
57-
$this->assertSame($error->get('id'), 'id');
61+
$this->assertSame('id', $error->get('id'));
5862
$this->assertTrue($error->has('links'));
5963
$this->assertInstanceOf(Accessable::class, $error->get('links'));
6064
$this->assertTrue($error->has('status'));
61-
$this->assertSame($error->get('status'), 'status');
65+
$this->assertSame('status', $error->get('status'));
6266
$this->assertTrue($error->has('code'));
63-
$this->assertSame($error->get('code'), 'code');
67+
$this->assertSame('code', $error->get('code'));
6468
$this->assertTrue($error->has('title'));
65-
$this->assertSame($error->get('title'), 'title');
69+
$this->assertSame('title', $error->get('title'));
6670
$this->assertTrue($error->has('detail'));
67-
$this->assertSame($error->get('detail'), 'detail');
71+
$this->assertSame('detail', $error->get('detail'));
6872
$this->assertTrue($error->has('source'));
6973
$this->assertInstanceOf(Accessable::class, $error->get('source'));
7074
$this->assertTrue($error->has('meta'));
7175
$this->assertInstanceOf(Accessable::class, $error->get('meta'));
76+
$this->assertTrue($error->has('fc'));
77+
$this->assertSame('test property for forward compatability', $error->get('fc'));
7278

7379
// test get() with not existing key throws an exception
7480
$this->assertFalse($error->has('something'));

0 commit comments

Comments
 (0)