floor9design/json-api-formatter

A set classes that allow for creating JSON API compliant objects

1.3.1 2023-02-25 19:09 UTC

README

Latest Version Packagist Software License

Build Status Build Status

Github Downloads Packagist Downloads

A set of classes that allow for creating JSON API compliant objects

Introduction

The JSON API has a precise response format. This set of classes allows these to be easily created and interrogated, meaning easy and reliable output/input processing.

Output is as simple as:

use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;

// Some example data:
$id = "2"; 
$type = 'test';
$attributes = ['test' => 'data'];

$json_api_response = new JsonApiFormatter();

$response = $json_api_response->dataResourceResponse($id, $type, $attributes); 
// a json-api string, good for direct output 

Other responses and elements, such as errors, meta data, links and source elements are all supported.

For more examples, see usage.

Features

Latest Version Packagist

The classes offer:

  • easy methods to wrap and unwrap content
  • default values and settings out of the box, improving the quality of response
  • fluent programming approach, allowing objects to be built on the fly

Install

Via Composer/packagist

Packagist Downloads

composer require floor9design/json-api-formatter

Via git

Github Downloads

git clone https://github.com/floor9design-ltd/json-api-formatter.git

Or:

git clone git@github.com:floor9design-ltd/json-api-formatter.git

Usage

This is discussed in the usage document.

Setup

There are no specific config setup steps required. The class should autoload in PSR-4 compliant systems. If you are using the class on its own, simply include it however is most appropriate.

Testing

Build Status Build Status

Tests can be run as follows:

  • ./vendor/phpunit/phpunit/phpunit

Static analysis/code review can be performed by using phpstan:

  • ./vendor/bin/phpstan

The following tests and also creates code coverage (usually maintained at 100%)

  • ./vendor/phpunit/phpunit/phpunit --coverage-html docs/tests/

Note, the following are very useful for validating/testing outputs:

Credits

Changelog

A changelog is generated here:

License

This software is available under the MIT licence.