Skip to content

Handle union types #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.6] - 2025-04-05

### Fixed
- Handle Union Types for `data` and `attributes` fields.

## [0.0.5] - 2025-03-24

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

[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.5...HEAD
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.6...HEAD
[0.0.6]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.6
[0.0.5]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.5
[0.0.4]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.4
[0.0.3]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.3
Expand Down
22 changes: 18 additions & 4 deletions src/FreeElephants/JsonApi/DTO/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace FreeElephants\JsonApi\DTO;

use Psr\Http\Message\MessageInterface;

/**
* @property AbstractResourceObject|mixed $data
*/
Expand All @@ -13,9 +11,25 @@ final public function __construct(array $payload)
{
$concreteClass = new \ReflectionClass($this);
$dataProperty = $concreteClass->getProperty('data');
/** @var \ReflectionNamedType $reflectionType */

$reflectionType = $dataProperty->getType();
$dataClassName = $reflectionType->getName();
if ($reflectionType instanceof \ReflectionNamedType) {
$dataClassName = $reflectionType->getName();
} else {
/** @var \ReflectionUnionType $reflectionType */
$dataClassName = $reflectionType->getTypes()[0]->getName();
}

/**
* In cases like:
* `public array|Example\ResourceObjectExt|Example\ResourceObject $data;`
* ReflectionUnionType::getTypes() return types in next orders:
* - Example\ResourceObjectExt
* - Example\ResourceObject
* - array
*
* This exception is [can] not covered with test. But this behavior not documented at https://www.php.net/manual/en/reflectionuniontype.gettypes.php
*/
if ($dataClassName !== 'array') {
$data = new $dataClassName($payload['data']);
} else {
Expand Down
11 changes: 8 additions & 3 deletions src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ class AbstractResourceObject

public function __construct(array $data)
{
$this->id = $data['id'] ?? null;
$this->id = $data['id'];
$this->type = $data['type'];

$concreteClass = new \ReflectionClass($this);

if (property_exists($this, 'attributes')) {
$attributesProperty = $concreteClass->getProperty('attributes');
$attributesClass = $attributesProperty->getType()->getName();
$attributesPropertyType = $concreteClass->getProperty('attributes')->getType();

if($attributesPropertyType instanceof \ReflectionUnionType) {
$attributesClass = $attributesPropertyType->getTypes()[0]->getName();
} else {
$attributesClass = $attributesPropertyType->getName();
}
$this->attributes = new $attributesClass($data['attributes']);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/Example/AttributesExt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO\Example;

class AttributesExt extends Attributes
{
public int $baz;
}
11 changes: 11 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/Example/ResourceObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO\Example;

use FreeElephants\JsonApi\DTO\AbstractResourceObject;

class ResourceObject extends AbstractResourceObject
{

}
9 changes: 9 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/Example/ResourceObjectExt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO\Example;

class ResourceObjectExt extends ResourceObject
{

}
25 changes: 25 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/Reflection/DocumentTestPHP8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO\Reflection;

use FreeElephants\JsonApi\AbstractTestCase;
use FreeElephants\JsonApi\DTO\AbstractDocument;
use FreeElephants\JsonApi\DTO\Example;

class DocumentTestPHP8 extends AbstractTestCase
{
public function testUnionTypes(): void
{
$document = new class([
'data' => [
'id' => '1',
'type' => 'foos',
],
]) extends AbstractDocument {
public Example\ResourceObjectExt|Example\ResourceObject $data;
};

$this->assertInstanceOf(Example\ResourceObjectExt::class, $document->data);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace FreeElephants\JsonApi\DTO;
namespace FreeElephants\JsonApi\DTO\Reflection;

use FreeElephants\JsonApi\AbstractTestCase;
use FreeElephants\JsonApi\DTO\AbstractResourceObject;
use FreeElephants\JsonApi\DTO\Example;
use FreeElephants\JsonApi\DTO\Example\AttributesExt;

class ResourceObjectTestPHP8 extends AbstractTestCase
{
Expand All @@ -13,6 +16,7 @@ public function testUnionTypes()
'type' => 'type',
'attributes' => [
'foo' => 'bar',
'baz' => 1,
],
'relationships' => [
'one' => [
Expand All @@ -23,11 +27,14 @@ public function testUnionTypes()
],
],
]) extends AbstractResourceObject{
public Example\Attributes $attributes;
public Example\AttributesExt|Example\Attributes $attributes;
public Example\OneRelationships|Example\TwoRelationships $relationships;
};

$this->assertSame('one', $resourceObject->relationships->one->data->type);
$this->assertSame('bar', $resourceObject->attributes->foo);
$this->assertSame(1, $resourceObject->attributes->baz);
$this->assertInstanceOf(AttributesExt::class, $resourceObject->attributes);
}
}