2017 © Pedro Peláez
 

library php-hal

REST HAL library as a C-extension for PHP

image

brandonlamb/php-hal

REST HAL library as a C-extension for PHP

  • Saturday, November 29, 2014
  • by brandonlamb
  • Repository
  • 1 Watchers
  • 3 Stars
  • 16 Installations
  • C
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

php-hal

PHP library for representing HAL resources for REST API, (*1)

Build
Status Latest Stable Version Total Downloads, (*2)

PhpHal Build Status

PhpHal is a library for representing and consuming resources in different hypermedia formats., (*3)

A resource in a [HATEOAS API] (http://en.wikipedia.org/wiki/HATEOAS) must describe its own capabilities and interconnections, which is the third level of Three Levels of the REST Maturity Model, (*4)

Why Hypermedia?

As you can read in the prologue of Designing Hypermedia APIs book:, (*5)

Hypermedia APIs embrace the principles that make the web great: flexibility, standardization, and loose coupling to any given service. They take into account the principles of systems design enumerated by Roy Fielding in his thesis, but with a little less sytems theory jargon., (*6)

Hypermedia designs scale better, are more easily changed and promote decoupling and encapsulation, with all the benefits those things bring. On the downside, it is not necessarily the most latency-tolerant design, and caches can get stale if you're not careful. It may not be as efficient on an individual request level as other designs., (*7)

-- Steve Klabnik, (*8)

Which Hypermedia specification should I use?

Hypermedia is being defined these days. Only the best APIs implement Hypermedia. Currently there is no de facto standard, so you must choose between the differents specifications., (*9)

PhpHal currenly implements or is planned to implement these specifications: * HAL: This is the most common and active. It has a JSON and a XML version. * Siren: Currently being defined. It implements some useful things like actions, classes, etc. * Collection+JSON: This is fully designed to be a CRUD oriented API., (*10)

Requirements

  • PHP 5.4.x

Installation

The recommended way to install PhpHal is through composer. You can see the package information on Packagist., (*11)

{
    "require": {
        "brandonlamb/php-hal": "dev-master"
    }
}

Examples

Writer

use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$resource = new Resource();
$resource->setURI('/foo');
$resource->setLink('foo', new Link('/bar'));
$resource->setData([
    'foo' => 'bar',
    'baz' => 'qux'
]);

$writer = new Hal\JsonWriter(true);
echo $writer->execute($resource);
{
    "foo": "bar",
    "baz": "qux",
    "_links": {
        "self": {
            "href": "/foo"
        },
        "foo": {
            "href": "/bar"
        }
    }
}

Resource with embedded resources as aapplication/vnd.siren+json

use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Siren;

$resource = new Resource();
$resource->setRepositoryKey('index');
$resource->setURI('/index?page=2');
$resource->setLink('prev', new Link('/index?page=1'));
$resource->setLink('next', new Link('/index?page=3'));
$resource->addData('count', 5);

$subresource = [];
foreach (range(1, 5) as $value) {
    $subresource = new Resource();
    $subresource->addData('value', $value);

    $subresources[] = $subresource;
}

$resource->addResources('subresources', $subresources);

$writer = new Siren\JsonWriter(true);
echo $writer->execute($resource);
{
    "class": [
        "index"
    ],
    "properties": {
        "count": 5
    },
    "entities": [
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 1
            }
        },
        ...
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 5
            }
        }
    ],
    "links": [
        {
            "rel": "self",
            "href": "/index?page=2"
        },
        {
            "rel": "prev",
            "href": "/index?page=1"
        },
        {
            "rel": "next",
            "href": "/index?page=3"
        }
    ]
}

Resource with linked resource as application/hal+xml

use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$author = new Resource();
$author->setURI('/john-doe');
$author->setTitle('John Doe');

$article = new Resource();
$article->setURI('/lorem-ipsum');
$article->addData('description', 'Lorem ipsum dolor sit amet ...');
$article->linkResource('author', $author);

$writer = new Hal\XmlWriter(true);
echo $writer->execute($article);
<?xml version="1.0"?>
<resource href="/lorem-ipsum">
  <description>Lorem ipsum dolor sit amet ...</description>
  <link rel="author" href="/john-doe" title="John Doe"/>
</resource>

Reader

use PhpHal\Format\Reader\Hal;

$json = '{"foo":"bar","baz":"qux","_links":{"self":{"href":"/foo"},"foo":{"href":"/bar"}}}';

$reader = new Hal\JsonReader();
$resource = $reader->execute($json);
print_r($resource);
PhpHal\Resource Object
(
    [uri:protected] => /foo
    [links:protected] => Array
        (
            [foo] => PhpHal\Link Object
                (
                    [href:protected] => /bar
                )

        )

    [data:protected] => Array
        (
            [foo] => bar
            [baz] => qux
        )

)

Tests

Tests are in the tests folder. To run them, you need PHPUnit. Example:, (*12)

$ phpunit --configuration phpunit.xml.dist

License

MIT, see LICENSE, (*13)

The Versions

29/11 2014

dev-master

9999999-dev

REST HAL library as a C-extension for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

extension

29/11 2014

v1.0.2

1.0.2.0

REST HAL library as a C-extension for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

extension

30/07 2014

v1.0.1

1.0.1.0

REST HAL library as a C-extension for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

extension

20/07 2014

v1.0.0

1.0.0.0

REST HAL library as a C-extension for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

extension