cjessamy / json-api

Implementation of JSON API (http://jsonapi.org) for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implementation of JSON API in PHP

This library is an attempt to express business rules of JSON API specification in a set of PHP 7 classes.

While it is still in 0.* versions, there are no major changes expected in the internal API.

A simple example to illustrate the general idea. This (slightly modified) JSON representation from the documentation

{
    "data": {
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "Rails is Omakase"
        },
        "relationships": {
            "author": {
                "data": {
                    "type": "people",
                    "id": "9"
                },
                "links": {
                    "self": "\/articles\/1\/relationships\/author",
                    "related": "\/articles\/1\/author"
                }
            }
        }
    }
}

can be built with the following php code:

$author = Relationship::fromLinkage(
    Linkage::fromSingleResourceId(
        new ResourceId('people', '9')
    )
);
$author->setLink('self', '/articles/1/relationships/author');
$author->setLink('related', '/articles/1/author');

$articles = new ResourceObject('articles', '1');
$articles->setRelationship('author', $author);
$articles->setAttribute('title', 'Rails is Omakase');

echo json_encode(Document::fromData($articles), JSON_PRETTY_PRINT);

Please refer to the tests for the full API documentation:

About

Implementation of JSON API (http://jsonapi.org) for PHP

License:MIT License


Languages

Language:PHP 100.0%