1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElevenLabs\Api\Service\Pagination\Provider; |
4
|
|
|
|
5
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
6
|
|
|
use ElevenLabs\Api\Service\Pagination\Pagination; |
7
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationLinks; |
8
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationProvider; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class HateoasPagination |
13
|
|
|
* |
14
|
|
|
* @package App\Service\Pagination |
15
|
|
|
*/ |
16
|
|
|
class HateoasPagination implements PaginationProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Default mapping for pagination request headers |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
const DEFAULT_PAGINATION_VALUE = [ |
24
|
|
|
// current page |
25
|
|
|
'page' => 'page', |
26
|
|
|
// number of items per page |
27
|
|
|
'perPage' => 'perPage', |
28
|
|
|
// number of items available in total |
29
|
|
|
'totalItems' => 'totalItems', |
30
|
|
|
// number of pages available |
31
|
|
|
'totalPages' => 'totalPages' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $paginationHeaders; |
38
|
|
|
|
39
|
7 |
|
public function __construct(array $config = []) |
40
|
|
|
{ |
41
|
7 |
|
foreach (self::DEFAULT_PAGINATION_VALUE as $name => $headerName) { |
42
|
7 |
|
if (isset($config[$name])) { |
43
|
7 |
|
$headerName = $config[$name]; |
44
|
|
|
} |
45
|
7 |
|
$this->paginationHeaders[$name] = $headerName; |
46
|
|
|
} |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $data The decoded response body |
51
|
|
|
* @param ResponseInterface $response |
52
|
|
|
* @param ResponseDefinition $responseDefinition |
53
|
|
|
* |
54
|
|
|
* @return Pagination |
55
|
|
|
*/ |
56
|
2 |
|
public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition) |
57
|
|
|
{ |
58
|
2 |
|
$paginationLinks = null; |
59
|
2 |
|
if (!empty($data['_links'])) { |
60
|
1 |
|
$links = self::parseLinks($data['_links']); |
61
|
1 |
|
$paginationLinks = new PaginationLinks($links['first'], $links['last'], $links['next'], $links['prev']); |
62
|
|
|
} |
63
|
2 |
|
$count = (int) $data[$this->paginationHeaders['totalPages']]; |
64
|
2 |
|
$total = (int) $data[$this->paginationHeaders['totalItems']]; |
65
|
2 |
|
$perPage = (int) $data[$this->paginationHeaders['perPage']]; |
66
|
2 |
|
$currentPage = (int) $data[$this->paginationHeaders['page']]; |
67
|
2 |
|
$data = reset($data['_embedded']); |
68
|
|
|
|
69
|
2 |
|
return new Pagination($currentPage, $perPage, $total, $count, $paginationLinks); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Indicate if the pagination is supported |
74
|
|
|
* |
75
|
|
|
* @param array $data The decoded response body |
76
|
|
|
* @param ResponseInterface $response |
77
|
|
|
* @param ResponseDefinition $responseDefinition |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
5 |
|
public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition) |
82
|
|
|
{ |
83
|
5 |
|
foreach ($this->paginationHeaders as $value) { |
84
|
5 |
|
if (!isset($data[$value])) { |
85
|
5 |
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
if (isset($data['_links']) && isset($data['_embedded'])) { |
90
|
2 |
|
$links = $data['_links']; |
91
|
2 |
|
if (isset($links['self']) && isset($links['first']) && isset($links['last'])) { |
92
|
1 |
|
return true; |
93
|
|
|
} |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
private static function parseLinks(array $headerLinks) |
101
|
|
|
{ |
102
|
1 |
|
$links = ['next' => null, 'prev' => null, 'first' => null, 'last' => null]; |
103
|
|
|
|
104
|
1 |
|
foreach ($headerLinks as $name => $headerLink) { |
105
|
1 |
|
if ($name === "self") { |
106
|
1 |
|
continue; |
107
|
|
|
} |
108
|
1 |
|
$links[$name] = $headerLink['href']; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $links; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|