/** * @param Response $controllerResponse */ public function __construct($controllerResponse) { if ($controllerResponse instanceof ResponseArray) { foreach ($controllerResponse->getResponses() as $response) { $this->records[] = $response->getData(); } } elseif ($controllerResponse instanceof Response) { $this->records[] = $controllerResponse->getData(); } parent::__construct(); }
public function __construct(Response $response) { parent::__construct(); $this->setStatusCode($response->getStatusCode(), $response->getStatusMessage()); $this->data = $response->getData(); $this->meta = $response->getMeta(); $this->messages = $response->getMessages(); }
/** * If the application throws an HTTPException, respond correctly (json etc.) * todo this was not working as the try catch blocks in controllers * was catching the exception before it would be handled, need * to come back to this */ public function setExceptionHandler(DiInterface $di) { //return $this; set_exception_handler(function ($exception) use($di) { /** @var $exception Exception */ // Handled exceptions if (is_a($exception, 'Phrest\\API\\Exceptions\\HandledException')) { $response = new Response(); $response->setStatusCode($exception->getCode(), $exception->getMessage()); $response->addMessage($exception->getMessage(), ResponseMessage::TYPE_WARNING); return (new JSONResponse($response))->send(); } else { $response = new Response(); $response->setStatusCode(500, 'Internal Server Error'); $response->addMessage('Internal Server Error', ResponseMessage::TYPE_WARNING); (new JSONResponse($response))->send(); } // Log the exception error_log($exception); error_log($exception->getTraceAsString()); return true; }); }