$ composer require germania-kg/response-decoder
The JsonApiResponseDecoder aims at responses adhering to the JSON:API standard. It implements the Psr\Http\Message\ResponseInterface
and provides two public public methods:
getResource( ResponseInterface $response) : array
getResourceCollection( ResponseInterface $response) : array
<?php
use Germania\ResponseDecoder\ResponseDecoderInterface;
use Germania\ResponseDecoder\JsonApiResponseDecoder;
$response = ...
$decoder = new JsonApiResponseDecoder;
// Both are arrays
$resourceCollection = $decoder->getResourceCollection($response);
$singleResource = $decoder->getResource($response);
Given this collection of items, each of which is an object within the data
element.
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!"
}
}, {
"type": "articles",
"id": "2",
"attributes": {
"title": "Rails is Omakase"
}
}]
}
The JsonApiResponseDecoder will now collect each object from the data
element and extract the attributes
:
<?php
use Germania\ResponseDecoder\JsonApiResponseDecoder;
$response = ...
$items = (new JsonApiResponseDecoder)->getResourceCollection($response);
print_r($items);
// Array {
// Array {
// "title" => "JSON:API paints my bikeshed!"
// },
// Array {
// "title" => "Rails is Omakase"
// }
// }
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles/1"
},
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"related": "http://example.com/articles/1/author"
}
}
}
}
}
The JsonApiResponseDecoder will now extract the attributes
from the data
element:
<?php
use Germania\ResponseDecoder\JsonApiResponseDecoder;
$response = ...
$item = (new JsonApiResponseDecoder)->getResource($response);
print_r($item);
// Array {
// "title" => "JSON:API paints my bikeshed!"
// }
$ git clone git@github.com:GermaniaKG/ResponseDecoder.git
$ cd ResponseDecoder
$ composer install
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test
# or
$ vendor/bin/phpunit