/** * Returns a response with the given message and status code * * @param string|array $data Data to send * @param int $status Status code of the response * @param bool $forceError If TRUE the response will be treated as an error, otherwise any status below 400 will be a normal response * @return Response * @internal */ public function createResponse($data, $status, $forceError = FALSE) { $body = NULL; $response = new Response(NULL, $status); $format = $this->requestFactory->getRequest()->format(); if (!$format) { $format = 'json'; } $messageKey = 'message'; if ($forceError || $status >= 400) { $messageKey = 'error'; } switch ($format) { case 'json': switch (gettype($data)) { case 'string': $body = array($messageKey => $data); break; case 'array': $body = $data; break; case 'NULL': $body = array($messageKey => $response->statusText($status)); break; } $response->contentType('application/json'); $response->content(json_encode($body)); break; case 'xml': // TODO: support more response formats // TODO: support more response formats default: $body = sprintf('Unsupported format: %s. Please set the Accept header to application/json', $format); $response->content($body); } return $response; }