Skip to content

Commit 724b23f

Browse files
committed
Add FC in Error source objects
1 parent 45d3997 commit 724b23f

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/V1/ErrorSource.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ protected function parse(mixed $object): void
3131
throw new ValidationException('ErrorSource has to be an object, "' . gettype($object) . '" given.');
3232
}
3333

34-
if (property_exists($object, 'pointer')) {
35-
if (!is_string($object->pointer)) {
36-
throw new ValidationException('property "pointer" has to be a string, "' . gettype($object->pointer) . '" given.');
37-
}
34+
foreach (get_object_vars($object) as $key => $value) {
35+
if ($key === 'pointer') {
36+
if (!is_string($value)) {
37+
throw new ValidationException('property "pointer" has to be a string, "' . gettype($value) . '" given.');
38+
}
3839

39-
$this->set('pointer', strval($object->pointer));
40-
}
40+
$this->set('pointer', strval($value));
41+
} elseif ($key === 'parameter') {
42+
if (!is_string($value)) {
43+
throw new ValidationException('property "parameter" has to be a string, "' . gettype($value) . '" given.');
44+
}
4145

42-
if (property_exists($object, 'parameter')) {
43-
if (!is_string($object->parameter)) {
44-
throw new ValidationException('property "parameter" has to be a string, "' . gettype($object->parameter) . '" given.');
46+
$this->set('parameter', strval($value));
47+
} else {
48+
$this->set($key, $value);
4549
}
46-
47-
$this->set('parameter', strval($object->parameter));
4850
}
4951
}
5052

tests/Unit/V1/ErrorSourceTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,21 @@ public function testOnlyPointerParameterPropertiesExists(): void
4343
$object = new \stdClass();
4444
$object->pointer = '/pointer';
4545
$object->parameter = 'parameter';
46-
$object->ignore = 'must be ignored';
46+
$object->fc = 'test property for forward compatability';
4747

4848
$source = new ErrorSource($object, $this->manager, $this->parent);
4949

5050
$this->assertInstanceOf(ErrorSource::class, $source);
5151
$this->assertInstanceOf(Accessable::class, $source);
52-
$this->assertSame($source->getKeys(), ['pointer', 'parameter']);
52+
$this->assertSame(['pointer', 'parameter', 'fc'], $source->getKeys());
5353

5454
$this->assertFalse($source->has('ignore'));
5555
$this->assertTrue($source->has('pointer'));
56-
$this->assertSame($source->get('pointer'), '/pointer');
56+
$this->assertSame('/pointer', $source->get('pointer'));
5757
$this->assertTrue($source->has('parameter'));
58-
$this->assertSame($source->get('parameter'), 'parameter');
58+
$this->assertSame('parameter', $source->get('parameter'));
59+
$this->assertTrue($source->has('fc'));
60+
$this->assertSame('test property for forward compatability', $source->get('fc'));
5961

6062
// test get() with not existing key throws an exception
6163
$this->assertFalse($source->has('something'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"errors": [
3+
{
4+
"source": {
5+
"header": "Content-Type"
6+
}
7+
}
8+
],
9+
"jsonapi": {
10+
"version": "1.1"
11+
}
12+
}

0 commit comments

Comments
 (0)