Skip to content

Commit 8dd95a9

Browse files
committed
add new response to handle item & collection
1 parent dfb24a0 commit 8dd95a9

File tree

3 files changed

+234
-2
lines changed

3 files changed

+234
-2
lines changed

src/Contracts/Response.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,42 @@
88

99
namespace PhpBootstrap\Contracts;
1010

11+
use League\Fractal\Pagination\Cursor;
1112
use Psr\Http\Message\ResponseInterface;
1213

1314
interface Response extends ResponseInterface
1415
{
16+
/**
17+
* @param $data
18+
* @param $code
19+
* @param array $headers
20+
* @return Response|static
21+
*/
22+
public function withArray(array $data, $code = 200, array $headers = []);
23+
24+
/**
25+
* @param $data
26+
* @param $transformer
27+
* @param int $code
28+
* @param null $resourceKey
29+
* @param array $meta
30+
* @param array $headers
31+
* @return Response
32+
*/
33+
public function withItem($data, $transformer, $code = 200, $resourceKey = null, $meta = [], array $headers = []);
34+
35+
/**
36+
* @param $data
37+
* @param $transformer
38+
* @param int $code
39+
* @param null $resourceKey
40+
* @param Cursor|null $cursor
41+
* @param array $meta
42+
* @param array $headers
43+
* @return Response
44+
*/
45+
public function withCollection($data, $transformer, $code = 200, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = []);
46+
1547
/**
1648
* Generates a response with custom code HTTP header and a given message.
1749
*
@@ -48,4 +80,58 @@ public function errorInternalError($message = '', array $headers = []);
4880
* @return mixed
4981
*/
5082
public function errorNotFound($message = '', array $headers = []);
83+
84+
/**
85+
* Generates a response with a 401 HTTP header and a given message.
86+
*
87+
* @param string $message
88+
* @param array $headers
89+
* @return mixed
90+
*/
91+
public function errorUnauthorized($message = '', array $headers = []);
92+
93+
/**
94+
* Generates a response with a 400 HTTP header and a given message.
95+
*
96+
* @param array $message
97+
* @param array $headers
98+
* @return mixed
99+
*/
100+
public function errorWrongArgs(array $message, array $headers = []);
101+
102+
/**
103+
* Generates a response with a 410 HTTP header and a given message.
104+
*
105+
* @param string $message
106+
* @param array $headers
107+
* @return mixed
108+
*/
109+
public function errorGone($message = '', array $headers = []);
110+
111+
/**
112+
* Generates a response with a 405 HTTP header and a given message.
113+
*
114+
* @param string $message
115+
* @param array $headers
116+
* @return mixed
117+
*/
118+
public function errorMethodNotAllowed($message = '', array $headers = []);
119+
120+
/**
121+
* Generates a Response with a 431 HTTP header and a given message.
122+
*
123+
* @param string $message
124+
* @param array $headers
125+
* @return mixed
126+
*/
127+
public function errorUnwillingToProcess($message = '', array $headers = []);
128+
129+
/**
130+
* Generates a Response with a 422 HTTP header and a given message.
131+
*
132+
* @param string $message
133+
* @param array $headers
134+
* @return mixed
135+
*/
136+
public function errorUnprocessable($message = '', array $headers = []);
51137
}

src/Middleware/ExampleMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ExampleMiddleware
1717
public function checkToken(ServerRequestInterface $request, ResponseInterface $response, callable $next)
1818
{
1919
if (!preg_match('/access_token/', $request->getUri()->getQuery())) {
20-
return $response->errorForbidden();
20+
return $response->errorUnauthorized();
2121
}
2222

2323
return $next($request, $response);

src/Services/Response.php

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
namespace PhpBootstrap\Services;
1010

11+
use League\Fractal\Manager;
12+
use League\Fractal\Pagination\Cursor;
13+
use League\Fractal\Resource\Collection;
14+
use League\Fractal\Resource\Item;
15+
use League\Fractal\TransformerAbstract;
1116
use Zend\Diactoros\MessageTrait;
1217
use InvalidArgumentException;
1318

@@ -147,10 +152,79 @@ public function withStatus($code, $reasonPhrase = '')
147152
return $new;
148153
}
149154

155+
/**
156+
* @param $data
157+
* @param $code
158+
* @param array $headers
159+
* @return Response|static
160+
*/
161+
public function withArray(array $data, $code = 200, array $headers = [])
162+
{
163+
$new = clone $this;
164+
$new->setStatusCode($code);
165+
$new->getBody()->write(json_encode([$data]));
166+
$new = $new->withHeader('Content-Type', 'application/json');
167+
$new->headers = array_merge($new->headers, $headers);
168+
return $new;
169+
}
170+
171+
/**
172+
* @param $data
173+
* @param TransformerAbstract|callable $transformer
174+
* @param int $code
175+
* @param null $resourceKey
176+
* @param array $meta
177+
* @param array $headers
178+
* @return Response
179+
*/
180+
public function withItem($data, $transformer, $code = 200, $resourceKey = null, $meta = [], array $headers = [])
181+
{
182+
$resource = new Item($data, $transformer, $resourceKey);
183+
184+
foreach ($meta as $metaKey => $metaValue) {
185+
$resource->setMetaValue($metaKey, $metaValue);
186+
}
187+
188+
$manager = new Manager();
189+
190+
$rootScope = $manager->createData($resource);
191+
192+
return $this->withArray($rootScope->toArray(), $code, $headers);
193+
}
194+
195+
/**
196+
* @param $data
197+
* @param TransformerAbstract|callable $transformer
198+
* @param int $code
199+
* @param null $resourceKey
200+
* @param Cursor|null $cursor
201+
* @param array $meta
202+
* @param array $headers
203+
* @return Response
204+
*/
205+
public function withCollection($data, $transformer, $code = 200, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = [])
206+
{
207+
$resource = new Collection($data, $transformer, $resourceKey);
208+
209+
foreach ($meta as $metaKey => $metaValue) {
210+
$resource->setMetaValue($metaKey, $metaValue);
211+
}
212+
213+
if (!is_null($cursor)) {
214+
$resource->setCursor($cursor);
215+
}
216+
217+
$manager = new Manager();
218+
219+
$rootScope = $manager->createData($resource);
220+
221+
return $this->withArray($rootScope->toArray(), $code, $headers);
222+
}
223+
150224
/**
151225
* Response for errors
152226
*
153-
* @param string $message
227+
* @param string|array $message
154228
* @param string $code
155229
* @param array $headers
156230
* @return mixed
@@ -211,6 +285,78 @@ public function errorNotFound($message = '', array $headers = [])
211285
return $this->withError($message, 404, $headers);
212286
}
213287

288+
/**
289+
* Generates a response with a 401 HTTP header and a given message.
290+
*
291+
* @param string $message
292+
* @param array $headers
293+
* @return mixed
294+
*/
295+
public function errorUnauthorized($message = '', array $headers = [])
296+
{
297+
return $this->withError($message, 401, $headers);
298+
}
299+
300+
/**
301+
* Generates a response with a 400 HTTP header and a given message.
302+
*
303+
* @param array $message
304+
* @param array $headers
305+
* @return mixed
306+
*/
307+
public function errorWrongArgs(array $message, array $headers = [])
308+
{
309+
return $this->withError($message, 400, $headers);
310+
}
311+
312+
/**
313+
* Generates a response with a 410 HTTP header and a given message.
314+
*
315+
* @param string $message
316+
* @param array $headers
317+
* @return mixed
318+
*/
319+
public function errorGone($message = '', array $headers = [])
320+
{
321+
return $this->withError($message, 410, $headers);
322+
}
323+
324+
/**
325+
* Generates a response with a 405 HTTP header and a given message.
326+
*
327+
* @param string $message
328+
* @param array $headers
329+
* @return mixed
330+
*/
331+
public function errorMethodNotAllowed($message = '', array $headers = [])
332+
{
333+
return $this->withError($message, 405, $headers);
334+
}
335+
336+
/**
337+
* Generates a Response with a 431 HTTP header and a given message.
338+
*
339+
* @param string $message
340+
* @param array $headers
341+
* @return mixed
342+
*/
343+
public function errorUnwillingToProcess($message = '', array $headers = [])
344+
{
345+
return $this->withError($message, 431, $headers);
346+
}
347+
348+
/**
349+
* Generates a Response with a 422 HTTP header and a given message.
350+
*
351+
* @param string $message
352+
* @param array $headers
353+
* @return mixed
354+
*/
355+
public function errorUnprocessable($message = '', array $headers = [])
356+
{
357+
return $this->withError($message, 422, $headers);
358+
}
359+
214360
/**
215361
* Set a valid status code.
216362
*

0 commit comments

Comments
 (0)