You are here

public function RestfulFormatterJson::prepare in RESTful 7

Massages the raw data to create a structured array to pass to the renderer.

Parameters

array $data: The raw data to return.

Return value

array The data prepared to be rendered.

Overrides RestfulFormatterInterface::prepare

File

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

Class

RestfulFormatterJson
@file Contains RestfulFormatterJson.

Code

public function prepare(array $data) {

  // If we're returning an error then set the content type to
  // 'application/problem+json; charset=utf-8'.
  if (!empty($data['status']) && floor($data['status'] / 100) != 2) {
    $this->contentType = 'application/problem+json; charset=utf-8';
    return $data;
  }
  $output = array(
    'data' => $data,
  );
  if (!empty($this->handler)) {
    if (method_exists($this->handler, 'getTotalCount') && method_exists($this->handler, 'isListRequest') && $this->handler
      ->isListRequest()) {

      // Get the total number of items for the current request without pagination.
      $output['count'] = $this->handler
        ->getTotalCount();
    }
    if (method_exists($this->handler, 'additionalHateoas')) {
      $output = array_merge($output, $this->handler
        ->additionalHateoas());
    }

    // Add HATEOAS to the output.
    $this
      ->addHateoas($output);
  }
  return $output;
}