Skip to content

Commit

Permalink
initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
navjotsinghprince committed Aug 20, 2022
0 parents commit 556cd1a
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
139 changes: 139 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Prince\Ferozepuria\HTTPResponse;
use Illuminate\Support\Facades\Validator;

//Feel Free To Visit https://navjotsinghprince.com
class TestController extends HTTPResponse {


/**
* Example 1...
*/
public function example1(Request $request)
{
$collection = collect([1, 2, 3]);

$class_Obj = new \stdClass();
$class_Obj->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
<?php

$response = [
"name" => "Prince Ferozepuria",
"email" => "[email protected]",
"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 [[email protected]](mailto:[email protected]). Your all questions will be answered.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md)
file for details.
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
]
}
}
}
188 changes: 188 additions & 0 deletions src/HTTPResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Prince\Ferozepuria;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;

/*
* This file is a part of HTTP Response Service Provider.
*
* (c) Prince Ferozepuria
*
* Feel Free to visit: https://navjotsinghprince.com
* Contact Us: [email protected]
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

class HTTPResponse extends Controller
{

public static $HTTP_OK = 200;
public static $HTTP_BAD_REQUEST = 400;
public static $HTTP_UNAUTHORIZED = 401;
public static $HTTP_FAILED = 402;
public static $HTTP_FORBIDDEN = 403;
public static $HTTP_NOT_FOUND = 404;
public static $HTTP_CONFLICT = 409;
public static $HTTP_UNPROCESSABLE_ENTITY = 422;
public static $HTTP_TOO_MANY_REQUESTS = 429;

/**
* Return successfull json response.
* The HTTP 200 OK success status response code indicates that the request has succeeded.
* @param string $message
* @param [int,boolean,string,array,collection,class_object] $data
* @return json
*/
public function sendSuccess($message, $data = null)
{
$response = [
"status" => 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);
}
}
Loading

0 comments on commit 556cd1a

Please sign in to comment.