Search by

webservco / jsonapi

ariademur

A PHP component/library. Interfaces for building JSON:API documents, plus the jsonapi member object.

Package info

github.com/webservco/jsonapi

Homepage

pkg:composer/webservco/jsonapi

Statistics

Installs: 77

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v2.3.0 2026-10-11 16:34 UTC

This package is auto-updated.

Last update: 2026-10-11 16:45:40 UTC


README

A PHP component/library. Please see webservco/sandbox for an example project using these types of components.

Interfaces for building JSON:API documents, plus the jsonapi member object.

  • Related: webservco/jsonapi-application (request handling, views and the request service implementation), webservco/view (JSONAPIRenderer)

Installation

composer require webservco/jsonapi

Usage

Your resource objects implement these interfaces. The renderer encodes public properties, and their names become the JSON member names.

use WebServCo\JSONAPI\Contract\Document\DataInterface;
use WebServCo\JSONAPI\Contract\Document\MetaInterface;

final readonly class ItemData implements DataInterface
{
    public string $type;

    public function __construct(public int $id, public ItemAttributes $attributes, public ?MetaInterface $meta)
    {
        $this->type = 'item';
    }
}

Put it in a document view (ItemView or CollectionView from webservco/jsonapi-application) together with the jsonapi member:

use WebServCo\JSONAPI\DataTransfer\Document\JSONAPI;

// {"jsonapi": {"version": "1.1"}, "data": {...}}
$view = new ItemView(new JSONAPI('1.1'), $itemData, null, $documentMeta);

Contracts

Interface Implementation Purpose
Contract\Document\JSONAPIInterface DataTransfer\Document\JSONAPI The jsonapi member, and the application/vnd.api+json media type constant.
Contract\Document\DataInterface yours Primary data of a document.
Contract\Data\ItemInterface yours One item in a collection.
Contract\Document\MetaInterface yours A meta object.
Contract\Errors\ErrorInterface DefaultError (webservco/jsonapi-application) An entry in errors.
Contract\Service\JSONAPIRequestServiceInterface JSONAPIRequestService (webservco/jsonapi-application) Checks content type and version, decodes the body.

Namespaces are relative to WebServCo\JSONAPI\.

Development

# Start project.
ddev start
# Install dependencies.
ddev c u
# Run all validations.
ddev c c
# Run unit tests.
ddev c test:dox

License

MIT