floor9design / json-api-formatter
A set classes that allow for creating JSON API compliant objects
Installs: 21 665
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
Requires (Dev)
- floor9design/testing-tools: ^1.3
- mockery/mockery: dev-master
- phpstan/phpstan: ^1.0.0
- phpunit/phpunit: ^10|^9
This package is auto-updated.
Last update: 2024-10-28 02:17:27 UTC
README
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
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
composer require floor9design/json-api-formatter
Via git
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
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.