Skip to content

coderm/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:

<?php
use \JsonApiPhp\JsonApi\Document;
use \JsonApiPhp\JsonApi\Document\Resource\{
    Linkage\SingleLinkage, Relationship, ResourceIdentifier, ResourceObject
};

$author = Relationship::fromLinkage(new SingleLinkage(new ResourceIdentifier('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::fromResource($articles), 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 in PHP 7

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%