Skip to content

Collections support #2

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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: CI

on: push
on:
- push
- pull_request

jobs:
php-tests:
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.2] - 2025-03-14

### Added
- AbstractCollection for handle array of resources in data field
- Exception on pass array as data into AbstractDocument constructor

## [0.0.1] - 2025-03-13

### 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.1...HEAD
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.2...HEAD
[0.0.2]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.2
[0.0.1]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.1
19 changes: 19 additions & 0 deletions src/FreeElephants/JsonApi/DTO/AbstractCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO;

abstract class AbstractCollection extends TopLevel
{
public array $data = [];

final public function __construct(array $payload = [])
{
foreach ($payload['data'] as $item) {
$dataItemClassName = $this->getDataItemClassName();
$this->data[] = new $dataItemClassName($item);
}
}

abstract public function getDataItemClassName(): string;
}
24 changes: 8 additions & 16 deletions src/FreeElephants/JsonApi/DTO/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,20 @@
/**
* @property AbstractResourceObject|mixed $data
*/
abstract class AbstractDocument
abstract class AbstractDocument extends TopLevel
{
final public function __construct(array $data)
final public function __construct(array $payload)
{
$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);
if ($dataClassName !== 'array') {
$data = new $dataClassName($payload['data']);
} else {
throw new \UnexpectedValueException('`data` property must be typed, for array of resources use AbstractCollection instead ' . self::class);
}
$this->data = $data;
}
}
25 changes: 25 additions & 0 deletions src/FreeElephants/JsonApi/DTO/TopLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO;

use Psr\Http\Message\MessageInterface;

/**
* @property AbstractResourceObject|AbstractResourceObject[] $data
*/
abstract class TopLevel
{
/**
* @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);
}
}
50 changes: 50 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO;

use FreeElephants\JsonApi\AbstractTestCase;
use FreeElephants\JsonApi\DTO\Example\SomeCollectionDocument;
use Nyholm\Psr7\ServerRequest;

class CollectionTest extends AbstractTestCase
{
public function testCollectionFromHttpMessage(): void
{
$request = new ServerRequest('GET', '/foos');
$request->getBody()->write(<<<JSON
{
"data": [
{
"id": "123",
"type": "foo",
"attributes": {
"foo": "bar",
"date": "2012-04-23T18:25:43.511Z",
"nested": {
"someNestedStructure": {
"someKey": "someValue"
}
}
},
"relationships": {
"baz": {
"data": {
"type": "bazs",
"id": "baz-id"
}
}
}
}
]
}
JSON
);

$collectionDto = SomeCollectionDocument::fromHttpMessage($request);

$this->assertCount(1, $collectionDto->data);
$this->assertSame('123', $collectionDto->data[0]->id);
$this->assertSame('foo', $collectionDto->data[0]->type);
}
}
19 changes: 19 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/Example/SomeCollectionDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace FreeElephants\JsonApi\DTO\Example;

use FreeElephants\JsonApi\DTO\AbstractCollection;
use FreeElephants\JsonApi\DTO\AbstractResourceObject;

class SomeCollectionDocument extends AbstractCollection
{
public function getDataItemClassName(): string
{
return DataItemResourceObject::class;
}
}

class DataItemResourceObject extends AbstractResourceObject
{
}