JSON Api Mapper
It is a mapper in PHP from response jsonapi.org., (*1)
This library create a object from response json-api. Access to elements in response easily, (*2)
Status Branch
master
:
develop
:
v1.0.0
:
, (*3)
Requisites
Install
composer require freddiegar/json-api-mapper
Usage
Creating instance of Mapper, see $jsonApiResponse here, (*4)
use FreddieGar\JsonApiMapper\JsonApiMapper;
$jsonApi = new JsonApiMapper($jsonApiResponse);
$data = $jsonApi->getData(0);
$included = $jsonApi->getIncluded();
By example, get data resource, (*5)
echo $data->getType(); // articles
echo $data->getId(); // 1
echo print_r($data->getAttributes(), true); // ['title' => 'JSON API paints my bikeshed!', 'body' => '...']
echo $data->getAttribute('created'); // 2015-05-22T14:56:29.000Z
echo $data->getAttribute('description'); // If not exist, return: null
echo print_r($data->getRelationships(), true); // ['author' => ['id' => '1', 'type' => 'people']]
echo get_class($data->getRelationship('author')); // return DataMapperInterface
echo $data->getRelationship('author')->getType(); // people
echo $data->getRelationship('author')->getId(); // 1
By example, get included, (*6)
echo get_class($included->getIncluded(0)); // return DataMapperInterface
echo $included->getIncluded(0)->getType(); // people
echo $included->getIncluded(0)->getId(); // 42
echo $included->getIncluded(0)->getName(); // John
echo $included->getIncluded(1); // null, it is not defined in response
By example, get errors, see $jsonApiResponse here, (*7)
$jsonApi = new JsonApiMapper($jsonApiResponse);
echo get_class($jsonApi->getErrors()); // Return ErrorsMapperInterface
$firstError = $jsonApi->getErrors(0); // Get first error
echo $firstError->getStatus(); // 422
echo print_r($firstError->getSource(), true); // ['pointer' => '/data/attributes/first-name']
echo $firstError->getTitle(); // Invalid Attribute
echo $firstError->getDetail(); // First name must contain at least three characters.
$secondError = $jsonApi->getErrors(1); // null, it is not defined in response
Find
Get data with id
= 2
$dataWithIdTwo = $data->find(2); // Return DataMapperInterface if exist else null
Get included by type
= people
$dataPeople = $included->find('people'); // Return DataMapperInterface if exist else null
Get included with type
= people and id
= 3
$dataPeopleWithIdThree = $included->find('people', 3); // Return DataMapperInterface if exist else null
// OR
$peopleWithIdThree = $dataPeople->find(3); // Return DataMapperInterface if exist else null
Alias in JsonApiResponse class
You can use any option to access to data in that response, (*8)
Method*
|
Alias |
Property |
Description |
getData() |
data() |
data |
Return object DataMapper if exists in response, else null |
getErrors() |
errors() |
errors |
Return object ErrorsMapper if exists in response, else null |
getMeta() |
meta() |
meta |
Return object MetaMapper if exists in response, else null |
getJsonApi() |
jsonapi() |
jsonapi |
Return object JsonApiMapper if exists in response, else null |
getIncluded() |
included() |
included |
Return object IncludedMapper if exists in response, else null |
getLinks() |
links() |
links |
Return object LinksMapper if exists in response, else null |
, (*9)
You will prefer to use get* (getData(), getErrors()) methods accessors, they are direct call, any other ways are overloading (__call
and __get
), this are slower, (*10)
Response Used In Example
You can see all example here, (*11)
Response json-api Resource used in this example
, (*12)
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!",
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"data": {"id": "42", "type": "people"}
}
}
}],
"included": [
{
"type": "people",
"id": "42",
"attributes": {
"name": "John",
"age": 80,
"gender": "male"
}
}
]
}
Response json-api Errors used in this example
, (*13)
{
"errors": [
{
"status": "422",
"source": { "pointer": "/data/attributes/first-name" },
"title": "Invalid Attribute",
"detail": "First name must contain at least three characters."
}
]
}
License
MIT, (*14)