From ec491ecea574ea4b2a28fd85dca85753b11c3ff1 Mon Sep 17 00:00:00 2001 From: Najm Njeim Date: Sun, 19 Sep 2021 04:50:56 +0300 Subject: [PATCH] general refactoring and code styling --- CHANGELOG.md | 3 + README.md | 49 ++-- .../lang/ar/messages.php | 0 .../lang/en/messages.php | 0 .../lang/fr/messages.php | 0 src/Respond.php | 21 +- src/RespondBuilder.php | 79 +++++++ src/RespondFactory.php | 77 ------- src/RespondHelper.php | 212 +++++++++--------- src/RespondInterface.php | 36 +++ src/RespondServiceProvider.php | 47 ++-- 11 files changed, 290 insertions(+), 234 deletions(-) rename {src/resources => resources}/lang/ar/messages.php (100%) rename {src/resources => resources}/lang/en/messages.php (100%) rename {src/resources => resources}/lang/fr/messages.php (100%) create mode 100644 src/RespondBuilder.php delete mode 100644 src/RespondFactory.php create mode 100644 src/RespondInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9568df6..a4863af 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,5 +8,8 @@ All notable changes will be documented in this file - adjusted translations and addition of respond with not authenticated helper. - removed dependency on the Http Response facade. +## 1.0.2 - 2021-09-19 +- general refactoring + diff --git a/README.md b/README.md index 7498d03..5b93bc8 100755 --- a/README.md +++ b/README.md @@ -21,14 +21,14 @@ use Nnjeim\Respond\Respond; ['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com/')->get('countries'); - if ($status === 200 && $response->success) { + if ($status === 200 && $response->success) { - return Respond::setMessage('countries') - ->toJson() - ->withSuccess($response->data); - } + return Respond::toJson() + ->setMessage('countries') + ->withSuccess($response->data); + } - abort(400); + abort(400); ``` ##### RespondHelper Instantiation @@ -39,20 +39,21 @@ private RespondHelper $respond; private array $data; private bool $success; -public function __construct(RespondHelper $respond) { - - $this->respond = $respond; +public function __construct(RespondHelper $respond) +{ + $this->respond = $respond; } . . . $respond = $this - ->respond - ->setMessage('countries') - ->toJson(); + ->respond + ->setMessage('countries') + ->toJson(); -if ($this->success) { - return $respond->withSuccess($data) +if ($this->success) +{ + return $respond->withSuccess($data) } return $respond->withErrors(); @@ -139,7 +140,7 @@ Record not found error. The default response status code is 404. ##### Respond with not authenticated ``` -Not authorized reponse. The default response status code is 401. +Not authenticated reponse. The default response status code is 401. @return array withNotAuthenticated($message = null) ``` @@ -156,15 +157,15 @@ Not authorized reponse. The default response status code is 403. ``` @return array - [ - 'response' => [ - 'success' => true, - 'message' => 'message', - 'data' => [], - 'errors' => [], - ], - 'status' => 200, - ]; + [ + 'response' => [ + 'success' => true, + 'message' => 'message', + 'data' => [], + 'errors' => [], + ], + 'status' => 200, + ]; ``` ## Testing diff --git a/src/resources/lang/ar/messages.php b/resources/lang/ar/messages.php similarity index 100% rename from src/resources/lang/ar/messages.php rename to resources/lang/ar/messages.php diff --git a/src/resources/lang/en/messages.php b/resources/lang/en/messages.php similarity index 100% rename from src/resources/lang/en/messages.php rename to resources/lang/en/messages.php diff --git a/src/resources/lang/fr/messages.php b/resources/lang/fr/messages.php similarity index 100% rename from src/resources/lang/fr/messages.php rename to resources/lang/fr/messages.php diff --git a/src/Respond.php b/src/Respond.php index eba7f09..0a81ae6 100644 --- a/src/Respond.php +++ b/src/Respond.php @@ -4,14 +4,15 @@ use Illuminate\Support\Facades\Facade; -class Respond extends Facade { - - /** - * Get the registered name of the component. - * - * @return string - */ - protected static function getFacadeAccessor() { - return 'respond'; - } +class Respond extends Facade +{ + /** + * Get the registered name of the component. + * + * @return string + */ + protected static function getFacadeAccessor() + { + return 'respond'; + } } diff --git a/src/RespondBuilder.php b/src/RespondBuilder.php new file mode 100644 index 0000000..f9d5134 --- /dev/null +++ b/src/RespondBuilder.php @@ -0,0 +1,79 @@ +attributes[$key] ?? null; + } + + /** + * @param $key + * @param $value + */ + public function __set($key, $value) + { + $this->attributes[$key] = $value; + } + + /** + * @return array|JsonResponse + */ + public function respond() + { + $this->formatResponse(); + + ['response' => $response, 'status' => $status] = $this->attributes; + + return ($this->json === null) ? compact('response', 'status') : response()->json($response, $status); + } + + private function formatResponse(): void + { + $response = [ + 'success' => false, + 'data' => [], + 'errors' => [], + ]; + + if ($this->success !== null) { + $response = array_merge($response, [ + 'success' => $this->success, + ]); + } + + if ($this->message !== null) { + $response = array_merge([ + 'message' => $this->message, + ], $response); + } + + if ($this->data !== null) { + $response = array_merge($response, [ + 'data' => $this->data + ]); + } + + if ($this->errors !== null) { + $response = array_merge($response, [ + 'errors' => $this->errors + ]); + } + + if ($this->meta !== null) { + $response = array_merge($response, $this->meta); + } + + $this->response = $response; + } +} diff --git a/src/RespondFactory.php b/src/RespondFactory.php deleted file mode 100644 index 9bcf223..0000000 --- a/src/RespondFactory.php +++ /dev/null @@ -1,77 +0,0 @@ -attributes[$key] ?? null; - } - - /** - * @param $key - * @param $value - */ - public function __set($key, $value) { - - $this->attributes[$key] = $value; - } - - /** - * @return array|\Illuminate\Http\JsonResponse - */ - public function respond() { - - $this->formatResponse(); - - ['response' => $response, 'status' => $status] = $this->attributes; - - return ($this->json === null) ? compact('response', 'status') : response()->json($response, $status); - } - - private function formatResponse(): void { - - $response = [ - 'success' => false, - 'data' => [], - 'errors' => [], - ]; - - if ($this->success !== null) { - $response = array_merge($response, [ - 'success' => $this->success, - ]); - } - - if ($this->message !== null) { - $response = array_merge([ - 'message' => $this->message, - ], $response); - } - - if ($this->data !== null) { - $response = array_merge($response, [ - 'data' => $this->data - ]); - } - - if ($this->errors !== null) { - $response = array_merge($response, [ - 'errors' => $this->errors - ]); - } - - if ($this->meta !== null) { - $response = array_merge($response, $this->meta); - } - - $this->response = $response; - } -} diff --git a/src/RespondHelper.php b/src/RespondHelper.php index 19f8414..2b6798c 100644 --- a/src/RespondHelper.php +++ b/src/RespondHelper.php @@ -2,84 +2,94 @@ namespace Nnjeim\Respond; -class RespondHelper extends RespondFactory { - - const HTTP_OK = 200; - const HTTP_CREATED = 201; - const HTTP_ACCEPTED = 202; - const HTTP_NO_CONTENT = 204; - const HTTP_UNAUTHORIZED = 401; - const HTTP_FORBIDDEN = 403; - const HTTP_NOT_FOUND = 404; - const HTTP_UNPROCESSABLE_ENTITY = 422; - const HTTP_INTERNAL_SERVER_ERROR = 500; - /** - * @param $code - * @return $this - */ - public function setStatusCode(int $code) { - +use Nnjeim\Respond\RespondBuilder; +use Nnjeim\Respond\RespondInterface; + +use Illuminate\Http\JsonResponse; + +class RespondHelper extends RespondBuilder implements RespondInterface { + + public const HTTP_OK = 200; + public const HTTP_CREATED = 201; + public const HTTP_ACCEPTED = 202; + public const HTTP_NO_CONTENT = 204; + public const HTTP_UNAUTHORIZED = 401; + public const HTTP_FORBIDDEN = 403; + public const HTTP_NOT_FOUND = 404; + public const HTTP_UNPROCESSABLE_ENTITY = 422; + public const HTTP_INTERNAL_SERVER_ERROR = 500; + + /** + * @param string $message + * @return $this + */ + public function setMessage(string $message): self + { + $this->message = $message; + + return $this; + } + + /** + * @param $data + * @return $this + */ + public function setData($data): self + { + $this->data = $data; + + return $this; + } + + /** + * @param array $meta + * @return $this + */ + public function setMeta(array $meta): self + { + $this->meta = $meta; + + return $this; + } + + /** + * @param array $errors + * @return $this + */ + public function setErrors(array $errors): self + { + $this->errors = $errors; + + return $this; + } + + /** + * @param int $code + * @return $this + */ + public function setStatusCode(int $code): self + { $this->status = $code; return $this; } - /** - * @param string $message - * @return $this - */ - public function setMessage(string $message) { - - $this->message = $message; - - return $this; - } - - /** - * @param array $meta - * @return $this - */ - public function setMeta(array $meta) { - - $this->meta = $meta; - - return $this; - } - - /** - * @param $data - * @return $this - */ - public function setData($data) { - - $this->data = $data; - - return $this; - } - - /** - * @param array $errors - * @return $this - */ - public function setErrors(array $errors) { - - $this->errors = $errors; - - return $this; - } - - public function toJson() { - + /** + * @return $this + */ + public function toJson(): self + { $this->json = true; return $this; } - /** - * @param array|null $data - * @return array - */ - public function withSuccess($data = null) { + /** + * @param null $data + * @return array|JsonResponse + */ + public function withSuccess($data = null) + { $this->success = true; $this->status ??= self::HTTP_OK; @@ -92,12 +102,12 @@ public function withSuccess($data = null) { return $this->respond(); } - /** - * @param array|null $errors - * @return array - */ - public function withErrors(?array $errors = null) { - + /** + * @param array|null $errors + * @return array|JsonResponse + */ + public function withErrors(?array $errors = null) + { $this->success = false; $this->status ??= self::HTTP_UNPROCESSABLE_ENTITY; @@ -112,11 +122,11 @@ public function withErrors(?array $errors = null) { return $this->respond(); } - /** - * @return array - */ - public function withNoContent() { - + /** + * @return array|JsonResponse + */ + public function withNoContent() + { $this->success = true; $this->status ??= self::HTTP_NO_CONTENT; @@ -128,11 +138,11 @@ public function withNoContent() { return $this->respond(); } - /** - * @return array - */ - public function withServerError() { - + /** + * @return array|JsonResponse + */ + public function withServerError() + { $this->success = false; $this->status ??= self::HTTP_INTERNAL_SERVER_ERROR; @@ -144,13 +154,13 @@ public function withServerError() { return $this->respond(); } - /** - * @param string|null $attribute - * @param bool $plural - * @return array - */ - public function withNotFound(?string $attribute = null, bool $plural = true) { - + /** + * @param string|null $attribute + * @param bool $plural + * @return array|JsonResponse + */ + public function withNotFound(?string $attribute = null, bool $plural = true) + { $this->success = false; $this->status ??= self::HTTP_NOT_FOUND; @@ -166,11 +176,11 @@ public function withNotFound(?string $attribute = null, bool $plural = true) { return $this->respond(); } - /** - * @return array - */ - public function withNotAuthenticated() { - + /** + * @return array|JsonResponse + */ + public function withNotAuthenticated() + { $this->success = false; $this->status ??= self::HTTP_UNAUTHORIZED; @@ -180,11 +190,11 @@ public function withNotAuthenticated() { return $this->respond(); } - /** - * @return array - */ - public function withNotAuthorized() { - + /** + * @return array|JsonResponse + */ + public function withNotAuthorized() + { $this->success = false; $this->status ??= self::HTTP_FORBIDDEN; diff --git a/src/RespondInterface.php b/src/RespondInterface.php new file mode 100644 index 0000000..0e72a12 --- /dev/null +++ b/src/RespondInterface.php @@ -0,0 +1,36 @@ +loadTranslationsFrom(__DIR__ . '/resources/lang', 'respond'); - } +class RespondServiceProvider extends ServiceProvider +{ + /** + * Bootstrap services. + * + * @return void + */ + public function boot() + { + // Load translations + $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'respond'); + } - /** - * Register services. - * - * @return void - */ - public function register() { + /** + * Register services. + * + * @return void + */ + public function register() + { + // Register the main class to use with the facade + $this->app->singleton('respond', function () { - // Register the main class to use with the facade - $this->app->singleton('respond', function () { - - return new RespondHelper; - }); - } + return new RespondHelper(); + }); + } }