Skip to content

Commit

Permalink
Add FC in Error source objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 19, 2024
1 parent 45d3997 commit 724b23f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/V1/ErrorSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ protected function parse(mixed $object): void
throw new ValidationException('ErrorSource has to be an object, "' . gettype($object) . '" given.');
}

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

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

if (property_exists($object, 'parameter')) {
if (!is_string($object->parameter)) {
throw new ValidationException('property "parameter" has to be a string, "' . gettype($object->parameter) . '" given.');
$this->set('parameter', strval($value));
} else {
$this->set($key, $value);
}

$this->set('parameter', strval($object->parameter));
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/Unit/V1/ErrorSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ public function testOnlyPointerParameterPropertiesExists(): void
$object = new \stdClass();
$object->pointer = '/pointer';
$object->parameter = 'parameter';
$object->ignore = 'must be ignored';
$object->fc = 'test property for forward compatability';

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

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

$this->assertFalse($source->has('ignore'));
$this->assertTrue($source->has('pointer'));
$this->assertSame($source->get('pointer'), '/pointer');
$this->assertSame('/pointer', $source->get('pointer'));
$this->assertTrue($source->has('parameter'));
$this->assertSame($source->get('parameter'), 'parameter');
$this->assertSame('parameter', $source->get('parameter'));
$this->assertTrue($source->has('fc'));
$this->assertSame('test property for forward compatability', $source->get('fc'));

// test get() with not existing key throws an exception
$this->assertFalse($source->has('something'));
Expand Down
12 changes: 12 additions & 0 deletions tests/files/20_error_with_source_header_property.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"errors": [
{
"source": {
"header": "Content-Type"
}
}
],
"jsonapi": {
"version": "1.1"
}
}

0 comments on commit 724b23f

Please sign in to comment.