jacobfennik/json-api-normalizer

Simple package to handle response from JSON:API

v0.1-beta.1 2018-12-12 01:10 UTC

This package is auto-updated.

Last update: 2024-09-12 13:19:11 UTC


README

Utility to normalize and build JSON:API response data.

Install

$ composer require jacobfennik/json-api-normalizer

Example

Building with the normalizer

<?php

use JacobFennik\JsonApiNormalizer\Normalizer;

$apiResponse = '
{
    "data": [{
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "JSON:API paints my bikeshed!"
        },
        "relationships": {
            "author": {
                "data": { "type": "people", "id": "9" }
            },
        }
    }],
    "included": [{
        "type": "people",
        "id": "9",
        "attributes": {
            "firstName": "Dan",
            "lastName": "Gebhardt",
            "twitter": "dgeb"
        },
    }]
}
';

$normalizer = new Normalizer($apiResponse);
$built = $normalizer->build();

Accessing built data

<?php
$normalizer = new Normalizer($apiResponse);

$article = $normalizer->build(1); // Build object with id '1' 

$title = $article->title;
$authorFirstName = $article->author->firstName;