Skip to content

Commit c6d0295

Browse files
author
n.gnato
committed
Use dto from json-api-dto, drop original sources, extend only
1 parent b1e849c commit c6d0295

File tree

10 files changed

+10
-96
lines changed

10 files changed

+10
-96
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @deprecated
77
* @see \FreeElephants\JsonApi\DTO\AbstractAttributes
88
*/
9-
abstract class AbstractAttributes extends BaseKeyValueStructure
9+
abstract class AbstractAttributes extends \FreeElephants\JsonApi\DTO\AbstractAttributes
1010
{
1111
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@
99
* @see \FreeElephants\JsonApi\DTO\AbstractDocument
1010
* @property AbstractResourceObject|mixed $data
1111
*/
12-
abstract class AbstractDocument
12+
abstract class AbstractDocument extends \FreeElephants\JsonApi\DTO\AbstractDocument
1313
{
14-
final public function __construct(array $data)
15-
{
16-
$concreteClass = new \ReflectionClass($this);
17-
$dataProperty = $concreteClass->getProperty('data');
18-
/** @var \ReflectionNamedType $reflectionType */
19-
$reflectionType = $dataProperty->getType();
20-
$dataClassName = $reflectionType->getName();
21-
$this->data = new $dataClassName($data['data']);
22-
}
23-
24-
/**
25-
* @param MessageInterface $httpMessage
26-
* @return static
27-
*/
28-
public static function fromHttpMessage(MessageInterface $httpMessage): self
29-
{
30-
$httpMessage->getBody()->rewind();
31-
$rawJson = $httpMessage->getBody()->getContents();
32-
$decodedJson = json_decode($rawJson, true);
33-
34-
return new static($decodedJson);
35-
}
3614
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
* @deprecated
77
* @see \FreeElephants\JsonApi\DTO\AbstractRelationships
88
*/
9-
abstract class AbstractRelationships
9+
abstract class AbstractRelationships extends \FreeElephants\JsonApi\DTO\AbstractRelationships
1010
{
11-
public function __construct(array $data)
12-
{
13-
foreach ($data as $relationshipName => $relationshipsData) {
14-
$this->{$relationshipName} = new RelationshipToOne($relationshipsData);
15-
}
16-
}
1711
}

src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,7 @@
88
*
99
* @deprecated
1010
* @see \FreeElephants\JsonApi\DTO\AbstractResourceObject
11-
* @property AbstractAttributes $attributes
12-
* @property AbstractRelationships $relationships
1311
*/
14-
class AbstractResourceObject
12+
class AbstractResourceObject extends \FreeElephants\JsonApi\DTO\AbstractResourceObject
1513
{
16-
public string $id;
17-
public string $type;
18-
19-
public function __construct(array $data)
20-
{
21-
$this->id = $data['id'];
22-
$this->type = $data['type'];
23-
24-
$concreteClass = new \ReflectionClass($this);
25-
26-
if (property_exists($this, 'attributes')) {
27-
$attributesProperty = $concreteClass->getProperty('attributes');
28-
$attributesClass = $attributesProperty->getType()->getName();
29-
$this->attributes = new $attributesClass($data['attributes']);
30-
}
31-
32-
if (property_exists($this, 'relationships')) {
33-
$relationshipsData = $data['relationships'];
34-
$concreteClass = new \ReflectionClass($this);
35-
$relationshipsProperty = $concreteClass->getProperty('relationships');
36-
$reflectionType = $relationshipsProperty->getType();
37-
38-
// handle php 8 union types
39-
if ($reflectionType instanceof \ReflectionUnionType) {
40-
$relationshipsClass = (new SuitableRelationshipsTypeDetector())->detect($reflectionType, $relationshipsData);
41-
} else {
42-
$relationshipsClass = $reflectionType->getName();
43-
}
44-
45-
$relationshipsDto = new $relationshipsClass($relationshipsData);
46-
$this->relationships = $relationshipsDto;
47-
}
48-
}
4914
}

src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66
* @deprecated
77
* @see \FreeElephants\JsonApi\DTO\BaseKeyValueStructure
88
*/
9-
class BaseKeyValueStructure
9+
class BaseKeyValueStructure extends \FreeElephants\JsonApi\DTO\BaseKeyValueStructure
1010
{
11-
public function __construct(array $attributes)
12-
{
13-
$concreteClass = new \ReflectionClass($this);
14-
foreach ($attributes as $name => $value) {
15-
$property = $concreteClass->getProperty($name);
16-
if ($property->hasType()) {
17-
$propertyType = $property->getType();
18-
if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) {
19-
$propertyClassName = $propertyType->getName();
20-
$value = new $propertyClassName($value);
21-
}
22-
}
23-
$this->{$name} = $value;
24-
}
25-
}
2611
}

src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* @deprecated
77
* @see \FreeElephants\JsonApi\DTO\RelationshipToOne
88
*/
9-
class RelationshipToOne extends AbstractDocument
9+
class RelationshipToOne extends \FreeElephants\JsonApi\DTO\RelationshipToOne
1010
{
11-
public ResourceIdentifierObject $data;
1211
}

src/FreeElephants/JsonApiToolkit/DTO/ResourceIdentifierObject.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66
* @deprecated
77
* @see \FreeElephants\JsonApi\DTO\ResourceIdentifierObject
88
*/
9-
class ResourceIdentifierObject
9+
class ResourceIdentifierObject extends \FreeElephants\JsonApi\DTO\ResourceIdentifierObject
1010
{
11-
public string $id;
12-
public string $type;
13-
14-
public function __construct(array $data)
15-
{
16-
$this->id = $data['id'];
17-
$this->type = $data['type'];
18-
}
1911
}

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)