You are here

protected function RestfulFormatterJson::addHateoas in RESTful 7

Add HATEOAS links to list of item.

Parameters

$data: The data array after initial massaging.

1 call to RestfulFormatterJson::addHateoas()
RestfulFormatterJson::prepare in plugins/formatter/json/RestfulFormatterJson.class.php
Massages the raw data to create a structured array to pass to the renderer.

File

plugins/formatter/json/RestfulFormatterJson.class.php, line 56
Contains RestfulFormatterJson.

Class

RestfulFormatterJson
@file Contains RestfulFormatterJson.

Code

protected function addHateoas(array &$data) {
  if (!$this->handler) {
    return;
  }
  $request = $this->handler
    ->getRequest();

  // Get self link.
  $data['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['previous'] = array(
      'title' => 'Previous',
      'href' => $this->handler
        ->getUrl($request),
    );
  }

  // 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['data']) + $previous_items) {
    $request['page'] = $page + 1;
    $data['next'] = array(
      'title' => 'Next',
      'href' => $this->handler
        ->getUrl($request),
    );
  }
}