|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ElevenLabs\Api\Service\Pagination\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
|
8
|
|
|
use ElevenLabs\Api\Service\Pagination\Pagination; |
|
9
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationLinks; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class HalProvider. |
|
14
|
|
|
*/ |
|
15
|
|
|
class HalProvider implements PaginationProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
const DEFAULT_PAGINATION_VALUE = [ |
|
21
|
|
|
// current page |
|
22
|
|
|
'page' => '_links.self.href.page', |
|
23
|
|
|
|
|
24
|
|
|
// number of items per page |
|
25
|
|
|
'perPage' => 'itemsPerPage', |
|
26
|
|
|
|
|
27
|
|
|
// number of items available in total |
|
28
|
|
|
'totalPages' => '_links.last.href.page', |
|
29
|
|
|
|
|
30
|
|
|
// number of pages available |
|
31
|
|
|
'totalItems' => 'totalItems', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $paginationName; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $config |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $config = []) |
|
43
|
|
|
{ |
|
44
|
|
|
foreach (self::DEFAULT_PAGINATION_VALUE as $name => $value) { |
|
45
|
|
|
if (isset($config[$name])) { |
|
46
|
|
|
$value = $config[$name]; |
|
47
|
|
|
} |
|
48
|
|
|
$this->paginationName[$name] = explode('.', $value); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $data |
|
54
|
|
|
* @param ResponseInterface $response |
|
55
|
|
|
* @param ResponseDefinition $responseDefinition |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition): bool |
|
60
|
|
|
{ |
|
61
|
|
|
$totalItems = $this->getValue($data, $this->paginationName['totalItems']); |
|
62
|
|
|
if (0 === $totalItems) { |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
$perPage = $this->getValue($data, $this->paginationName['perPage']); |
|
66
|
|
|
foreach ($this->paginationName as $key => $value) { |
|
67
|
|
|
if ('totalPages' === $key && $totalItems < $perPage) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
if (null === $this->getValue($data, $value)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return isset($data['_embedded']['item']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @param ResponseInterface $response |
|
81
|
|
|
* @param ResponseDefinition $responseDefinition |
|
82
|
|
|
* |
|
83
|
|
|
* @return Pagination |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination |
|
86
|
|
|
{ |
|
87
|
|
|
$paginationLinks = null; |
|
|
|
|
|
|
88
|
|
|
$links = $data['_links'] ?? []; |
|
89
|
|
|
$paginationLinks = new PaginationLinks( |
|
90
|
|
|
$links['first']['href'] ?? $links['self']['href'] ?? '', |
|
91
|
|
|
$links['last']['href'] ?? $links['self']['href'] ?? '', |
|
92
|
|
|
$links['next']['href'] ?? null, |
|
93
|
|
|
$links['prev']['href'] ?? null |
|
94
|
|
|
); |
|
95
|
|
|
$pagination = [ |
|
96
|
|
|
'page' => $this->getValue($data, $this->paginationName['page']), |
|
97
|
|
|
'perPage' => $this->getValue($data, $this->paginationName['perPage']), |
|
98
|
|
|
'totalItems' => $this->getValue($data, $this->paginationName['totalItems']), |
|
99
|
|
|
]; |
|
100
|
|
|
$data = array_map(function ($data) { |
|
101
|
|
|
$relations = array_map(function ($item) { |
|
102
|
|
|
unset($item['_links']); |
|
103
|
|
|
|
|
104
|
|
|
return $item; |
|
105
|
|
|
}, $data['_embedded'] ?? []); |
|
106
|
|
|
unset($data['_links'], $data['_embedded']); |
|
107
|
|
|
|
|
108
|
|
|
return array_merge($relations, $data); |
|
109
|
|
|
}, $data['_embedded']['item'] ?? []); |
|
110
|
|
|
|
|
111
|
|
|
return new Pagination( |
|
112
|
|
|
$pagination['page'] ?? 1, |
|
|
|
|
|
|
113
|
|
|
$pagination['perPage'], |
|
114
|
|
|
$pagination['totalItems'], |
|
115
|
|
|
(int) ceil($pagination['totalItems'] / $pagination['perPage']), |
|
116
|
|
|
$paginationLinks |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $items |
|
122
|
|
|
* @param array $values |
|
123
|
|
|
* |
|
124
|
|
|
* @return array|int|mixed|null |
|
125
|
|
|
*/ |
|
126
|
|
|
private function getValue(array $items, array $values) |
|
127
|
|
|
{ |
|
128
|
|
|
$value = $items; |
|
129
|
|
|
foreach ($values as $item) { |
|
130
|
|
|
if (isset($value[$item])) { |
|
131
|
|
|
$value = $value[$item]; |
|
132
|
|
|
} elseif (is_string($value) && 1 === preg_match('#'.$item.'=([^&]*)#', $value, $tab)) { |
|
133
|
|
|
$value = isset($tab[1]) ? (int) $tab[1] : null; |
|
134
|
|
|
} elseif (is_string($value) && 0 === preg_match('#'.$item.'=([^&]*)#', $value)) { |
|
135
|
|
|
return 1; |
|
136
|
|
|
} else { |
|
137
|
|
|
return null; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $value; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|