From aa6aa3cdbd000c58cd848270551373706a4e8102 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 13 Mar 2025 16:08:30 +0300 Subject: [PATCH 1/4] Prepare dto extracting --- CHANGELOG.md | 4 ++++ src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php | 4 ++++ src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php | 2 ++ .../JsonApiToolkit/DTO/AbstractRelationships.php | 4 ++++ .../JsonApiToolkit/DTO/AbstractResourceObject.php | 3 +++ .../JsonApiToolkit/DTO/BaseKeyValueStructure.php | 4 ++++ src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php | 4 ++++ .../JsonApiToolkit/DTO/ResourceIdentifierObject.php | 4 ++++ 8 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 653d6a7..d1932ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Deprecated +- mark classes in namespace FreeElephants\JsonApiToolkit\DTO are deprecated + ### Changed - Add conflict with origin neomerx/json-api (use fork laravel-json-api/neomerx-json-api for best php 8.* support) +- Use DTO from free-elephants/json-api-dto ## [0.0.15] - 2025-02-06 diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php index 4021bf4..cc12558 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php @@ -2,6 +2,10 @@ namespace FreeElephants\JsonApiToolkit\DTO; +/** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\AbstractAttributes + */ abstract class AbstractAttributes extends BaseKeyValueStructure { } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php index f3275cc..a8225f7 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php @@ -5,6 +5,8 @@ use Psr\Http\Message\MessageInterface; /** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\AbstractDocument * @property AbstractResourceObject|mixed $data */ abstract class AbstractDocument diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php index 354596b..f5e05a4 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php @@ -2,6 +2,10 @@ namespace FreeElephants\JsonApiToolkit\DTO; +/** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\AbstractRelationships + */ abstract class AbstractRelationships { public function __construct(array $data) diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php index 76b5c15..dae15fe 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php @@ -5,6 +5,9 @@ use FreeElephants\JsonApiToolkit\DTO\Reflection\SuitableRelationshipsTypeDetector; /** + * + * @deprecated + * @see \FreeElephants\JsonApi\DTO\AbstractResourceObject * @property AbstractAttributes $attributes * @property AbstractRelationships $relationships */ diff --git a/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php b/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php index 5c6c17d..835ca81 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php @@ -2,6 +2,10 @@ namespace FreeElephants\JsonApiToolkit\DTO; +/** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\BaseKeyValueStructure + */ class BaseKeyValueStructure { public function __construct(array $attributes) diff --git a/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php b/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php index 1ddc2b3..e8132e6 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php @@ -2,6 +2,10 @@ namespace FreeElephants\JsonApiToolkit\DTO; +/** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\RelationshipToOne + */ class RelationshipToOne extends AbstractDocument { public ResourceIdentifierObject $data; diff --git a/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php b/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php index f58aa7e..d58fdff 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php @@ -2,6 +2,10 @@ namespace FreeElephants\JsonApiToolkit\DTO; +/** + * @deprecated + * @see \FreeElephants\JsonApi\DTO\ResourceIdentifierObject + */ class ResourceIdentifierObject { public string $id; From ad65209704471d42d4677d831e1e6f3ab3e0a51e Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 13 Mar 2025 16:09:27 +0300 Subject: [PATCH 2/4] Try checkout@v3 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 08c242f..2253809 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: steps: - name: Checkout Code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Install PHP uses: shivammathur/setup-php@v2 From b1e849c664d5c9bb72454df494667a3c171f2e11 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 13 Mar 2025 16:10:10 +0300 Subject: [PATCH 3/4] try actions/cache@v3 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2253809..2020442 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ matrix.prefer }}- From c6d029545525f79580d2fffdaf62707d5f2c0b61 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 13 Mar 2025 17:49:34 +0300 Subject: [PATCH 4/4] Use dto from json-api-dto, drop original sources, extend only --- composer.json | 1 + .../JsonApiToolkit/DTO/AbstractAttributes.php | 2 +- .../JsonApiToolkit/DTO/AbstractDocument.php | 24 +----------- .../DTO/AbstractRelationships.php | 8 +--- .../DTO/AbstractResourceObject.php | 37 +------------------ .../DTO/BaseKeyValueStructure.php | 17 +-------- .../JsonApiToolkit/DTO/RelationshipToOne.php | 3 +- .../DTO/ResourceIdentifierObject.php | 10 +---- .../JsonApiToolkit/DTO/DocumentTest.php | 2 +- .../DTO/Example/OneRelationships.php | 2 +- 10 files changed, 10 insertions(+), 96 deletions(-) diff --git a/composer.json b/composer.json index f35744c..96c0181 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "doctrine/persistence": "^2.0|^3.0", "fig/http-message-util": "^1.0", "free-elephants/i18n": "^0.0.1", + "free-elephants/json-api-dto": "^0.0.1", "laminas/laminas-stratigility": "^3.2", "laravel-json-api/neomerx-json-api": "^5.0.2", "league/openapi-psr7-validator": "0.19 - 0.22", diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php index cc12558..bb5b1b0 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php @@ -6,6 +6,6 @@ * @deprecated * @see \FreeElephants\JsonApi\DTO\AbstractAttributes */ -abstract class AbstractAttributes extends BaseKeyValueStructure +abstract class AbstractAttributes extends \FreeElephants\JsonApi\DTO\AbstractAttributes { } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php index a8225f7..fe00e08 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php @@ -9,28 +9,6 @@ * @see \FreeElephants\JsonApi\DTO\AbstractDocument * @property AbstractResourceObject|mixed $data */ -abstract class AbstractDocument +abstract class AbstractDocument extends \FreeElephants\JsonApi\DTO\AbstractDocument { - final public function __construct(array $data) - { - $concreteClass = new \ReflectionClass($this); - $dataProperty = $concreteClass->getProperty('data'); - /** @var \ReflectionNamedType $reflectionType */ - $reflectionType = $dataProperty->getType(); - $dataClassName = $reflectionType->getName(); - $this->data = new $dataClassName($data['data']); - } - - /** - * @param MessageInterface $httpMessage - * @return static - */ - public static function fromHttpMessage(MessageInterface $httpMessage): self - { - $httpMessage->getBody()->rewind(); - $rawJson = $httpMessage->getBody()->getContents(); - $decodedJson = json_decode($rawJson, true); - - return new static($decodedJson); - } } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php index f5e05a4..ef1324f 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php @@ -6,12 +6,6 @@ * @deprecated * @see \FreeElephants\JsonApi\DTO\AbstractRelationships */ -abstract class AbstractRelationships +abstract class AbstractRelationships extends \FreeElephants\JsonApi\DTO\AbstractRelationships { - public function __construct(array $data) - { - foreach ($data as $relationshipName => $relationshipsData) { - $this->{$relationshipName} = new RelationshipToOne($relationshipsData); - } - } } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php b/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php index dae15fe..18c8d1f 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php @@ -8,42 +8,7 @@ * * @deprecated * @see \FreeElephants\JsonApi\DTO\AbstractResourceObject - * @property AbstractAttributes $attributes - * @property AbstractRelationships $relationships */ -class AbstractResourceObject +class AbstractResourceObject extends \FreeElephants\JsonApi\DTO\AbstractResourceObject { - public string $id; - public string $type; - - public function __construct(array $data) - { - $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(); - $this->attributes = new $attributesClass($data['attributes']); - } - - if (property_exists($this, 'relationships')) { - $relationshipsData = $data['relationships']; - $concreteClass = new \ReflectionClass($this); - $relationshipsProperty = $concreteClass->getProperty('relationships'); - $reflectionType = $relationshipsProperty->getType(); - - // handle php 8 union types - if ($reflectionType instanceof \ReflectionUnionType) { - $relationshipsClass = (new SuitableRelationshipsTypeDetector())->detect($reflectionType, $relationshipsData); - } else { - $relationshipsClass = $reflectionType->getName(); - } - - $relationshipsDto = new $relationshipsClass($relationshipsData); - $this->relationships = $relationshipsDto; - } - } } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php b/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php index 835ca81..4d83dd2 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php @@ -6,21 +6,6 @@ * @deprecated * @see \FreeElephants\JsonApi\DTO\BaseKeyValueStructure */ -class BaseKeyValueStructure +class BaseKeyValueStructure extends \FreeElephants\JsonApi\DTO\BaseKeyValueStructure { - public function __construct(array $attributes) - { - $concreteClass = new \ReflectionClass($this); - foreach ($attributes as $name => $value) { - $property = $concreteClass->getProperty($name); - if ($property->hasType()) { - $propertyType = $property->getType(); - if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) { - $propertyClassName = $propertyType->getName(); - $value = new $propertyClassName($value); - } - } - $this->{$name} = $value; - } - } } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php b/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php index e8132e6..a78d870 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php @@ -6,7 +6,6 @@ * @deprecated * @see \FreeElephants\JsonApi\DTO\RelationshipToOne */ -class RelationshipToOne extends AbstractDocument +class RelationshipToOne extends \FreeElephants\JsonApi\DTO\RelationshipToOne { - public ResourceIdentifierObject $data; } diff --git a/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php b/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php index d58fdff..b60be1a 100644 --- a/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php +++ b/src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php @@ -6,14 +6,6 @@ * @deprecated * @see \FreeElephants\JsonApi\DTO\ResourceIdentifierObject */ -class ResourceIdentifierObject +class ResourceIdentifierObject extends \FreeElephants\JsonApi\DTO\ResourceIdentifierObject { - public string $id; - public string $type; - - public function __construct(array $data) - { - $this->id = $data['id']; - $this->type = $data['type']; - } } diff --git a/tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php b/tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php index 6ac771c..a0a2e26 100644 --- a/tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php +++ b/tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php @@ -70,7 +70,7 @@ class FooAttributes extends AbstractAttributes class FooRelationships extends AbstractRelationships { - public RelationshipToOne $baz; + public \FreeElephants\JsonApi\DTO\RelationshipToOne $baz; } class Nested extends BaseKeyValueStructure diff --git a/tests/FreeElephants/JsonApiToolkit/DTO/Example/OneRelationships.php b/tests/FreeElephants/JsonApiToolkit/DTO/Example/OneRelationships.php index b62c848..4ad123b 100644 --- a/tests/FreeElephants/JsonApiToolkit/DTO/Example/OneRelationships.php +++ b/tests/FreeElephants/JsonApiToolkit/DTO/Example/OneRelationships.php @@ -7,5 +7,5 @@ class OneRelationships extends AbstractRelationships { - public RelationshipToOne $one; + public \FreeElephants\JsonApi\DTO\RelationshipToOne $one; }