Hal library

dev-master 2014-03-23 11:05 UTC

This package is not auto-updated.

Last update: 2024-11-09 14:40:10 UTC


README

A very simple Hal library for creating objects that can be turned into HAL compatible payloads.

Links

Use

Create the top level resource of your response. All resources require an instance as Hal\Link as a constructor parameter and the type of the resource.

$resource = new Resource(new Link('self', 'http://example.com/api/book/1'), 'book');

Now that we have our resource, we can add additional HAL attributes such as a _link

$resource->addLink(new Link('publisher', 'http://example.com/api/publisher/56'));

Or an _embedded resource

$resource->addEmbedded(new Resource(
    new Link('self', 'http://exmaple.com/api/author/99'),
    'author',
    null,
    null,
    array(
        'name' => 'George Orwell',
        'born' => '25 June 1903',
        'died' => '21 January 1950'
    )
), 'author');

And add some attributes to the resource

$resource->addAttributes(array(
    'title' => 'Animal Farm',
    'pages' => 112,
    'language' => 'English',
    'country' => 'United Kingdom'
));

Once you have created your Resource, you can output an array representation

$array = $resource->toArray();