class RestfulFormatterJson in RESTful 7
@file Contains RestfulFormatterJson.
Hierarchy
- class \RestfulPluginBase implements RestfulPluginInterface
- class \RestfulFormatterBase implements RestfulFormatterInterface
- class \RestfulFormatterJson implements RestfulFormatterInterface
- class \RestfulFormatterBase implements RestfulFormatterInterface
Expanded class hierarchy of RestfulFormatterJson
File
- plugins/
formatter/ json/ RestfulFormatterJson.class.php, line 8 - Contains RestfulFormatterJson.
View source
class RestfulFormatterJson extends \RestfulFormatterBase implements \RestfulFormatterInterface {
/**
* Content Type
*
* @var string
*/
protected $contentType = 'application/json; charset=utf-8';
/**
* {@inheritdoc}
*/
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;
}
/**
* Add HATEOAS links to list of item.
*
* @param $data
* The data array after initial massaging.
*/
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),
);
}
}
/**
* {@inheritdoc}
*/
public function render(array $structured_data) {
return drupal_json_encode($structured_data);
}
/**
* {@inheritdoc}
*/
public function getContentTypeHeader() {
return $this->contentType;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RestfulFormatterBase:: |
protected | property | The entity handler containing more info about the request. | |
RestfulFormatterBase:: |
public | function |
Formats the un-structured data into the output format. Overrides RestfulFormatterInterface:: |
|
RestfulFormatterBase:: |
public | function |
Generic constructor. Overrides RestfulPluginBase:: |
|
RestfulFormatterJson:: |
protected | property | Content Type | |
RestfulFormatterJson:: |
protected | function | Add HATEOAS links to list of item. | |
RestfulFormatterJson:: |
public | function |
Returns the content type for the selected output format. Overrides RestfulFormatterBase:: |
|
RestfulFormatterJson:: |
public | function |
Massages the raw data to create a structured array to pass to the renderer. Overrides RestfulFormatterInterface:: |
|
RestfulFormatterJson:: |
public | function |
Renders an array in the selected format. Overrides RestfulFormatterInterface:: |
|
RestfulPluginBase:: |
protected | property | The plugin definition array. | |
RestfulPluginBase:: |
public | function |
Gets information about the restful plugin. Overrides RestfulPluginInterface:: |
|
RestfulPluginBase:: |
public | function |
Gets information about the restful plugin key. Overrides RestfulPluginInterface:: |
|
RestfulPluginBase:: |
public | function |
Sets information about the restful plugin. Overrides RestfulPluginInterface:: |
|
RestfulPluginBase:: |
public | function |
Gets information about the restful plugin key. Overrides RestfulPluginInterface:: |