Skip to content

cjessamy/json-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:https://jsonapi.org) for PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%