diff --git a/src/LangleyFoxall/Helpers/ApiResponse.php b/src/LangleyFoxall/Helpers/ApiResponse.php index 6669ab0..c196e5b 100644 --- a/src/LangleyFoxall/Helpers/ApiResponse.php +++ b/src/LangleyFoxall/Helpers/ApiResponse.php @@ -1,78 +1,158 @@ status = $status; - $this->success = $success; - $this->error = $error; + $this->request = $request; + } + + /** + * @param string $message + * @param array $data + * @param array $meta + * @param int $status + * @return $this + */ + public function success(string $message = null, array $data = null, array $meta = null, int $status = 200) + { + $this->type = 'success'; + $this->message = $message; $this->data = $data; $this->meta = $meta; + $this->status = $status; + + return $this; } /** - * @param array|null $data - * @param array|null $meta - * @param int $status - * @return ApiResponse + * @param mixed $message + * @param int $status + * @return $this + */ + public function error($message = null, int $status = 400) + { + $this->message = $message; + $this->status = $status; + + return $this; + } + + public function type(string $type) + { + $this->type = $type; + + return $this; + } + + /** + * @param string $message + * @return $this */ - public static function success($data = null, $meta = null, int $status = 200) + public function message(string $message) { - $response = new self; + $this->message = $message; - $response->status = $status; - $response->data = $data; - $response->meta = $meta; + return $this; + } - return $response; + /** + * @param array $data + * @return $this + */ + public function data(array $data = null) + { + $this->data = $data; + + return $this; + } + + /** + * @param array $meta + * @return $this + */ + public function meta(array $meta = null) + { + $this->meta = $meta; + + return $this; } /** - * @param mixed $error * @param int $status - * @return ApiResponse + * @return $this */ - public static function error($error = null, int $status = 400) + public function status(int $status) { - $response = new self; + $this->status = $status; - $response->status = $status; - $response->success = false; - $response->error = $error; + return $this; + } - return $response; + /** + * @param string $uri + * @return $this + */ + public function redirect(string $uri) + { + $this->uri = $uri; + + return $this; } - public function json() + /** + * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse + */ + public function end() { - return response()->json(get_object_vars($this)) - ->setStatusCode($this->status); - }} \ No newline at end of file + $type = $this->type; + $message = $this->message; + $status = $this->status; + $data = $this->data; + $meta = $this->meta; + + if ($this->request->expectsJson()) { + if ($type === 'error') { + return ApiResponse::error($message, $status)->json(); + } + + return ApiResponse::success($data, $meta, $status)->json(); + } + + + if ($this->uri) { + return redirect($this->uri); + } + + return back()->with($type, $message); + } +} \ No newline at end of file diff --git a/src/LangleyFoxall/Helpers/Response.php b/src/LangleyFoxall/Helpers/Response.php new file mode 100644 index 0000000..c196e5b --- /dev/null +++ b/src/LangleyFoxall/Helpers/Response.php @@ -0,0 +1,158 @@ +request = $request; + } + + /** + * @param string $message + * @param array $data + * @param array $meta + * @param int $status + * @return $this + */ + public function success(string $message = null, array $data = null, array $meta = null, int $status = 200) + { + $this->type = 'success'; + $this->message = $message; + $this->data = $data; + $this->meta = $meta; + $this->status = $status; + + return $this; + } + + /** + * @param mixed $message + * @param int $status + * @return $this + */ + public function error($message = null, int $status = 400) + { + $this->message = $message; + $this->status = $status; + + return $this; + } + + public function type(string $type) + { + $this->type = $type; + + return $this; + } + + /** + * @param string $message + * @return $this + */ + public function message(string $message) + { + $this->message = $message; + + return $this; + } + + /** + * @param array $data + * @return $this + */ + public function data(array $data = null) + { + $this->data = $data; + + return $this; + } + + /** + * @param array $meta + * @return $this + */ + public function meta(array $meta = null) + { + $this->meta = $meta; + + return $this; + } + + /** + * @param int $status + * @return $this + */ + public function status(int $status) + { + $this->status = $status; + + return $this; + } + + /** + * @param string $uri + * @return $this + */ + public function redirect(string $uri) + { + $this->uri = $uri; + + return $this; + } + + /** + * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse + */ + public function end() + { + $type = $this->type; + $message = $this->message; + $status = $this->status; + $data = $this->data; + $meta = $this->meta; + + if ($this->request->expectsJson()) { + if ($type === 'error') { + return ApiResponse::error($message, $status)->json(); + } + + return ApiResponse::success($data, $meta, $status)->json(); + } + + + if ($this->uri) { + return redirect($this->uri); + } + + return back()->with($type, $message); + } +} \ No newline at end of file