Skip to content

json-api-php/json-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Implementation of JSON API in PHP

WARNING! Work in progress! The internal library API is not stable yet!

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

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: