<?php require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; // create the top level resource $resource = new Resource(new Link('self', '/customer/123'), 'customer'); // add some attributes to the resource $resource->addAttributes(array('firstname' => 'David', 'lastname' => 'White', 'age' => 32)); // add some embedded resources $address = new Resource(new Link('self', '/address/23987'), 'address'); $address->addAttributes(array('street' => 'A Road', 'town' => 'Harrogate', 'county' => 'North Yorkshire')); $resource->addEmbedded($address, 'addresses'); // add some links $orders = new Link('orders', '/customer/123/orders'); $payments = new Link('payments', '/customer/123/payments'); $resource->addLink($orders); $resource->addLink($payments); // print out the resulting array print_r($resource->toArray());
<?php // @link http://en.wikipedia.org/wiki/Animal_Farm require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; $resource = new Resource(new Link('self', 'http://example.com/api/book/1'), 'book'); $resource->addLink(new Link('publisher', 'http://example.com/api/publisher/56')); $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'); $resource->addAttributes(array('title' => 'Animal Farm', 'pages' => 112, 'language' => 'English', 'country' => 'United Kingdom')); $array = $resource->toArray(); echo json_encode($array, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);