|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
9
|
|
|
|
|
10
|
|
|
class HalCollection implements MiddlewareInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var int $numPerPage */ |
|
13
|
|
|
private $numPerPage; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* HalCollection constructor. |
|
17
|
|
|
* @param int $numPerPage |
|
18
|
|
|
*/ |
|
19
|
2 |
|
public function __construct(int $numPerPage) |
|
20
|
|
|
{ |
|
21
|
2 |
|
$this->numPerPage = $numPerPage; |
|
22
|
2 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ServerRequestInterface $request |
|
26
|
|
|
* @param RequestHandlerInterface $handler |
|
27
|
|
|
* @return ResponseInterface |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
30
|
|
|
{ |
|
31
|
2 |
|
$params = $request->getQueryParams(); |
|
32
|
2 |
|
$page = isset($params['page']) ? (int) $params['page'] : 1; |
|
33
|
2 |
|
$limit = $this->numPerPage; |
|
34
|
2 |
|
$params['limit'] = $limit; |
|
35
|
2 |
|
$params['offset'] = ($page * $limit) - $limit; |
|
36
|
2 |
|
$request = $request->withQueryParams($params); |
|
37
|
|
|
|
|
38
|
2 |
|
$response = $handler->handle($request); |
|
39
|
|
|
|
|
40
|
2 |
|
$uri = $request->getUri(); |
|
41
|
2 |
|
$data = json_decode($response->getBody()->getContents(), true); |
|
42
|
2 |
|
$pageCount = (int) ceil($data['total'] / $limit); |
|
43
|
|
|
|
|
44
|
|
|
$hal = [ |
|
45
|
|
|
'_links' => [ |
|
46
|
|
|
'self' => [ |
|
47
|
2 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(), |
|
48
|
|
|
], |
|
49
|
|
|
'first' => [ |
|
50
|
2 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(), |
|
51
|
|
|
], |
|
52
|
|
|
], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
2 |
|
if ($page !== 1) { |
|
56
|
1 |
|
$hal['_links']['prev'] = [ |
|
57
|
1 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . ($page - 1), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
if ($page !== $pageCount) { |
|
62
|
1 |
|
$hal['_links']['next'] = [ |
|
63
|
1 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . ($page + 1), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$hal['_links']['last'] = [ |
|
68
|
2 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . $pageCount, |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
/** @todo add _self links for entities in collection */ |
|
72
|
|
|
|
|
73
|
2 |
|
$data = array_merge($hal, $data); |
|
74
|
2 |
|
foreach ($data['_embedded'] as $key => $value) { |
|
75
|
2 |
|
$data['_embedded'][$key]['_links'] = [ |
|
76
|
|
|
'self' => [ |
|
77
|
2 |
|
'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '/' . $value['id'], |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
2 |
|
$body = $response->getBody(); |
|
82
|
2 |
|
$body->rewind(); |
|
83
|
2 |
|
$body->write(json_encode($data)); |
|
84
|
2 |
|
$response = $response->withHeader('Content-Type', 'application/hal+json'); |
|
85
|
|
|
|
|
86
|
2 |
|
return $response->withBody($body); |
|
87
|
|
|
} |
|
88
|
|
|
} |