Skip to content

Commit 6458c54

Browse files
samizdamn.gnato
andauthored
Optional attributes field (#8)
* Check input attributes value * Prepare v0.0.7 * Optional id --------- Co-authored-by: n.gnato <[email protected]>
1 parent 2b02699 commit 6458c54

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.0.7] - 2025-06-26
10+
11+
### Fixed
12+
- `id`, `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2.
13+
914
## [0.0.6] - 2025-04-05
1015

1116
### Fixed
@@ -37,7 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3742
### Added
3843
- Extract all DTO types from FreeElephants/json-api-php-toolkit to this project
3944

40-
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.6...HEAD
45+
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.7...HEAD
46+
[0.0.7]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.7
4147
[0.0.6]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.6
4248
[0.0.5]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.5
4349
[0.0.4]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.4

src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class AbstractResourceObject
1515

1616
public function __construct(array $data)
1717
{
18-
$this->id = $data['id'];
18+
$this->id = $data['id'] ?? null;
1919
$this->type = $data['type'];
2020

2121
$concreteClass = new \ReflectionClass($this);
2222

23-
if (property_exists($this, 'attributes')) {
23+
if (property_exists($this, 'attributes') && array_key_exists('attributes', $data)) {
2424
$attributesPropertyType = $concreteClass->getProperty('attributes')->getType();
2525

2626
if($attributesPropertyType instanceof \ReflectionUnionType) {
@@ -31,7 +31,7 @@ public function __construct(array $data)
3131
$this->attributes = new $attributesClass($data['attributes']);
3232
}
3333

34-
if (property_exists($this, 'relationships')) {
34+
if (property_exists($this, 'relationships') && array_key_exists('relationships', $data)) {
3535
$relationshipsData = $data['relationships'];
3636
$concreteClass = new \ReflectionClass($this);
3737
$relationshipsProperty = $concreteClass->getProperty('relationships');

tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ public function testRelationshipTypes()
3030
$this->assertInstanceOf(Example\OneRelationships::class, $resourceObject->relationships);
3131
$this->assertSame('one', $resourceObject->relationships->one->data->type);
3232
}
33+
34+
public function testWithNullableId(): void
35+
{
36+
$resourceObject = new class([
37+
'type' => 'type',
38+
'attributes' => [
39+
'foo' => 'bar',
40+
],
41+
'relationships' => [
42+
'one' => [
43+
'data' => [
44+
'type' => 'one',
45+
'id' => 'one',
46+
],
47+
],
48+
],
49+
]) extends AbstractResourceObject {
50+
public Example\Attributes $attributes;
51+
public Example\OneRelationships $relationships;
52+
};
53+
54+
$this->assertNull($resourceObject->id);
55+
}
3356
}
3457

3558
class Attributes extends AbstractAttributes

0 commit comments

Comments
 (0)