/** * @param array $resourceInfo * @return Resource */ public function process(array $resourceInfo) { $resource = new Resource($resourceInfo['links']['self'], $resourceInfo['data']); unset($resourceInfo['links']['self']); foreach ($resourceInfo['links'] as $key => $href) { $resource->setLink(new Link($href, $key)); } if (isset($resourceInfo['embedded'])) { foreach ($resourceInfo['embedded'] as $rel) { foreach ($rel as $rel => $data) { $resource->setEmbedded($rel, new Resource($data['links']['self'], $data['data'])); } } } return $resource; }
/** * @param Resource $embed * @return SimpleXMLElement */ protected function _getEmbRes(Resource $embed) { $resource = $this->_xml->addChild('resource'); return $embed->getXML($resource); }
<?php require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; // create the top level resource $purchase = new Resource(new Link('self', '/purchase/12234234'), 'purchase', array(new Link('payment', '/payment/9080'), new Link('contact', '/contact/8992'), new Link('shipping', '/shipping/1922')), array('items' => array(new Resource(new Link('self', '/item/12'), 'item', null, null, array('quantity' => 1)), new Resource(new Link('self', '/item/23'), 'item', null, null, array('quantity' => 2)), new Resource(new Link('self', '/item/45'), 'item', null, null, array('quantity' => 1)), new Resource(new Link('self', '/item/99'), 'item', null, null, array('quantity' => 3)))), array('date' => '2014-03-07')); print_r($purchase->toArray());
public function testConstruct() { $resource = new Resource(new Link('/test', 'self')); $this->assertEquals(array('_links' => array('self' => array('href' => '/test'))), $resource->asArray()); }
<?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());
/** * Generate the URL for the read page for the given resource. * * @param Resource|int|string $resourceOrIdentifier * * @return string */ public function generateReadUrl($resourceOrIdentifier) { if ($resourceOrIdentifier instanceof Resource) { $data = $resourceOrIdentifier->toArray(); $id = $data['username']; } else { $id = $resourceOrIdentifier; } $route = $this->router->generate('ingewikkeld_rest_user_user_read', array('id' => $id), UrlGeneratorInterface::ABSOLUTE_PATH); return $route; }
/** * Converts the give HAL Resource to a plain text representation that can be returned in the response. * * @param string $format * @param \Hal\Resource $resource * * @throws NotAcceptableHttpException * * @return string */ protected function convertResourceToPlainText($format, Resource $resource) { switch ($format) { case 'xml': return (string) $resource->getXML()->asXml(); case 'json': return (string) $resource; default: throw new NotAcceptableHttpException(); } }
public function testTemplatedLink() { $fixture = <<<'EOF' { "_links":{ "self":{ "href":"/dogs" }, "search":{ "href":"/dogs?q={text}", "templated": true } }, "_embedded":{ "dog":[ { "_links":{ "self":{ "href":"/dogs\/1" } }, "id":"1", "name":"tiber", "color":"black" }, { "_links":{ "self":{ "href":"/dogs\/2" } }, "id":"2", "name":"sally", "color":"white" }, { "_links":{ "self":{ "href":"/dogs\/3" } }, "id":"3", "name":"fido", "color":"gray" } ] } } EOF; $parent = new Resource('/dogs'); /* Add any relevent links */ $parent->setLink(new Link('/dogs?q={text}', 'search', null, null, null, true)); $dogs[1] = new Resource('/dogs/1'); $dogs[1]->setData(array('id' => '1', 'name' => 'tiber', 'color' => 'black')); $dogs[2] = new Resource('/dogs/2', array('id' => '2', 'name' => 'sally', 'color' => 'white')); $dogs[3] = new Resource('/dogs/3', array('id' => '3', 'name' => 'fido', 'color' => 'gray')); /* Add the embedded resources */ foreach ($dogs as $dog) { $parent->setEmbedded('dog', $dog); } $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); }
<?php // https://phlyrestfully.readthedocs.org/en/latest/halprimer.html require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; $resource = new Resource(new Link('self', 'http://example.org/api/user?page=3'), 'users', array(new Link('first', "http://example.org/api/user"), new Link('prev', "http://example.org/api/user?page=2"), new Link('next', "http://example.org/api/user?page=4"), new Link('last', "http://example.org/api/user?page=33")), array('users' => array(new Resource(new Link('self', 'http://example.org/api/user/mwop'), 'user', null, null, array('id' => 'mwop', 'name' => 'Matthew Weier O\'Phinney')), new Resource(new Link('self', 'http://example.org/api/user/mac_nibblet'), 'user', null, null, array('id' => 'mac_nibblet', 'name' => 'Antoine Hedgecock')), new Resource(new Link('self', 'http://example.org/api/user/spiffyjr'), 'user', null, null, array('id' => 'spiffyjr', 'name' => 'Kyle Spraggs')))), array('count' => 3, 'total' => 498)); $array = $resource->toArray(); echo json_encode($array, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
public function testJsonNumericConversion() { $fixture = <<<'EOF' { "_links":{ "search":{ "href":"/dogs?q={text}" } }, "data": 98765432 } EOF; $fixture1 = <<<'EOF' { "_links":{ "search":{ "href":"/dogs?q={text}" } }, "data": "98765432" } EOF; $parent = new Resource('', array('data' => '98765432')); $parent->setLink(new Link('/dogs?q={text}', 'search')); // Default is off $this->assertEquals(json_decode($fixture1), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON); // Active On $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_OFF); // Active Off $this->assertEquals(json_decode($fixture1), json_decode((string) $parent)); $parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON); // State Returned $this->assertEquals(json_decode($fixture), json_decode((string) $parent)); }
<?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);