Skip to content

Commit ce10d04

Browse files
samizdamn.gnato
andauthored
DTO extracting (#31)
* Prepare dto extracting * Try checkout@v3 * try actions/cache@v3 * Use dto from json-api-dto, drop original sources, extend only --------- Co-authored-by: n.gnato <[email protected]>
1 parent c98539e commit ce10d04

File tree

12 files changed

+41
-98
lines changed

12 files changed

+41
-98
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
steps:
2323
- name: Checkout Code
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v3
2525

2626
- name: Install PHP
2727
uses: shivammathur/setup-php@v2
@@ -36,7 +36,7 @@ jobs:
3636

3737
- name: Cache Composer packages
3838
id: composer-cache
39-
uses: actions/cache@v2
39+
uses: actions/cache@v3
4040
with:
4141
path: vendor
4242
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ matrix.prefer }}-

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Deprecated
10+
- mark classes in namespace FreeElephants\JsonApiToolkit\DTO are deprecated
11+
912
### Changed
1013
- Add conflict with origin neomerx/json-api (use fork laravel-json-api/neomerx-json-api for best php 8.* support)
14+
- Use DTO from free-elephants/json-api-dto
1115

1216
## [0.0.15] - 2025-02-06
1317

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"doctrine/persistence": "^2.0|^3.0",
1919
"fig/http-message-util": "^1.0",
2020
"free-elephants/i18n": "^0.0.1",
21+
"free-elephants/json-api-dto": "^0.0.1",
2122
"laminas/laminas-stratigility": "^3.2",
2223
"laravel-json-api/neomerx-json-api": "^5.0.2",
2324
"league/openapi-psr7-validator": "0.19 - 0.22",

