Skip to content

Commit 77a0081

Browse files
committed
update readme
1 parent 5c11e96 commit 77a0081

File tree

1 file changed

+165
-18
lines changed

1 file changed

+165
-18
lines changed

readme.md

+165-18
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,70 @@
2828
- *then run `composer install` or `composer update`*
2929

3030
## How To Use
31-
- First instantiate the response object
31+
32+
Simple example how to use
3233
```
3334
<?php
3435
3536
use PhpRestfulApiResponse\Response;
3637
3738
$response = new Response();
39+
40+
echo $response->withArray([
41+
'status' => 'created',
42+
'id' => 1
43+
], 201); //response code 201
3844
```
39-
- With simple array
45+
response
46+
```json
47+
{
48+
"status": "created",
49+
"id": 1
50+
}
4051
```
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
4166
<?php
67+
/** @var \PhpRestfulApiResponse\Response $response */
4268
echo $response->withArray([
4369
'status' => 'created',
4470
'id' => 1
4571
], 201); //response code 201
4672
```
73+
response
4774
```json
4875
{
4976
"status": "created",
5077
"id": 1
5178
}
5279
```
53-
- With item/object
5480

81+
##### With Item
5582
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
5884
<?php
5985
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
6086

87+
/** @var \PhpRestfulApiResponse\Response $response */
6188
echo $response->withItem(
6289
new Book('harry', '[email protected]', 'how to be a ninja', 100000, 2017),
6390
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
6491
200 //response code 200
6592
);
6693
```
94+
response
6795
```json
6896
{
6997
"data":
@@ -79,11 +107,13 @@ echo $response->withItem(
79107
}
80108
}
81109
```
82-
- With collection of items
83-
```
110+
111+
##### With Collection
112+
```php
84113
<?php
85114
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
86115

116+
/** @var \PhpRestfulApiResponse\Response $response */
87117
$response->withCollection(
88118
[
89119
new Book('harry', '[email protected]', 'how to be a ninja', 100000, 2017),
@@ -94,6 +124,7 @@ $response->withCollection(
94124
200
95125
);
96126
```
127+
response
97128
```json
98129
{
99130
"data": [
@@ -129,25 +160,33 @@ $response->withCollection(
129160
}]
130161
}
131162
```
132-
- 404 Not Found
133-
```
163+
164+
#### Error
165+
166+
##### 403 Forbidden
167+
```php
134168
<?php
169+
/** @var \PhpRestfulApiResponse\Response $response */
135170
echo $response->errorNotFound();
136171
```
172+
response
137173
```json
138174
{
139175
"error":
140176
{
141-
"http_code": 404,
142-
"phrase": "Not Found"
177+
"http_code": 403,
178+
"phrase": "Forbidden"
143179
}
144180
}
145181
```
146-
- 500 Internal Server Error
147-
```
182+
183+
##### 500 Internal Server Error
184+
```php
148185
<?php
186+
/** @var \PhpRestfulApiResponse\Response $response */
149187
echo $response->errorInternalError();
150188
```
189+
response
151190
```json
152191
{
153192
"error":
@@ -157,14 +196,51 @@ echo $response->errorInternalError();
157196
}
158197
}
159198
```
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+
}
161232
```
233+
234+
##### 400 Bad Request
235+
```php
162236
<?php
237+
/** @var \PhpRestfulApiResponse\Response $response */
163238
echo $response->errorWrongArgs([
164239
'username' => 'required',
165240
'password' => 'required'
166241
]);
167242
```
243+
response
168244
```json
169245
{
170246
"error":
@@ -179,21 +255,92 @@ echo $response->errorWrongArgs([
179255
}
180256
}
181257
```
182-
- 401 Unauthorized
183-
```
258+
259+
##### 410 Gone
260+
```php
184261
<?php
185-
echo $response->errorUnauthorized();
262+
/** @var \PhpRestfulApiResponse\Response $response */
263+
echo $response->errorGone();
186264
```
265+
response
187266
```json
188267
{
189268
"error":
190269
{
191-
"http_code": 401,
270+
"http_code": 410,
192271
"phrase": "Unauthorized"
193272
}
194273
}
195274
```
196275

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+
197344
## How To Run The Test
198345
```
199346
composer test

0 commit comments

Comments
 (0)