Download the PHP package rstgroup/json-api-php without Composer
On this page you can find all versions of the php package rstgroup/json-api-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rstgroup/json-api-php
More information about rstgroup/json-api-php
Files in rstgroup/json-api-php
Package json-api-php
Short Description A PHP library for creating json-api standard responses (see: http://jsonapi.org)
License MIT
Informations about the package json-api-php
json-api-php
It's a lightweight PHP library for creating json-api standard responses. If you want to know more about the json-api standard, please see the details at http://jsonapi.org.
About this doc
This document is not finished yet, it's only a draft for now.
Usage
Quick example
Introduction
As an example we take two resources: "authors" and "posts". Author creates posts; he can have many posts, but every single post is related to only one author.
The first thing that we should do is define our resources.
Defining resources
Every single resource class should extend the (surprise!) Resource
class. However, you don't need to inherit from the Resource
class if you don't want to. For example:
As you can see, every resource requires these three properties to be defined:
- collection name
- name
- href
The first two of them are used by the Writer
class in order to apply naming conventions specified by the standard. "href" is used to prepare full link to a particular resource. Name
stands for name of a single entity. Collection name
stands for plural name of resource entities, while href
should contain url template for a single resource entity.
Ok, now we know what kind of resources we have, what are their names, collection names, and links. You may have noticed that links are set as templates with placeholders, e.g. {authors.id}
. Properly prepared placeholders are used by Writer
to determine which parts should be replaced with values (and for a few other things, too). It will be helpful with more complex links, for example: /authors/{authors.id}/posts/{posts.id}
.
It's important to know that a placeholder MUST have a form like this: {_RESOURCE_COLLECTIONNAME.id}
Now it's time for entities.
Defining entities
The very first entity we would like to create is Author
. As you can see below, the Author
class implements EntityInterface
. It means that two methods should be implemented: getId()
and toArray()
.
getId()
returns id of a particular entity and it's used in every place in the library where that id should be known.
toArray()
should return those properties of an Author
class that are considered resource fields in the response representation. In the Author
class these will be firstName
and lastName
, as id
will be added automatically.
Now we're ready to prepare our first json-api standard result:
And the result looks like this:
So, we have an authors list that could be achieved via, for example http://api.example.com/authors
link, but what about related resources like posts
? Well, for that we need to define resource relations.
Adding resource relations
One-to-one
As it was said earlier, every single post can have a relation to just one author. Let's say that we have two posts, added by the same author. For that we need to modify the Post
class a little:
What we've just done here was adding the $author
property and its setter and getter methods. Author should be an instance of the Author
entity.
Hint: we DID NOT add our
$author
property to the list of entity fields returned by thetoArray()
method, because$author
has only one purpose: to hold data of author related to the post.
Setter is not as important as getter---we could use constructor for that as well---but getter method in this case MUST be named exactly getAuthor()
. Why? Because Writer
will automatically determine the name of a needed getter method based on:
- relation type (one-to-one/one-to-many)
- resource name/collection name (here: author/authors)
In our case one post is related to one author, so the relation type is one-to-one
. Therefore, the "author" resource name will be used as the name of the getter method. In case of a one-to-many
relation (like author having many posts), collection name ("posts") of the Post
resource will be used. Because of that, getPosts()
from the Author
entity class will serve as the getter method.
Anyway, we create relation between post and its author like this:
And the result is:
As you can see, new fields called links
were added to the result in case of every post entity.
Field links
contains short info about author related to a particular post. It could be returned in two (three for one-to-many relation type) forms: either as an id or as an object. The latter takes place by default, in order to change this behaviour you need to call:
The result will be:
Or you could just---for some strange reasons---switch that off:
The result will be:
One-to-many
For now we know how to define a one-to-one
relation (post has an author), but what about one-to-many
relation (author has posts)? This is how it should be done in case of the Author
class:
... and next:
So the result looks like this:
Embedding related resources data
As mentioned in the previous section, adding relation to a resource causes the links
field to appear in every entity. However, if you want data on related resource to be embedded in response representation, you should fill entity object up with proper values, and turn on attaching linked
objects to representation, because it's disabled by default.
In our case filling up is already done as we've set all the properties earlier via constructor:
So there's only one thing left to do:
As we can see, the top-level linked
field contains entities with data on resources related to the author resource:
Url templates
Url templates can be used to describe URL format for resources according to their type. You can add your url templates like this:
TODO's
- support for 'meta'.