/** * Generic response. * * @api * @param array|null $payload * @return \Illuminate\Contracts\Http\Response */ public function respond($payload) { if ($meta = $this->getMeta()) { $payload = array_merge($payload, ['meta' => $meta]); } return !($callback = $this->request->input('callback')) ? $this->response->json($payload, $this->getStatusCode(), $this->getHeaders()) : $this->response->jsonp($callback, $payload, $this->getStatusCode(), $this->getHeaders()); }
/** * Serialize the data and wrap it in a JSON response object. * * @param int|null $statusCode * @param array $headers * @return \Illuminate\Http\JsonResponse */ public function respond(int $statusCode = null, array $headers = []) : JsonResponse { if (!is_null($statusCode)) { $this->setStatus($statusCode); } $data = $this->includeStatusCode($this->toArray()); return $this->responseFactory->json($data, $this->statusCode, $headers); }
/** * Generic response. * * @api * @param array|null $payload * @return \Illuminate\Contracts\Http\Response */ public function respond($payload = []) { if ($meta = $this->getMeta()) { $payload = array_merge($payload, ['meta' => $meta]); } $statusCode = config('api.suppress_response_code') === true ? StatusCode::OK : $this->getStatusCode(); return !($callback = $this->request->input('callback')) ? $this->response->json($payload, $statusCode, $this->getHeaders(), JSON_PRETTY_PRINT) : $this->response->jsonp($callback, $payload, $statusCode, $this->getHeaders()); }
public function testJsonResponseFromArrayableInterface() { // mock one Arrayable object $content = $this->getMockBuilder('Illuminate\\Contracts\\Support\\Arrayable')->setMethods(['toArray'])->getMock(); $content->expects($this->once())->method('toArray')->willReturn(['hello' => 'world']); $responseFactory = new ResponseFactory(); $response = $responseFactory->json($content); $this->assertInstanceOf('\\Symfony\\Component\\HttpFoundation\\Response', $response); $this->assertEquals('{"hello":"world"}', $response->getContent()); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); }