2017 © Pedro Peláez
 

library jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

image

alsvanzelf/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  • Thursday, January 18, 2018
  • by lode
  • Repository
  • 4 Watchers
  • 26 Stars
  • 9,710 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 5 Open issues
  • 19 Versions
  • 11 % Grown

The README.md

jsonapi Build Status Scrutinizer Code Quality Code Coverage

A simple and human-friendly library for api servers (php serving json)., (*1)

It allows you to generate json output according to the JSON:API v1.1 standard, while being easy to understand for people without knowledge of the jsonapi standard., (*2)

The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them. Read more about it at jsonapi.org., (*3)

Installation

Use Composer require to get the latest stable version:, (*4)

composer require alsvanzelf/jsonapi

The library supports, and is is tested on, php versions 5.6, 7 and 8., (*5)

Upgrading from v1

If you used v1 of this library, see UPGRADE_1_TO_2.md on how to upgrade., (*6)

Getting started

A small resource example

use alsvanzelf\jsonapi\ResourceDocument;

$document = new ResourceDocument($type='user', $id=42);
$document->add('name', 'Zaphod Beeblebrox');
$document->add('heads', 2);
$document->sendResponse();

Which will result in:, (*7)

{
    "jsonapi": {
        "version": "1.1"
    },
    "data": {
        "type": "user",
        "id": "42",
        "attributes": {
            "name": "Zaphod Beeblebrox",
            "heads": 2
        }
    }
}

A collection of resources with relationships

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;

$arthur      = new ResourceObject('user', 1);
$ford        = new ResourceObject('user', 2);
$zaphod      = new ResourceObject('user', 42);
$heartOfGold = new ResourceObject('starship', 2001);

$arthur->add('name', 'Arthur Dent');
$ford->add('name', 'Ford Prefect');
$zaphod->add('name', 'Zaphod Beeblebrox');
$heartOfGold->add('name', 'Heart of Gold');

$zaphod->addRelationship('drives', $heartOfGold);

$users    = [$arthur, $ford, $zaphod];
$document = CollectionDocument::fromResources(...$users);
$document->sendResponse();

Which will result in:, (*8)

{
    "jsonapi": {
        "version": "1.1"
    },
    "data": [
        {
            "type": "user",
            "id": "1",
            "attributes": {
                "name": "Arthur Dent"
            }
        },
        {
            "type": "user",
            "id": "2",
            "attributes": {
                "name": "Ford Prefect"
            }
        },
        {
            "type": "user",
            "id": "42",
            "attributes": {
                "name": "Zaphod Beeblebrox"
            },
            "relationships": {
                "drives": {
                    "data": {
                        "type": "starship",
                        "id": "2001"
                    }
                }
            }
        }
    ],
    "included": [
        {
            "type": "starship",
            "id": "2001",
            "attributes": {
                "name": "Heart of Gold"
            }
        }
    ]
}

Turning an exception into jsonapi

use alsvanzelf\jsonapi\ErrorsDocument;

$exception = new Exception('That is not valid', 422);

$document = ErrorsDocument::fromException($exception);
$document->sendResponse();

Which will result in:, (*9)

{
    "jsonapi": {
        "version": "1.1"
    },
    "errors": [
        {
            "status": "422",
            "code": "Exception",
            "meta": {
                "class": "Exception",
                "message": "That is not valid",
                "code": 422,
                "file": "README.md",
                "line": 137,
                "trace": []
            }
        }
    ]
}

This can be useful for development. For production usage, you can better construct an ErrorsDocument with only specific values., (*10)

Using extensions and profiles

The Atomic Operations extension and the Cursor Pagination profile come packaged along. Any 3rd party of self-made extension can be applied with:, (*11)

use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;

class ExampleExtension implements ExtensionInterface {
    public function getOfficialLink() {
        return 'https://example.org/extension-documentation';
    }

    public function getNamespace() {
        return 'foo';
    }
}

$document = new ResourceDocument('user', 42);
$document->add('name', 'Zaphod Beeblebrox');

$extension = new ExampleExtension();
$document->applyExtension($extension);
$document->addExtensionMember($extension, 'bar', 'baz');

$document->sendResponse();

Which will result in:, (*12)

{
    "foo:bar": "baz",
    "jsonapi": {
        "version": "1.1",
        "ext": [
            "https://example.org/extension-documentation"
        ]
    },
    "data": {
        "type": "user",
        "id": "42",
        "attributes": {
            "name": "Zaphod Beeblebrox"
        }
    }
}

A similar flow can be used for profiles., (*13)

Other examples

Examples for all kind of responses are in the /examples directory., (*14)

Features

This library supports v1.1 of the JSON:API specification., (*15)

It has support for generating & sending documents with:, (*16)

  • single resources
  • resource collections
  • to-one and to-many relationships
  • errors (easily turning exceptions into jsonapi output)
  • v1.1 extensions and profiles
  • v1.1 @-members for JSON-LD and others

Also there's tools to help processing of incoming requests:, (*17)

  • parse request options (include paths, sparse fieldsets, sort fields, pagination, filtering)
  • parse request documents for creating, updating and deleting resources and relationships

Next to custom extensions/profiles, the following official extensions/profiles are included:, (*18)

Plans for the future include:, (*19)

  • validate request options (#58)
  • validate request documents (#57)

Contributing

If you use the library, please ask questions or share what can be improved by creating an issue., (*20)

For bugs issues or Pull Requests are welcome!, (*21)

Licence

MIT, (*22)

The Versions

18/01 2018

1.5.1.x-dev

1.5.1.9999999-dev https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

18/01 2018

v1.5.1

1.5.1.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

18/01 2018

dev-relation-free-format

dev-relation-free-format https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

30/06 2017

dev-master

9999999-dev https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

30/06 2017

v1.5.0

1.5.0.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

23/03 2017

v1.4.1

1.4.1.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

23/03 2017

v1.4.0

1.4.0.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

23/03 2017

dev-pass-meta-to-collection

dev-pass-meta-to-collection https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

09/08 2016

v1.3.1

1.3.1.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

15/06 2016

v1.3.0

1.3.0.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

23/01 2016

dev-separate-link-meta

dev-separate-link-meta https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

23/01 2016

v1.2.1

1.2.1.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

19/07 2015

v1.2.0

1.2.0.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

09/07 2015

v1.1.2

1.1.2.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

17/06 2015

v1.1.1

1.1.1.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

15/06 2015

v1.1.0

1.1.0.0 https://github.com/lode/jsonapi

Simple and human friendly library for api servers (PHP serving out JSON)

  Sources   Download

MIT

The Requires

  • php >=5.4

 

api json jsonapi

12/06 2015

v1.0.2

1.0.2.0 https://github.com/lode/jsonapi

Simple and small http://jsonapi.org library for php servers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

jsonapi

12/06 2015

v1.0.1

1.0.1.0 https://github.com/lode/jsonapi

Simple and small http://jsonapi.org library for php servers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

jsonapi

09/06 2015

v1.0.0

1.0.0.0 https://github.com/lode/jsonapi

Simple and small http://jsonapi.org library for php servers

  Sources   Download

MIT

The Requires

 

jsonapi