28
28
- * then run ` composer install ` or ` composer update ` *
29
29
30
30
## How To Use
31
- - First instantiate the response object
31
+
32
+ Simple example how to use
32
33
```
33
34
<?php
34
35
35
36
use PhpRestfulApiResponse\Response;
36
37
37
38
$response = new Response();
39
+
40
+ echo $response->withArray([
41
+ 'status' => 'created',
42
+ 'id' => 1
43
+ ], 201); //response code 201
38
44
```
39
- - With simple array
45
+ response
46
+ ``` json
47
+ {
48
+ "status" : " created" ,
49
+ "id" : 1
50
+ }
40
51
```
52
+
53
+ ## Available Response Format
54
+ * [ with array] ( #with-array )
55
+ * [ with item] ( #with-item )
56
+ * [ with collection] ( #with-collection )
57
+ * [ error] ( #error )
58
+ * [ 403 Forbidden] ( #403-forbidden )
59
+ * [ 500 Internal Server Error] ( #500-internal-server-error )
60
+ * [ 404 Not Found] ( #404-not-found )
61
+ * [ 401 Unauthorized] ( #401-unauthorized )
62
+ * [ 400 Bad Request] ( #400-bad-request )
63
+
64
+ ##### With Array
65
+ ``` php
41
66
<?php
67
+ /** @var \PhpRestfulApiResponse\Response $response */
42
68
echo $response->withArray([
43
69
'status' => 'created',
44
70
'id' => 1
45
71
], 201); //response code 201
46
72
```
73
+ response
47
74
``` json
48
75
{
49
76
"status" : " created" ,
50
77
"id" : 1
51
78
}
52
79
```
53
- - With item/object
54
80
81
+ ##### With Item
55
82
For this sample, we use [ class Book] ( https://github.com/harryosmar/php-restful-api-response/blob/master/tests/unit/Lib/Book.php ) as an item
56
-
57
- ```
83
+ ``` php
58
84
<?php
59
85
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
60
86
87
+ /** @var \PhpRestfulApiResponse\Response $response */
61
88
echo $response->withItem(
62
89
new Book('harry', '
[email protected] ', 'how to be a ninja', 100000, 2017),
63
90
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
64
91
200 //response code 200
65
92
);
66
93
```
94
+ response
67
95
``` json
68
96
{
69
97
"data" :
@@ -79,11 +107,13 @@ echo $response->withItem(
79
107
}
80
108
}
81
109
```
82
- - With collection of items
83
- ```
110
+
111
+ ##### With Collection
112
+ ``` php
84
113
<?php
85
114
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
86
115
116
+ /** @var \PhpRestfulApiResponse\Response $response */
87
117
$response->withCollection(
88
118
[
89
119
new Book('harry', '
[email protected] ', 'how to be a ninja', 100000, 2017),
@@ -94,6 +124,7 @@ $response->withCollection(
94
124
200
95
125
);
96
126
```
127
+ response
97
128
``` json
98
129
{
99
130
"data" : [
@@ -129,25 +160,33 @@ $response->withCollection(
129
160
}]
130
161
}
131
162
```
132
- - 404 Not Found
133
- ```
163
+
164
+ #### Error
165
+
166
+ ##### 403 Forbidden
167
+ ``` php
134
168
<?php
169
+ /** @var \PhpRestfulApiResponse\Response $response */
135
170
echo $response->errorNotFound();
136
171
```
172
+ response
137
173
``` json
138
174
{
139
175
"error" :
140
176
{
141
- "http_code" : 404 ,
142
- "phrase" : " Not Found "
177
+ "http_code" : 403 ,
178
+ "phrase" : " Forbidden "
143
179
}
144
180
}
145
181
```
146
- - 500 Internal Server Error
147
- ```
182
+
183
+ ##### 500 Internal Server Error
184
+ ``` php
148
185
<?php
186
+ /** @var \PhpRestfulApiResponse\Response $response */
149
187
echo $response->errorInternalError();
150
188
```
189
+ response
151
190
``` json
152
191
{
153
192
"error" :
@@ -157,14 +196,51 @@ echo $response->errorInternalError();
157
196
}
158
197
}
159
198
```
160
- - 400 Bad Request
199
+
200
+ ##### 404 Not Found
201
+ ``` php
202
+ <?php
203
+ /** @var \PhpRestfulApiResponse\Response $response */
204
+ echo $response->errorNotFound();
205
+ ```
206
+ response
207
+ ``` json
208
+ {
209
+ "error" :
210
+ {
211
+ "http_code" : 404 ,
212
+ "phrase" : " Not Found"
213
+ }
214
+ }
215
+ ```
216
+
217
+ ##### 401 Unauthorized
218
+ ``` php
219
+ <?php
220
+ /** @var \PhpRestfulApiResponse\Response $response */
221
+ echo $response->errorUnauthorized();
222
+ ```
223
+ response
224
+ ``` json
225
+ {
226
+ "error" :
227
+ {
228
+ "http_code" : 401 ,
229
+ "phrase" : " Unauthorized"
230
+ }
231
+ }
161
232
```
233
+
234
+ ##### 400 Bad Request
235
+ ``` php
162
236
<?php
237
+ /** @var \PhpRestfulApiResponse\Response $response */
163
238
echo $response->errorWrongArgs([
164
239
'username' => 'required',
165
240
'password' => 'required'
166
241
]);
167
242
```
243
+ response
168
244
``` json
169
245
{
170
246
"error" :
@@ -179,21 +255,92 @@ echo $response->errorWrongArgs([
179
255
}
180
256
}
181
257
```
182
- - 401 Unauthorized
183
- ```
258
+
259
+ ##### 410 Gone
260
+ ``` php
184
261
<?php
185
- echo $response->errorUnauthorized();
262
+ /** @var \PhpRestfulApiResponse\Response $response */
263
+ echo $response->errorGone();
186
264
```
265
+ response
187
266
``` json
188
267
{
189
268
"error" :
190
269
{
191
- "http_code" : 401 ,
270
+ "http_code" : 410 ,
192
271
"phrase" : " Unauthorized"
193
272
}
194
273
}
195
274
```
196
275
276
+ ##### 410 Gone
277
+ ``` php
278
+ <?php
279
+ /** @var \PhpRestfulApiResponse\Response $response */
280
+ echo $response->errorGone();
281
+ ```
282
+ response
283
+ ``` json
284
+ {
285
+ "error" :
286
+ {
287
+ "http_code" : 410 ,
288
+ "phrase" : " Gone"
289
+ }
290
+ }
291
+ ```
292
+
293
+ ##### 405 Method Not Allowed
294
+ ``` php
295
+ <?php
296
+ /** @var \PhpRestfulApiResponse\Response $response */
297
+ echo $response->errorMethodNotAllowed();
298
+ ```
299
+ response
300
+ ``` json
301
+ {
302
+ "error" :
303
+ {
304
+ "http_code" : 405 ,
305
+ "phrase" : " Method Not Allowed"
306
+ }
307
+ }
308
+ ```
309
+
310
+ ##### 431 Request Header Fields Too Large
311
+ ``` php
312
+ <?php
313
+ /** @var \PhpRestfulApiResponse\Response $response */
314
+ echo $response->errorUnwillingToProcess();
315
+ ```
316
+ response
317
+ ``` json
318
+ {
319
+ "error" :
320
+ {
321
+ "http_code" : 431 ,
322
+ "phrase" : " Request Header Fields Too Large"
323
+ }
324
+ }
325
+ ```
326
+
327
+ ##### 422 Unprocessable Entity
328
+ ``` php
329
+ <?php
330
+ /** @var \PhpRestfulApiResponse\Response $response */
331
+ echo $response->errorUnprocessable();
332
+ ```
333
+ response
334
+ ``` json
335
+ {
336
+ "error" :
337
+ {
338
+ "http_code" : 422 ,
339
+ "phrase" : " Unprocessable Entity"
340
+ }
341
+ }
342
+ ```
343
+
197
344
## How To Run The Test
198
345
```
199
346
composer test
0 commit comments