2017 © Pedro Peláez
 

library json-api-mapper

Mapper response json-api in PHP

image

freddiegar/json-api-mapper

Mapper response json-api in PHP

  • Saturday, May 5, 2018
  • by freddiegar
  • Repository
  • 1 Watchers
  • 1 Stars
  • 151 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 9 % Grown

The README.md

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: Build Status develop: Build Status v1.0.0: Build Status, (*3)

Requisites

  • php >= 7.1.3

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

Performance

, (*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)

The Versions

05/05 2018

dev-master

9999999-dev

Mapper response json-api in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api json mapper mapping map jsonapi json-api jsonapi.org

05/05 2018

dev-develop

dev-develop

Mapper response json-api in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api json mapper mapping map jsonapi json-api jsonapi.org

05/05 2018

v1.0.0

1.0.0.0

Mapper response json-api in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api json mapper mapping map jsonapi json-api jsonapi.org