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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-api-php

json-api-php

Made with passion - RST Group Code Climate Build Status Test Coverage

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:

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 the toArray() 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:

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


All versions of json-api-php with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rstgroup/json-api-php contains the following files

Loading the files please wait ....