-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 88fb1ac
Showing
7 changed files
with
400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 kolirt | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<p align="center"> | ||
<img src="https://raw.githubusercontent.com/kolirt/laravel-api-response/3e46d9c25ba6e7fd096ec57c668125cb2ab985ce/image.svg"> | ||
</p> | ||
|
||
# Laravel Api Response | ||
|
||
The package will help to generate json answers. | ||
|
||
## Installation | ||
|
||
``` | ||
$ composer require kolirt/laravel-api-response | ||
``` | ||
|
||
## Example | ||
Error response. | ||
```php | ||
return api() | ||
->error() | ||
->setCode(400) // default code 400 | ||
|
||
->setDescription(['Description #1', 'Description #2']) | ||
// or | ||
->setDescription('Description') | ||
|
||
->setData(['Data #1', 'Data #2']) | ||
// or | ||
->setData('Data') | ||
|
||
->render(); | ||
``` | ||
|
||
```php | ||
[ | ||
'ok' => false, | ||
'error_code' => 400, | ||
|
||
'description' => ['Description #1', 'Description #2'], | ||
// or | ||
'description' => 'Description', | ||
|
||
'result' => ['Data #1', 'Data #2'], | ||
// or | ||
'result' => 'Data', | ||
] | ||
``` | ||
|
||
Success response. | ||
```php | ||
return api() | ||
->success() | ||
->setCode(200) // default code 200 | ||
|
||
->setDescription(['Description #1', 'Description #2']) | ||
// or | ||
->setDescription('Description #1') | ||
|
||
->setData(['Data #1', 'Data #2']) | ||
// or | ||
->setData('Data') | ||
|
||
->render(); | ||
``` | ||
|
||
```php | ||
[ | ||
'ok' => true, | ||
|
||
'description' => ['Description #1', 'Description #2'], | ||
// or | ||
'description' => 'Description', | ||
|
||
'result' => ['Data #1', 'Data #2'], | ||
// or | ||
'result' => 'Data', | ||
] | ||
``` | ||
|
||
## Methods | ||
|
||
### error | ||
Default response code 400. | ||
```php | ||
return api()->error(); | ||
``` | ||
|
||
### success | ||
Default response code 200. | ||
```php | ||
return api()->success(); | ||
``` | ||
|
||
### setCode | ||
Set custom response code. Available [codes](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). | ||
```php | ||
return api()->setCode($code); | ||
``` | ||
|
||
### setDescription | ||
Set description to response. | ||
```php | ||
return api()->setDescription(['Description #1', 'Description #2']); | ||
// or | ||
return api()->setDescription('Description'); | ||
``` | ||
|
||
### setErrors | ||
Set description to response. | ||
```php | ||
return api()->setErrors([ | ||
'first_name' => 'Error message', | ||
'last_name' => ['Error message 1', 'Error message 2'] | ||
]); | ||
``` | ||
|
||
### abort | ||
```php | ||
return api()->abort('Error message', 400); | ||
``` | ||
|
||
### cookie | ||
Add cookie to response. | ||
```php | ||
return api()->cookie(cookie('token', 'asdsadsadas', 60 * 3)); | ||
``` | ||
|
||
### setData | ||
Set data to response. | ||
```php | ||
return api()->setData(['Data #1', 'Data #2']); | ||
// or | ||
return api()->setData('Data'); | ||
``` | ||
|
||
### render | ||
Render response. | ||
```php | ||
return api()->render(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "kolirt/laravel-api-response", | ||
"description": "The package will help to generate json answers.", | ||
"keywords": [ | ||
"laravel", | ||
"json response", | ||
"api", | ||
"rest api" | ||
], | ||
"license": "MIT", | ||
"version": "2.0.3", | ||
"authors": [ | ||
{ | ||
"name": "kolirt" | ||
} | ||
], | ||
"require": {}, | ||
"autoload": { | ||
"files": [ | ||
"src/helpers.php" | ||
], | ||
"psr-4": { | ||
"Kolirt\\ApiResponse\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
namespace Kolirt\ApiResponse; | ||
|
||
class Api | ||
{ | ||
|
||
private $responseCode = 200; | ||
private $responseOk = true; | ||
|
||
private $description = null; | ||
private $errors = null; | ||
private $data = null; | ||
|
||
private $cookies = []; | ||
|
||
/** | ||
* @param int $code | ||
* @return $this | ||
*/ | ||
public function error($code = 400) | ||
{ | ||
$this->setResponseOk(false); | ||
$this->setCode($code); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param int $code | ||
* @return $this | ||
*/ | ||
public function success($code = 200) | ||
{ | ||
$this->setResponseOk(true); | ||
$this->setCode($code); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $text | ||
* @return $this | ||
*/ | ||
public function setDescription($text) | ||
{ | ||
$this->description = $text; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $errors | ||
* @return $this | ||
*/ | ||
public function setErrors($errors, $errored = true) | ||
{ | ||
if ($errored) { | ||
$this->setResponseOk(false); | ||
$this->setCode(422); | ||
} | ||
$this->errors = $errors; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $code | ||
* @return $this | ||
*/ | ||
public function setCode($code) | ||
{ | ||
$this->responseCode = $code; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $data | ||
* @return $this | ||
*/ | ||
public function setData($data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $cookie | ||
* @return $this | ||
*/ | ||
public function cookie($cookie) | ||
{ | ||
$this->cookies[] = $cookie; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function render() | ||
{ | ||
$result = [ | ||
'ok' => $this->getResponseOk(), | ||
]; | ||
|
||
if (!$this->getResponseOk()) | ||
$result['error_code'] = $this->getResponseCode(); | ||
|
||
if ($this->getDescription()) | ||
$result['description'] = $this->getDescription(); | ||
|
||
if ($this->getErrors()) | ||
$result['errors'] = $this->getErrors(); | ||
|
||
if ($this->getData()) | ||
$result['result'] = $this->getData(); | ||
|
||
$response = response()->json($result, $this->getResponseCode()); | ||
|
||
foreach ($this->cookies as $cookie) { | ||
$response->withCookie($cookie); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param $message | ||
* @param int $code | ||
* @return $this | ||
*/ | ||
public function abort($message, $code = 400) | ||
{ | ||
$response = $this->error($code)->setDescription($message)->render(); | ||
abort($response); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->render(); | ||
} | ||
|
||
private function setResponseOk($status) | ||
{ | ||
$this->responseOk = $status; | ||
} | ||
|
||
private function getResponseCode() | ||
{ | ||
return $this->responseCode; | ||
} | ||
|
||
private function getResponseOk() | ||
{ | ||
return $this->responseOk; | ||
} | ||
|
||
private function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
private function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
private function getData() | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
if (!function_exists('api')) { | ||
function api() | ||
{ | ||
return new \Kolirt\ApiResponse\Api; | ||
} | ||
} |