protected function RestfulFormatterHalJson::addHateoas in RESTful 7
Add HATEOAS links to list of item.
Parameters
array $data: The data array after initial massaging.
1 call to RestfulFormatterHalJson::addHateoas()
- RestfulFormatterHalJson::prepare in plugins/
formatter/ hal_json/ RestfulFormatterHalJson.class.php - Massages the raw data to create a structured array to pass to the renderer.
File
- plugins/
formatter/ hal_json/ RestfulFormatterHalJson.class.php, line 77 - Contains RestfulFormatterHalJson.
Class
- RestfulFormatterHalJson
- @file Contains RestfulFormatterHalJson.
Code
protected function addHateoas(array &$data) {
if (!$this->handler) {
return;
}
$request = $this->handler
->getRequest();
if (!isset($data['_links'])) {
$data['_links'] = array();
}
// Get self link.
$data['_links']['self'] = array(
'title' => 'Self',
'href' => $this->handler
->versionedUrl($this->handler
->getPath()),
);
$page = !empty($request['page']) ? $request['page'] : 1;
if ($page > 1) {
$request['page'] = $page - 1;
$data['_links']['previous'] = array(
'title' => 'Previous',
'href' => $this->handler
->getUrl($request),
);
}
$curies_resource = $this
->withCurie($this->handler
->getResourceName());
// We know that there are more pages if the total count is bigger than the
// number of items of the current request plus the number of items in
// previous pages.
$items_per_page = $this->handler
->getRange();
$previous_items = ($page - 1) * $items_per_page;
if (isset($data['count']) && $data['count'] > count($data[$curies_resource]) + $previous_items) {
$request['page'] = $page + 1;
$data['_links']['next'] = array(
'title' => 'Next',
'href' => $this->handler
->getUrl($request),
);
}
if (!($curie = $this
->getCurie())) {
return;
}
$data['_links']['curies'] = array(
'name' => $curie['name'],
'href' => $curie['href'] ? $curie['href'] : url('docs/rels', array(
'absolute' => TRUE,
)) . '/{rel}',
'templated' => TRUE,
);
}