eleven-labs /
api-service
| 1 | <?php |
||
| 2 | namespace ElevenLabs\Api\Service\Pagination\Provider; |
||
| 3 | |||
| 4 | use ElevenLabs\Api\Definition\ResponseDefinition; |
||
| 5 | use ElevenLabs\Api\Service\Pagination\Pagination; |
||
| 6 | use ElevenLabs\Api\Service\Pagination\PaginationLinks; |
||
| 7 | use ElevenLabs\Api\Service\Pagination\PaginationProvider; |
||
| 8 | use Psr\Http\Message\ResponseInterface; |
||
| 9 | |||
| 10 | class PaginationHeader implements PaginationProvider |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Default mapping for pagination request headers |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | const DEFAULT_PAGINATION_HEADERS = [ |
||
| 18 | // current page |
||
| 19 | 'page' => 'X-Page', |
||
| 20 | // number of items per page |
||
| 21 | 'perPage' => 'X-Per-Page', |
||
| 22 | // number of items available in total |
||
| 23 | 'totalItems' => 'X-Total-Items', |
||
| 24 | // number of pages available |
||
| 25 | 'totalPages' => 'X-Total-Pages' |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $paginationHeaders; |
||
| 32 | |||
| 33 | 4 | public function __construct(array $config = []) |
|
| 34 | { |
||
| 35 | 4 | foreach (self::DEFAULT_PAGINATION_HEADERS as $name => $headerName) { |
|
| 36 | 4 | if (isset($config[$name])) { |
|
| 37 | 1 | $headerName = $config[$name]; |
|
| 38 | } |
||
| 39 | 4 | $this->paginationHeaders[$name] = $headerName; |
|
| 40 | } |
||
| 41 | 4 | } |
|
| 42 | |||
| 43 | /** {@inheritdoc} */ |
||
| 44 | 3 | public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition) |
|
| 45 | { |
||
| 46 | 3 | $paginationLinks = null; |
|
| 47 | 3 | if ($response->hasHeader('Link')) { |
|
| 48 | 1 | $links = self::parseHeaderLinks($response->getHeader('Link')); |
|
| 49 | 1 | $paginationLinks = new PaginationLinks( |
|
| 50 | 1 | $links['first'], |
|
| 51 | 1 | $links['last'], |
|
| 52 | 1 | $links['next'], |
|
| 53 | 1 | $links['prev'] |
|
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | 3 | return new Pagination( |
|
| 58 | 3 | (int) $response->getHeaderLine($this->paginationHeaders['page']), |
|
| 59 | 3 | (int) $response->getHeaderLine($this->paginationHeaders['perPage']), |
|
| 60 | 3 | (int) $response->getHeaderLine($this->paginationHeaders['totalItems']), |
|
| 61 | 3 | (int) $response->getHeaderLine($this->paginationHeaders['totalPages']), |
|
| 62 | 3 | $paginationLinks |
|
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** {@inheritdoc} */ |
||
| 67 | 1 | public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition) |
|
| 68 | { |
||
| 69 | 1 | $support = true; |
|
| 70 | 1 | foreach ($this->paginationHeaders as $headerName) { |
|
| 71 | 1 | $support = $support & ($response->getHeaderLine($headerName) !== ''); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 72 | } |
||
| 73 | |||
| 74 | 1 | return (bool) $support; |
|
| 75 | } |
||
| 76 | |||
| 77 | 1 | private static function parseHeaderLinks(array $headerLinks) |
|
| 78 | { |
||
| 79 | 1 | $links = ['next' => null, 'prev' => null]; |
|
| 80 | |||
| 81 | 1 | foreach ($headerLinks as $headerLink) { |
|
| 82 | 1 | preg_match('/rel="([^"]+)"/', $headerLink, $matches); |
|
| 83 | 1 | if (isset($matches[1]) && in_array($matches[1], ['next', 'prev', 'first', 'last'])) { |
|
| 84 | 1 | $parts = explode(';', $headerLink); |
|
| 85 | 1 | $url = trim($parts[0], " <>"); |
|
| 86 | 1 | $links[$matches[1]] = $url; |
|
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | 1 | return $links; |
|
| 91 | } |
||
| 92 | } |
||
| 93 |