2017 © Pedro Peláez
 

library json-api

A response JSON API library for PHP

image

sarbinski/json-api

A response JSON API library for PHP

  • Thursday, December 14, 2017
  • by sarb
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

JSON API

Examples

Example 1 - create JSON API data object from an array

<?php
require_once __DIR__ . '/vendor/autoload.php';


// Data loaded from database(for example)
$data = [
    ['id' => '1', 'name' => 'Foo'],
    ['id' => '2', 'name' => 'Bar'],
];

$api = new Sarbinski\JsonApi\Api();
$api->createData($dataObject, 'Sarbinski\JsonApi\Schemas\TestSchema');
echo $api->buildMainJSONStructure()->getPrintData(true);

will output:, (*1)

{
    "data": [
        {
            "type": "test-schema",
            "id": "1",
            "attributes": {
                "name": "1_Foo"
            }
        },
        {
            "type": "test-schema",
            "id": "2",
            "attributes": {
                "name": "2_Bar"
            }
        }
    ]
}

The TestSchema is an Adapter that extracts attributes for given entity, (*2)

<?php
class TestSchema extends Schema
{
    protected $type = 'test-schema';

    public function getAttributes($resource)
    {
        return [
            'name' => $resource['id'] . '_' . $resource['name'],
        ];
    }

    public function getId($resource)
    {
        return $resource['id'];
    }

    public function getRelationships($resource)
    {
        return false;
    }
}

Example 2 - create JSON API data object from Traversable object

Again we use TestSchema as a Schema adapter, (*3)

<?php
class DataClass implements Iterator {
    private $items = [];

    public function __construct()
    {
        $this->items = [
            ['id' => 1, 'name' => 'a'],
            ['id' => 2, 'name' => 'b'],
        ];
    }

    public function current()
    {
        return current($this->items);
    }

    public function key()
    {
        return key($this->items);
    }

    public function next()
    {
        return next($this->items);
    }

    public function rewind()
    {
        return reset($this->items);
    }

    public function valid()
    {
        return key($this->items) !== null;
    }
}

$dataObject = new DataClass();
$api->createData($dataObject, 'Sarbinski\JsonApi\Schemas\TestSchema');
echo $api->buildMainJSONStructure()->getPrintData(true);

will output:, (*4)

{
    "data": [
        {
            "type": "test-schema",
            "id": 1,
            "attributes": {
                "name": "1_a"
            }
        },
        {
            "type": "test-schema",
            "id": 2,
            "attributes": {
                "name": "2_b"
            }
        }
    ]
}

Auto loading of Schema classes

This lib is PSR-4 compatible. You can put your Schema classes anywhere as long as you have a proper autoloading registered., (*5)

The Versions

14/12 2017

dev-master

9999999-dev https://github.com/Sarbinski/json-api

A response JSON API library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Stanislav Sarbinski

30/10 2017

0.1

0.1.0.0 https://github.com/Sarbinski/json-api

A response JSON API library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Stanislav Sarbinski