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
|
11 |
|
public function __construct(array $config = []) |
43
|
|
|
{ |
44
|
11 |
|
foreach (self::DEFAULT_PAGINATION_VALUE as $name => $value) { |
45
|
11 |
|
if (isset($config[$name])) { |
46
|
11 |
|
$value = $config[$name]; |
47
|
|
|
} |
48
|
11 |
|
$this->paginationName[$name] = explode('.', $value); |
49
|
|
|
} |
50
|
11 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $data |
54
|
|
|
* @param ResponseInterface $response |
55
|
|
|
* @param ResponseDefinition $responseDefinition |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
7 |
|
public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition): bool |
60
|
|
|
{ |
61
|
7 |
|
foreach ($this->paginationName as $key => $value) { |
62
|
7 |
|
if (null === $this->getValue($data, $value)) { |
63
|
5 |
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return isset($data['_embedded']['item']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $data |
72
|
|
|
* @param ResponseInterface $response |
73
|
|
|
* @param ResponseDefinition $responseDefinition |
74
|
|
|
* |
75
|
|
|
* @return Pagination |
76
|
|
|
*/ |
77
|
4 |
|
public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination |
78
|
|
|
{ |
79
|
4 |
|
$paginationLinks = null; |
|
|
|
|
80
|
4 |
|
$links = $data['_links']; |
81
|
4 |
|
$paginationLinks = new PaginationLinks( |
82
|
4 |
|
$links['first']['href'] ?? $links['self']['href'], |
83
|
4 |
|
$links['last']['href'] ?? $links['self']['href'], |
84
|
4 |
|
$links['next']['href'] ?? null, |
85
|
4 |
|
$links['prev']['href'] ?? null |
86
|
|
|
); |
87
|
|
|
$pagination = [ |
88
|
4 |
|
'page' => $this->getValue($data, $this->paginationName['page']), |
89
|
4 |
|
'perPage' => $this->getValue($data, $this->paginationName['perPage']), |
90
|
4 |
|
'totalItems' => $this->getValue($data, $this->paginationName['totalItems']), |
91
|
|
|
]; |
92
|
|
|
$data = array_map(function ($data) { |
93
|
|
|
$relations = array_map(function ($item) { |
94
|
4 |
|
unset($item['_links']); |
95
|
|
|
|
96
|
4 |
|
return $item; |
97
|
4 |
|
}, $data['_embedded'] ?? []); |
98
|
4 |
|
unset($data['_links'], $data['_embedded']); |
99
|
|
|
|
100
|
4 |
|
return array_merge($relations, $data); |
101
|
4 |
|
}, $data['_embedded']['item']); |
102
|
|
|
|
103
|
4 |
|
return new Pagination( |
104
|
4 |
|
$pagination['page'], |
|
|
|
|
105
|
4 |
|
$pagination['perPage'], |
|
|
|
|
106
|
4 |
|
$pagination['totalItems'], |
|
|
|
|
107
|
4 |
|
(int) ceil($pagination['totalItems'] / $pagination['perPage']), |
108
|
4 |
|
$paginationLinks |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $items |
114
|
|
|
* @param array $values |
115
|
|
|
* |
116
|
|
|
* @return array|int|mixed|null |
117
|
|
|
*/ |
118
|
11 |
|
private function getValue(array $items, array $values) |
119
|
|
|
{ |
120
|
11 |
|
$value = $items; |
121
|
11 |
|
foreach ($values as $item) { |
122
|
11 |
|
if (isset($value[$item])) { |
123
|
9 |
|
$value = $value[$item]; |
124
|
11 |
|
} elseif (is_string($value) && 1 === preg_match('#'.$item.'=([^&]*)#', $value, $tab)) { |
125
|
6 |
|
$value = isset($tab[1]) ? (int) $tab[1] : null; |
126
|
6 |
|
} elseif (is_string($value) && 0 === preg_match('#'.$item.'=([^&]*)#', $value)) { |
127
|
1 |
|
return 1; |
128
|
|
|
} else { |
129
|
5 |
|
return null; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
7 |
|
return $value; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|