Skip to content

vendelev/json-api

 
 

Repository files navigation

Implementation of JSON API in PHP 7

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 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 (less imports):

<?php
$articles = new ResourceObject('articles', '1');
$author = Relationship::fromLinkage(
    new SingleLinkage(
        new ResourceIdentifier('people', '9')
    )
);
$author->setLink('self', '/articles/1/relationships/author');
$author->setLink('related','/articles/1/author');
$articles->setRelationship('author', $author);
$articles->setAttribute('title', 'Rails is Omakase');
$doc = Document::fromResource($articles);
echo json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

Please refer to the tests for the full API documentation:

Installation

With composer: json-api-php/json-api.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%