Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}-
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/FreeElephants/JsonApiToolkit/DTO/AbstractAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace FreeElephants\JsonApiToolkit\DTO;

abstract class AbstractAttributes extends BaseKeyValueStructure
/**
* @deprecated
* @see \FreeElephants\JsonApi\DTO\AbstractAttributes
*/
abstract class AbstractAttributes extends \FreeElephants\JsonApi\DTO\AbstractAttributes
{
}
26 changes: 3 additions & 23 deletions src/FreeElephants/JsonApiToolkit/DTO/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,10 @@
use Psr\Http\Message\MessageInterface;

/**
* @deprecated
* @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);
}
}
12 changes: 5 additions & 7 deletions src/FreeElephants/JsonApiToolkit/DTO/AbstractRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace FreeElephants\JsonApiToolkit\DTO;

abstract class AbstractRelationships
/**
* @deprecated
* @see \FreeElephants\JsonApi\DTO\AbstractRelationships
*/
abstract class AbstractRelationships extends \FreeElephants\JsonApi\DTO\AbstractRelationships
{
public function __construct(array $data)
{
foreach ($data as $relationshipName => $relationshipsData) {
$this->{$relationshipName} = new RelationshipToOne($relationshipsData);
}
}
}
40 changes: 4 additions & 36 deletions src/FreeElephants/JsonApiToolkit/DTO/AbstractResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,10 @@
use FreeElephants\JsonApiToolkit\DTO\Reflection\SuitableRelationshipsTypeDetector;

/**
* @property AbstractAttributes $attributes
* @property AbstractRelationships $relationships
*
* @deprecated
* @see \FreeElephants\JsonApi\DTO\AbstractResourceObject
*/
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;
}
}
}
21 changes: 5 additions & 16 deletions src/FreeElephants/JsonApiToolkit/DTO/BaseKeyValueStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@

namespace FreeElephants\JsonApiToolkit\DTO;

class BaseKeyValueStructure
/**
* @deprecated
* @see \FreeElephants\JsonApi\DTO\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;
}
}
}
7 changes: 5 additions & 2 deletions src/FreeElephants/JsonApiToolkit/DTO/RelationshipToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace FreeElephants\JsonApiToolkit\DTO;

class RelationshipToOne extends AbstractDocument
/**
* @deprecated
* @see \FreeElephants\JsonApi\DTO\RelationshipToOne
*/
class RelationshipToOne extends \FreeElephants\JsonApi\DTO\RelationshipToOne
{
public ResourceIdentifierObject $data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace FreeElephants\JsonApiToolkit\DTO;

class ResourceIdentifierObject
/**
* @deprecated
* @see \FreeElephants\JsonApi\DTO\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'];
}
}
2 changes: 1 addition & 1 deletion tests/FreeElephants/JsonApiToolkit/DTO/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

class OneRelationships extends AbstractRelationships
{
public RelationshipToOne $one;
public \FreeElephants\JsonApi\DTO\RelationshipToOne $one;
}