Example #1
0
 /**
  * Ensures that getData() returns data as expected.
  *
  */
 public function testGetData()
 {
     $data = ['foo' => 'foo', 'bar' => 'baz'];
     $response = JsonResponse::create($data);
     $result = $response->getData();
     $this->assertArrayHasKey('foo', $result);
     $this->assertArrayHasKey('bar', $result);
     $this->assertEquals($result['foo'], 'foo');
     $this->assertEquals($result['bar'], 'baz');
 }
Example #2
0
 /**
  * Helper method to return a JSON error response.
  *
  * @param string $message
  *   The error message.
  * @param string $code
  *   The error code to include in the response. This is typically a machine
  *   name representation of the error. (e.g. "unauthorized" or "not_allowed")
  * @param int $status
  *   The HTTP status code for this response.
  *
  * @return JsonResponse
  *
  */
 public function toError($message, $code = 'system', $status = 500)
 {
     $data = ['error' => $code, 'message' => $message];
     return JsonResponse::create($data, $status);
 }
Example #3
0
/**
 * Allows for modification of the response depending on the exception thrown
 * while attempting to execute the request.
 *
 * If a previous module has already generated a ResponseInterface object based
 * on a specific exception, the response will be included as the second
 * parameter.
 *
 * @param Exception $e
 *   The exception that was thrown.
 * @param ResponseInterface $response
 *   A ResponseInterface object, if a previous module has already responded to
 *   this exception.
 *
 * @return ResponseInterface|NULL
 *
 */
function hook_restapi_exception(Exception $e, ResponseInterface $response = NULL)
{
    if ($e instanceof RestApiException) {
        $response = JsonResponse::create("This is my modified response");
    }
    return $response;
}
 /**
  * {@inheritdoc}
  *
  */
 public function toJson($data, $status = 200)
 {
     return JsonResponse::create($data, $status);
 }