src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
abstract class AbstractAttributes extends BaseKeyValueStructure
5+
/**
6+
* @deprecated
7+
* @see \FreeElephants\JsonApi\DTO\AbstractAttributes
8+
*/
9+
abstract class AbstractAttributes extends \FreeElephants\JsonApi\DTO\AbstractAttributes
610
{
711
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,10 @@
55
use Psr\Http\Message\MessageInterface;
66

77
/**
8+
* @deprecated
9+
* @see \FreeElephants\JsonApi\DTO\AbstractDocument
810
* @property AbstractResourceObject|mixed $data
911
*/
10-
abstract class AbstractDocument
12+
abstract class AbstractDocument extends \FreeElephants\JsonApi\DTO\AbstractDocument
1113
{
12-
final public function __construct(array $data)
13-
{
14-
$concreteClass = new \ReflectionClass($this);
15-
$dataProperty = $concreteClass->getProperty('data');
16-
/** @var \ReflectionNamedType $reflectionType */
17-
$reflectionType = $dataProperty->getType();
18-
$dataClassName = $reflectionType->getName();
19-
$this->data = new $dataClassName($data['data']);
20-
}
21-
22-
/**
23-
* @param MessageInterface $httpMessage
24-
* @return static
25-
*/
26-
public static function fromHttpMessage(MessageInterface $httpMessage): self
27-
{
28-
$httpMessage->getBody()->rewind();
29-
$rawJson = $httpMessage->getBody()->getContents();
30-
$decodedJson = json_decode($rawJson, true);
31-
32-
return new static($decodedJson);
33-
}
3414
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
abstract class AbstractRelationships
5+
/**
6+
* @deprecated
7+
* @see \FreeElephants\JsonApi\DTO\AbstractRelationships
8+
*/
9+
abstract class AbstractRelationships extends \FreeElephants\JsonApi\DTO\AbstractRelationships
610
{
7-
public function __construct(array $data)
8-
{
9-
foreach ($data as $relationshipName => $relationshipsData) {
10-
$this->{$relationshipName} = new RelationshipToOne($relationshipsData);
11-
}
12-
}
1311
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,10 @@
55
use FreeElephants\JsonApiToolkit\DTO\Reflection\SuitableRelationshipsTypeDetector;
66

77
/**
8-
* @property AbstractAttributes $attributes
9-
* @property AbstractRelationships $relationships
8+
*
9+
* @deprecated
10+
* @see \FreeElephants\JsonApi\DTO\AbstractResourceObject
1011
*/
11-
class AbstractResourceObject
12+
class AbstractResourceObject extends \FreeElephants\JsonApi\DTO\AbstractResourceObject
1213
{
13-
public string $id;
14-
public string $type;
15-
16-
public function __construct(array $data)
17-
{
18-
$this->id = $data['id'];
19-
$this->type = $data['type'];
20-
21-
$concreteClass = new \ReflectionClass($this);
22-
23-
if (property_exists($this, 'attributes')) {
24-
$attributesProperty = $concreteClass->getProperty('attributes');
25-
$attributesClass = $attributesProperty->getType()->getName();
26-
$this->attributes = new $attributesClass($data['attributes']);
27-
}
28-
29-
if (property_exists($this, 'relationships')) {
30-
$relationshipsData = $data['relationships'];
31-
$concreteClass = new \ReflectionClass($this);
32-
$relationshipsProperty = $concreteClass->getProperty('relationships');
33-
$reflectionType = $relationshipsProperty->getType();
34-
35-
// handle php 8 union types
36-
if ($reflectionType instanceof \ReflectionUnionType) {
37-
$relationshipsClass = (new SuitableRelationshipsTypeDetector())->detect($reflectionType, $relationshipsData);
38-
} else {
39-
$relationshipsClass = $reflectionType->getName();
40-
}
41-
42-
$relationshipsDto = new $relationshipsClass($relationshipsData);
43-
$this->relationships = $relationshipsDto;
44-
}
45-
}
4614
}

src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
class BaseKeyValueStructure
5+
/**
6+
* @deprecated
7+
* @see \FreeElephants\JsonApi\DTO\BaseKeyValueStructure
8+
*/
9+
class BaseKeyValueStructure extends \FreeElephants\JsonApi\DTO\BaseKeyValueStructure
610
{
7-
public function __construct(array $attributes)
8-
{
9-
$concreteClass = new \ReflectionClass($this);
10-
foreach ($attributes as $name => $value) {
11-
$property = $concreteClass->getProperty($name);
12-
if ($property->hasType()) {
13-
$propertyType = $property->getType();
14-
if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) {
15-
$propertyClassName = $propertyType->getName();
16-
$value = new $propertyClassName($value);
17-
}
18-
}
19-
$this->{$name} = $value;
20-
}
21-
}
2211
}

src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
class RelationshipToOne extends AbstractDocument
5+
/**
6+
* @deprecated
7+
* @see \FreeElephants\JsonApi\DTO\RelationshipToOne
8+
*/
9+
class RelationshipToOne extends \FreeElephants\JsonApi\DTO\RelationshipToOne
610
{
7-
public ResourceIdentifierObject $data;
811
}

src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
namespace FreeElephants\JsonApiToolkit\DTO;
44

5-
class ResourceIdentifierObject
5+
/**
6+
* @deprecated
7+
* @see \FreeElephants\JsonApi\DTO\ResourceIdentifierObject
8+
*/
9+
class ResourceIdentifierObject extends \FreeElephants\JsonApi\DTO\ResourceIdentifierObject
610
{
7-
public string $id;
8-
public string $type;
9-
10-
public function __construct(array $data)
11-
{
12-
$this->id = $data['id'];
13-
$this->type = $data['type'];
14-
}
1511
}

tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FooAttributes extends AbstractAttributes
7070

7171
class FooRelationships extends AbstractRelationships
7272
{
73-
public RelationshipToOne $baz;
73+
public \FreeElephants\JsonApi\DTO\RelationshipToOne $baz;
7474
}
7575

7676
class Nested extends BaseKeyValueStructure

tests/FreeElephants/JsonApiToolkit/DTO/Example/OneRelationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
class OneRelationships extends AbstractRelationships
99
{
10-
public RelationshipToOne $one;
10+
public \FreeElephants\JsonApi\DTO\RelationshipToOne $one;
1111
}

0 commit comments

Comments
 (0)