Skip to content

fennik/json-api-normalizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-api-normalizer

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;

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages