From 556cd1a435ef7d842737125aa75421d63fe97c8d Mon Sep 17 00:00:00 2001 From: navjotsingh Date: Sat, 20 Aug 2022 11:55:19 +0530 Subject: [PATCH] initialize --- LICENSE.md | 22 ++++ README.md | 139 ++++++++++++++++++++ composer.json | 39 ++++++ src/HTTPResponse.php | 188 ++++++++++++++++++++++++++++ src/HTTPResponseServiceProvider.php | 28 +++++ 5 files changed, 416 insertions(+) create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/HTTPResponse.php create mode 100644 src/HTTPResponseServiceProvider.php diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e841473 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,22 @@ + +MIT License + +Copyright (c) 2022 Prince Ferozepuria + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..100ade9 --- /dev/null +++ b/README.md @@ -0,0 +1,139 @@ +## HTTP Response Introduction + +Send HTTP json response with status codes automatically with ease pre-configured methods. + + +### Installation + +```bash +composer require princeferozepuria/http-response +``` + +### Import HTTPResponse + +```bash +use Prince\Ferozepuria\HTTPResponse; +``` + + +### Extending and Usage + +```php +name="Prince Ferozepuria"; + + $response = [ + "string" => "Prince Ferozepuria", + "int" => 1, + "boolean" => true, + "array" => ["prince", "ferozepuria"], + "collection" => $collection, + "class_object" => $class_Obj, + "is_null" => null, + "is_empty" => "", + ]; + + return $this->sendSuccess("success",$response); + + } + + + /** + * Example 2... + */ + public function example2(Request $request) + { + $validator = Validator::make($request->all(), [ + 'name' => 'required', + 'email'=>'required|email' + ]); + + if ($validator->fails()) { + return $this->validationFailed("all fields are required", $validator->errors()); + } + + } + + + /** + * Example 3... + * Usage With Custom Object + */ + public function example3(Request $request) + { + $data = ["name" => "Prince Ferozepuria"]; + + $response = new HTTPResponse(); + return $response->sendSuccess("This is just test message", $data); + + } + + + } +``` + + +### Available Methods + +```php + "Prince Ferozepuria", + "email" => "fzr@navjotsinghprince.com", + "website" => "https://navjotsinghprince.com" + ]; + + return $this->sendSuccess("success response message", $response); + + return $this->sendSuccessForce("success force response message","total",$response); + + return $this->sendFailure("failed response message",$response); + + return $this->notFound("not Found response message",$response); + + return $this->validationFailed("validation failed response message",$response); + + return $this->forbidden("forbidden response message"); + + return $this->unauthorized("unauthorized response message"); + + return $this->dataProcessFailed("data process failed message"); + +``` + + +## Authors + +* :point_right: [Navjot Singh Prince](https://github.com/navjotsinghprince) + +See also the site of [contributor](https://navjotsinghprince.com) +who participated in this package. + +## Contact US + +If you discover any question within package, please send an e-mail to Prince Ferozepuria via [fzr@navjotsinghprince.com](mailto:fzr@navjotsinghprince.com). Your all questions will be answered. + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) +file for details. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fe16237 --- /dev/null +++ b/composer.json @@ -0,0 +1,39 @@ +{ + "name": "princeferozepuria/http-response", + "description": "Send HTTP Json response with automatic status code via pre-configured methods", + "type": "library", + "license": "MIT", + "keywords": [ + "php", + "laravel", + "http", + "json", + "response" + ], + "homepage": "https://github.com/navjotsinghprince/http-response", + "autoload": { + "psr-4": { + "Prince\\Ferozepuria\\": "src/" + } + }, + "authors": [ + { + "name": "Prince Ferozepuria", + "email": "navjotsinghprince1@gmail.com", + "role": "Software Developer", + "homepage": "https://navjotsinghprince.com/" + } + ], + "support": { + "issues": "https://github.com/navjotsinghprince/http-response" + }, + "minimum-stability": "dev", + "require": {}, + "extra": { + "laravel": { + "providers": [ + "Prince\\Ferozepuria\\HTTPResponseServiceProvider" + ] + } + } +} \ No newline at end of file diff --git a/src/HTTPResponse.php b/src/HTTPResponse.php new file mode 100644 index 0000000..904b528 --- /dev/null +++ b/src/HTTPResponse.php @@ -0,0 +1,188 @@ + self::$HTTP_OK, + "response" => self::$HTTP_OK, + 'message' => $message, + ]; + if ($data != null) { + $response['data'] = $data; + } + return Response::json($response, self::$HTTP_OK); + } + + + /** + * Return successfull json response with your custom key value. + * Custom Key: Your custom response key. + * + * Custom Value: Your custom response value. + * @param string $message + * @param string $key + * @param [int,boolean,string,array,collection,class_object] $value + * @return json + */ + public function sendSuccessForce($message, $key, $value) + { + $response = [ + "status" => self::$HTTP_OK, + "response" => self::$HTTP_OK, + 'message' => $message, + $key => $value, + ]; + return Response::json($response, self::$HTTP_OK); + } + + + /** + * Return failed json response. + * This response is sent when a request conflicts with the current state of the server. + * @param string $message + * @param string array $data + * @return json + */ + public function sendFailure($message, $data = null) + { + $response = [ + "status" => self::$HTTP_CONFLICT, + "response" => self::$HTTP_CONFLICT, + 'message' => $message, + ]; + if ($data != null) { + $response['data'] = $data; + } + return Response::json($response, self::$HTTP_CONFLICT); + } + + + /** + * Return validation failed json response. + * Unprocessable Entity The server does not want to execute it due to validation + * @param [string] $message + * @param [collection] $errors + * @return json + */ + public function validationFailed($message, $errors) + { + $response = [ + "status" => self::$HTTP_UNPROCESSABLE_ENTITY, + "response" => self::$HTTP_UNPROCESSABLE_ENTITY, + 'message' => $message, + 'errors' => $errors, + ]; + return Response::json($response, self::$HTTP_UNPROCESSABLE_ENTITY); + } + + + /** + * Return not found json response. + * The server can not find the requested resource + * @param [string] $message + * @param [array] $errors + * @return json + */ + public function notFound($message, $errors = null) + { + $response = [ + "status" => self::$HTTP_NOT_FOUND, + "response" => self::$HTTP_NOT_FOUND, + 'message' => $message, + 'errors' => is_null($errors) ? 'not specified' : $errors, + ]; + return Response::json($response, self::$HTTP_NOT_FOUND); + } + + + /** + * Return unauthorized json response. + * Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". + * That is, the client must authenticate itself to get the requested response + * @param string $message + * @return json + */ + public function unauthorized($message) + { + $response = [ + "status" => self::$HTTP_UNAUTHORIZED, + "response" => self::$HTTP_UNAUTHORIZED, + 'message' => $message, + 'errors' => [$message] + ]; + return Response::json($response, self::$HTTP_UNAUTHORIZED); + } + + + /** + * Return forbidden json response. + * The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource + * @param [string] $message + * @return json + */ + public function forbidden($message) + { + $response = [ + "status" => self::$HTTP_FORBIDDEN, + "response" => self::$HTTP_FORBIDDEN, + 'message' => $message, + ]; + return Response::json($response, self::$HTTP_FORBIDDEN); + } + + + /** + * Return data process Failed json response. + * The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). + * @param [string] $message + * @return json + */ + public function dataProcessFailed($message) + { + $response = [ + "status" => self::$HTTP_BAD_REQUEST, + "response" => self::$HTTP_BAD_REQUEST, + 'message' => $message, + 'errors' => [$message] + ]; + return Response::json($response, self::$HTTP_BAD_REQUEST); + } +} diff --git a/src/HTTPResponseServiceProvider.php b/src/HTTPResponseServiceProvider.php new file mode 100644 index 0000000..23e6bec --- /dev/null +++ b/src/HTTPResponseServiceProvider.php @@ -0,0 +1,28 @@ +