You are here

protected function EntityShareServerRestAbstract::formatResponse in Entity Share 7

Format the response in function of the accept header.

1 call to EntityShareServerRestAbstract::formatResponse()
EntityShareServerRestAbstract::outputResponse in modules/entity_share_server/includes/entity_share_server.rest.abstract.inc
Output the response to the client.

File

modules/entity_share_server/includes/entity_share_server.rest.abstract.inc, line 310
Class for handling Entity Share Rest Server request.

Class

EntityShareServerRestAbstract
Abstract Class to manage the EntityShare Rest server.

Code

protected function formatResponse() {
  $result = array(
    'result' => $this
      ->getResult(),
    'error' => $this
      ->getError(),
    'status' => !$this
      ->getError() ? self::STATUS_OK : self::STATUS_ERROR,
  );

  // Default formatters.
  $formatter = array(
    'application/json' => function ($result, $server) {
      return drupal_json_encode($result);
    },
  );
  drupal_alter(self::HOOK_PREFIX . 'response_formatter', $formatter);

  // Call the formatter matching the request.
  foreach ($formatter as $http_accept => $callback) {
    if (strstr($_SERVER['HTTP_ACCEPT'], $http_accept)) {
      if (is_callable($callback)) {
        $this
          ->setHeader('Content-Type', $http_accept);
        $this->response = call_user_func_array($callback, array(
          $result,
          $this,
        ));
        break;
      }
      else {

        // Watchdog.
        watchdog(self::WATCHDOG_TYPE, 'The Content Type %type callback is not valid', array(
          '%type' => $http_accept,
        ), WATCHDOG_ERROR);
      }
    }
  }
  if (is_null($this->response)) {
    $this->response = t('HTTP_ACCEPT "@accept" not managed', array(
      '@accept' => $_SERVER['HTTP_ACCEPT'],
    ));
  }
}