Skip to content

Commit 3e6bfcd

Browse files
committed
update readme
1 parent 15d2ada commit 3e6bfcd

File tree

1 file changed

+162
-1
lines changed

1 file changed

+162
-1
lines changed

readme.md

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,168 @@
2222
- *then run `composer install` or `composer update`*
2323

2424
## How To Use
25-
Check the use sample in the [test code](https://github.com/harryosmar/php-restful-api-response/blob/master/tests/unit/ResponseTest.php)
25+
- First instantiate the response object
26+
```
27+
<?php
28+
29+
use PhpRestfulApiResponse\Response;
30+
31+
$response = new Response();
32+
```
33+
- With simple array
34+
```
35+
<?php
36+
echo $response->withArray([
37+
'status' => 'created',
38+
'id' => 1
39+
], 201); //response code 201
40+
```
41+
```json
42+
{
43+
"status": "created",
44+
"id": 1
45+
}
46+
```
47+
- With item/object
48+
```
49+
<?php
50+
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
51+
52+
echo $response->withItem(
53+
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
54+
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
55+
200 //response code 200
56+
);
57+
```
58+
```json
59+
{
60+
"data":
61+
{
62+
"title": "how to be a ninja",
63+
"author":
64+
{
65+
"name": "harry",
66+
"email": "harryosmarsitohang"
67+
},
68+
"year": 2017,
69+
"price": 100000
70+
}
71+
}
72+
```
73+
- With collection of items
74+
```
75+
<?php
76+
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
77+
78+
$response->withCollection(
79+
[
80+
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
81+
new Book('harry', 'harryosmarsitohang', 'how to be a mage', 500000, 2016),
82+
new Book('harry', 'harryosmarsitohang', 'how to be a samurai', 25000, 2000),
83+
],
84+
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
85+
200
86+
);
87+
```
88+
```json
89+
{
90+
"data": [
91+
{
92+
"title": "how to be a ninja",
93+
"author":
94+
{
95+
"name": "harry",
96+
"email": "harryosmarsitohang"
97+
},
98+
"year": 2017,
99+
"price": 100000
100+
},
101+
{
102+
"title": "how to be a mage",
103+
"author":
104+
{
105+
"name": "harry",
106+
"email": "harryosmarsitohang"
107+
},
108+
"year": 2016,
109+
"price": 500000
110+
},
111+
{
112+
"title": "how to be a samurai",
113+
"author":
114+
{
115+
"name": "harry",
116+
"email": "harryosmarsitohang"
117+
},
118+
"year": 2000,
119+
"price": 25000
120+
}]
121+
}
122+
```
123+
- 404 Not Found
124+
```
125+
<?php
126+
echo $response->errorNotFound();
127+
```
128+
```json
129+
{
130+
"error":
131+
{
132+
"http_code": 404,
133+
"phrase": "Not Found"
134+
}
135+
}
136+
```
137+
- 500 Internal Server Error
138+
```
139+
<?php
140+
echo $response->errorInternalError();
141+
```
142+
```json
143+
{
144+
"error":
145+
{
146+
"http_code": 500,
147+
"phrase": "Internal Server Error"
148+
}
149+
}
150+
```
151+
- 400 Bad Request
152+
```
153+
<?php
154+
echo $response->errorWrongArgs([
155+
'username' => 'required',
156+
'password' => 'required'
157+
]);
158+
```
159+
```json
160+
{
161+
"error":
162+
{
163+
"http_code": 400,
164+
"phrase": "Bad Request",
165+
"message":
166+
{
167+
"username": "required",
168+
"password": "required"
169+
}
170+
}
171+
}
172+
```
173+
- 401 Unauthorized
174+
```
175+
<?php
176+
echo $response->errorUnauthorized();
177+
```
178+
```json
179+
{
180+
"error":
181+
{
182+
"http_code": 401,
183+
"phrase": "Unauthorized"
184+
}
185+
}
186+
```
26187

27188
## How To Run The Test
28189
```

0 commit comments

Comments
 (0)