Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 554cf71

Browse files
author
Andrey Helldar
committed
Added ability to disable content wrapping in data key
1 parent 963ce87 commit 554cf71

File tree

4 files changed

+359
-28
lines changed

4 files changed

+359
-28
lines changed

README.md

Lines changed: 220 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ Package for standardizing the responses from the API of your **Symfony based** a
1717
</p>
1818

1919

20+
## Content
21+
* [Installation](#installation)
22+
* [Using](#using)
23+
* [as NULL with code](#as-null-with-code)
24+
* [as integer with default code](#as-integer-with-default-code)
25+
* [as string with default code](#as-string-with-default-code)
26+
* [as string with code](#as-string-with-code)
27+
* [as integer with code](#as-integer-with-code)
28+
* [as array](#as-array)
29+
* [with additional content](#with-additional-content)
30+
* [Use without `data` key](#use-without-data-key)
31+
* [as NULL with code](#as-null-with-code-and-without-data-key)
32+
* [as integer with default code](#as-integer-with-default-code-and-without-data-key)
33+
* [as string with default code](#as-string-with-default-code-and-without-data-key)
34+
* [as string with code](#as-string-with-code-and-without-data-key)
35+
* [as integer with code](#as-integer-with-code-and-without-data-key)
36+
* [as array](#as-array-and-without-data-key)
37+
* [with additional content](#with-additional-content-and-without-data-key)
38+
* [Using in Laravel 5+ framework](#using-in-laravel-5-framework)
39+
* [Copyright and License](#copyright-and-license)
40+
41+
2042
## Installation
2143

2244
To get the latest version of `API Response`, simply require the project using [Composer](https://getcomposer.org/):
@@ -51,44 +73,44 @@ Alright! Use `api_response()` helper.
5173

5274
## Using
5375

54-
### returned NULL with code:
76+
### as NULL with code:
5577
```php
5678
return api_response(null, 304);
5779
```
58-
returned with code 304:
80+
return with code 304:
5981
```json
6082
{
6183
"data": null
6284
}
6385
```
6486

65-
### returned integer with default code:
87+
### as integer with default code:
6688
```php
6789
return api_response(304);
6890
```
69-
returned with code 200:
91+
return with code 200:
7092
```json
7193
{
7294
"data": 304
7395
}
7496
```
7597

76-
### returned string with default code:
98+
### as string with default code:
7799
```php
78100
return api_response('qwerty');
79101
```
80-
returned with code 200:
102+
return with code 200:
81103
```json
82104
{
83105
"data": "qwerty"
84106
}
85107
```
86108

87-
### returned string with code:
109+
### as string with code:
88110
```php
89111
return api_response('qwerty', 400);
90112
```
91-
returned with code 400:
113+
return with code 400:
92114
```json
93115
{
94116
"error": {
@@ -98,11 +120,11 @@ returned with code 400:
98120
}
99121
```
100122

101-
### returned integer with code:
123+
### as integer with code:
102124
```php
103125
return api_response(304, 400);
104126
```
105-
returned with code 400:
127+
return with code 400:
106128
```json
107129
{
108130
"error": {
@@ -112,7 +134,7 @@ returned with code 400:
112134
}
113135
```
114136

115-
### returned array:
137+
### as array:
116138
```php
117139
$data = [
118140
[
@@ -130,7 +152,7 @@ $data = [
130152
```php
131153
return api_response($data, 400);
132154
```
133-
returned with code 400:
155+
return with code 400:
134156
```json
135157
{
136158
"error": {
@@ -153,7 +175,7 @@ returned with code 400:
153175
```php
154176
return api_response($data, 200);
155177
```
156-
returned with code 200:
178+
return with code 200:
157179
```json
158180
{
159181
"data": [
@@ -169,22 +191,199 @@ returned with code 200:
169191
}
170192
```
171193

172-
If the first parameter is a number, then the decryption of the error by code will be returned. In other cases, the value of the passed variable will be returned.
194+
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
195+
196+
### with additional content
197+
```php
198+
return api_response('title', 200, [], ['foo' => 'bar']);
199+
```
200+
return with code 200:
201+
```json
202+
{
203+
"data": "title",
204+
"foo": "bar"
205+
}
206+
```
207+
208+
return with code 400:
209+
```json
210+
{
211+
"error": {
212+
"code": 400,
213+
"data":"ok"
214+
},
215+
"foo": "bar"
216+
}
217+
```
218+
219+
220+
```php
221+
return api_response(['data' => 'foo', 'bar' => 'baz']);
222+
```
223+
224+
return with code 200:
225+
```json
226+
{
227+
"data": "foo",
228+
"bar": "baz"
229+
}
230+
```
231+
232+
return with code 400:
233+
```json
234+
{
235+
"error": {
236+
"code": 400,
237+
"data":"foo"
238+
},
239+
"bar": "baz"
240+
}
241+
```
242+
243+
244+
## Use without `data` key
245+
246+
If you do not want to wrap the response in the `data` key, you need to write your helper that calls the service or use `Helldar\ApiResponse\Services\Response` class:
247+
248+
```php
249+
use Helldar\ApiResponse\Services\Response;
250+
251+
function api_response($data = null, int $status_code = 200, array $headers = [], array $with = [])
252+
{
253+
return Response::init()
254+
->headers($headers)
255+
->data($data, false)
256+
->with($with)
257+
->status($status_code)
258+
->response();
259+
}
260+
```
261+
262+
### as NULL with code and without `data` key:
263+
```php
264+
return api_response(null, 304);
265+
```
266+
return with code 304:
267+
```json
268+
{}
269+
```
270+
271+
### as integer with default code and without `data` key:
272+
```php
273+
return api_response(304);
274+
```
275+
return with code 200:
276+
```json
277+
304
278+
```
279+
280+
### as string with default code and without `data` key:
281+
```php
282+
return api_response('qwerty');
283+
```
284+
return with code 200:
285+
```json
286+
"qwerty"
287+
```
288+
289+
### as string with code and without `data` key:
290+
```php
291+
return api_response('qwerty', 400);
292+
```
293+
return with code 400:
294+
```json
295+
{
296+
"error": {
297+
"code": 400,
298+
"data": "qwerty"
299+
}
300+
}
301+
```
302+
303+
### as integer with code and without `data` key:
304+
```php
305+
return api_response(304, 400);
306+
```
307+
return with code 400:
308+
```json
309+
{
310+
"error": {
311+
"code": 400,
312+
"data": 304
313+
}
314+
}
315+
```
173316

317+
### as array and without `data` key:
318+
```php
319+
$data = [
320+
[
321+
'title' => 'Title #1',
322+
'description' => 'Description #1',
323+
],
324+
[
325+
'title' => 'Title #2',
326+
'description' => 'Description #2',
327+
],
328+
];
329+
```
174330

175-
## with additional content
331+
#### as error and without `data` key
332+
```php
333+
return api_response($data, 400);
334+
```
335+
return with code 400:
336+
```json
337+
{
338+
"error": {
339+
"code": 400,
340+
"data": [
341+
{
342+
"title": "Title #1",
343+
"description": "Description #1"
344+
},
345+
{
346+
"title": "Title #2",
347+
"description": "Description #2"
348+
}
349+
]
350+
}
351+
}
352+
```
353+
354+
#### as success and without `data` key
355+
```php
356+
return api_response($data, 200);
357+
```
358+
return with code 200:
359+
```json
360+
[
361+
{
362+
"title": "Title #1",
363+
"description": "Description #1"
364+
},
365+
{
366+
"title": "Title #2",
367+
"description": "Description #2"
368+
}
369+
]
370+
```
371+
372+
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
373+
374+
### with additional content and without `data` key:
176375
```php
177376
return api_response('title', 200, [], ['foo' => 'bar']);
178377
```
179-
returned with code 200:
378+
return with code 200:
180379
```json
181380
{
182381
"data": "title",
183382
"foo": "bar"
184383
}
185384
```
186385

187-
returned with code 400:
386+
return with code 400:
188387
```json
189388
{
190389
"error": {
@@ -197,18 +396,18 @@ returned with code 400:
197396

198397

199398
```php
200-
return api_response(['data' => 'foo', 'bar' => 'baz]);
399+
return api_response(['data' => 'foo', 'bar' => 'baz']);
201400
```
202401

203-
returned with code 200:
402+
return with code 200:
204403
```json
205404
{
206405
"data": "foo",
207406
"bar": "baz"
208407
}
209408
```
210409

211-
returned with code 400:
410+
return with code 400:
212411
```json
213412
{
214413
"error": {
@@ -219,6 +418,7 @@ returned with code 400:
219418
}
220419
```
221420

421+
222422
### Using in Laravel 5+ framework
223423

224424
To use you need to add three methods to the file `app/Exceptions/Handler.php`:

0 commit comments

Comments
 (0)