2017 © Pedro Peláez
 

library hal

Hal library

image

monkeyphp/hal

Hal library

  • Sunday, March 23, 2014
  • by MonkeyPHP
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Hal

A very simple Hal library for creating objects that can be turned into HAL compatible payloads., (*1)

  • https://phlyrestfully.readthedocs.org/en/latest/halprimer.html
  • https://api-sandbox.foxycart.com/hal-browser/hal_browser.html#https://api-sandbox.foxycart.com/stores/8/transactions?zoom=items,payments
  • http://stateless.co/hal_specification.html
  • http://nocarrier.co.uk/2013/03/expanding-zoom/
  • http://gotohal.net/

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., (*2)

$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, (*3)

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

Or an _embedded resource, (*4)

$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, (*5)

$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, (*6)

$array = $resource->toArray();

The Versions