freddiegar/json-api-mapper

Mapper response json-api in PHP

v1.0.0 2018-05-05 15:53 UTC

This package is auto-updated.

Last update: 2024-04-22 07:25:33 UTC


README

It is a mapper in PHP from response jsonapi.org.

This library create a object from response json-api. Access to elements in response easily

Status Branch

master: Build Status develop: Build Status v1.0.0: Build Status

Requisites

  • php >= 7.1.3

Install

composer require freddiegar/json-api-mapper

Usage

Creating instance of Mapper, see $jsonApiResponse here

use FreddieGar\JsonApiMapper\JsonApiMapper;

$jsonApi = new JsonApiMapper($jsonApiResponse);

$data = $jsonApi->getData(0);
$included = $jsonApi->getIncluded();

By example, get data resource

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

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

$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

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

Performance

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

Response Used In Example

You can see all example here

Response json-api Resource used in this example

{
  "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

{
  "errors": [
      {
        "status": "422",
        "source": { "pointer": "/data/attributes/first-name" },
        "title":  "Invalid Attribute",
        "detail": "First name must contain at least three characters."
      }
    ]
}

License

MIT