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
|
|
|
$perPage = $this->getValue($data, $this->paginationName['perPage']); |
63
|
|
|
foreach ($this->paginationName as $key => $value) { |
64
|
|
|
if ('totalPages' === $key && $totalItems < $perPage) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
if (null === $this->getValue($data, $value)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return isset($data['_embedded']['item']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $data |
77
|
|
|
* @param ResponseInterface $response |
78
|
|
|
* @param ResponseDefinition $responseDefinition |
79
|
|
|
* |
80
|
|
|
* @return Pagination |
81
|
|
|
*/ |
82
|
|
|
public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination |
83
|
|
|
{ |
84
|
|
|
$paginationLinks = null; |
|
|
|
|
85
|
|
|
$links = $data['_links']; |
86
|
|
|
$paginationLinks = new PaginationLinks( |
87
|
|
|
$links['first']['href'] ?? $links['self']['href'], |
88
|
|
|
$links['last']['href'] ?? $links['self']['href'], |
89
|
|
|
$links['next']['href'] ?? null, |
90
|
|
|
$links['prev']['href'] ?? null |
91
|
|
|
); |
92
|
|
|
$pagination = [ |
93
|
|
|
'page' => $this->getValue($data, $this->paginationName['page']), |
94
|
|
|
'perPage' => $this->getValue($data, $this->paginationName['perPage']), |
95
|
|
|
'totalItems' => $this->getValue($data, $this->paginationName['totalItems']), |
96
|
|
|
]; |
97
|
|
|
$data = array_map(function ($data) { |
98
|
|
|
$relations = array_map(function ($item) { |
99
|
|
|
unset($item['_links']); |
100
|
|
|
|
101
|
|
|
return $item; |
102
|
|
|
}, $data['_embedded'] ?? []); |
103
|
|
|
unset($data['_links'], $data['_embedded']); |
104
|
|
|
|
105
|
|
|
return array_merge($relations, $data); |
106
|
|
|
}, $data['_embedded']['item']); |
107
|
|
|
|
108
|
|
|
return new Pagination( |
109
|
|
|
$pagination['page'], |
|
|
|
|
110
|
|
|
$pagination['perPage'], |
|
|
|
|
111
|
|
|
$pagination['totalItems'], |
|
|
|
|
112
|
|
|
(int) ceil($pagination['totalItems'] / $pagination['perPage']), |
113
|
|
|
$paginationLinks |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $items |
119
|
|
|
* @param array $values |
120
|
|
|
* |
121
|
|
|
* @return array|int|mixed|null |
122
|
|
|
*/ |
123
|
|
|
private function getValue(array $items, array $values) |
124
|
|
|
{ |
125
|
|
|
$value = $items; |
126
|
|
|
foreach ($values as $item) { |
127
|
|
|
if (isset($value[$item])) { |
128
|
|
|
$value = $value[$item]; |
129
|
|
|
} elseif (is_string($value) && 1 === preg_match('#'.$item.'=([^&]*)#', $value, $tab)) { |
130
|
|
|
$value = isset($tab[1]) ? (int) $tab[1] : null; |
131
|
|
|
} elseif (is_string($value) && 0 === preg_match('#'.$item.'=([^&]*)#', $value)) { |
132
|
|
|
return 1; |
133
|
|
|
} else { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $value; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|