From 6563d2ddad13f9e71cfd331e840a59c45af9355d Mon Sep 17 00:00:00 2001 From: Andrew Zhdanovskih Date: Mon, 28 Mar 2022 13:33:20 +0200 Subject: [PATCH] Fix an issue with the file list pagination (#190) * Fix pagination - add 'from' parameter to request; - add 'getPageRequestParameters' method to generate a previous/next links * Update changelog, update readme Co-authored-by: Roman Sedykh --- CHANGELOG.md | 5 +++++ README.md | 1 + src/Apis/FileApi.php | 17 +++++++++++++++++ src/Interfaces/Api/FileApiInterface.php | 9 +++++++++ 4 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fb0a1d..2f31f1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based now on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [3.2.2] +### Fix for an issue with the file list pagination +- Added `getPageRequestParameters` method: use it to load the next page parameters from `$fileListResponse->getNext()`/`$fileListResponse->getPrevious()`. +- **Attention**: if you've implemented your own class for `FileApiInterface`, add `public getPageRequestParameters(?string $url): array` method to the implementation. + ## [3.2.1] ### Secure delivery image processing fix - Resolved issue with invalid signed urls being generated for image transformations. diff --git a/README.md b/README.md index ed680c7..758ce19 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,7 @@ After that, you can access to file operation methods: $files = $page->getResults(); } ``` +- `getPageRequestParameters(string | null $url)` — Get an array with next page request parameters. Use it to create links to the previous/next page of the list. - `storeFile(string $id)` — Stores a single file by UUID. Returns the `Uploadcare\File` (`FileInfoInterface`). Takes file UUID as an argument. - `deleteFile(string $id)` — Removes individual files. Returns file info. diff --git a/src/Apis/FileApi.php b/src/Apis/FileApi.php index e751f19..689907d 100644 --- a/src/Apis/FileApi.php +++ b/src/Apis/FileApi.php @@ -25,6 +25,22 @@ final class FileApi extends AbstractApi implements FileApiInterface { public const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}'; + public function getPageRequestParameters(?string $url): ?array + { + if ($url === null) { + return null; + } + + $query = \parse_url($url, PHP_URL_QUERY); + if (!\is_string($query) || empty($query)) { + return null; + } + $parameters = null; + \parse_str($query, $parameters); + + return $parameters; + } + /** * {@inheritDoc} */ @@ -67,6 +83,7 @@ public function listFiles(int $limit = 100, string $orderBy = 'datetime_uploaded 'ordering' => $orderBy, 'removed' => $removed, 'add_fields' => $addFields, + 'from' => $from, ]; if (\is_bool($stored)) { $parameters['stored'] = $stored; diff --git a/src/Interfaces/Api/FileApiInterface.php b/src/Interfaces/Api/FileApiInterface.php index 5e2edb6..a676425 100644 --- a/src/Interfaces/Api/FileApiInterface.php +++ b/src/Interfaces/Api/FileApiInterface.php @@ -9,6 +9,15 @@ interface FileApiInterface { + /** + * Makes an array from next/previous url got from API. Use this method to generate links to next or previous pages. + * + * @param string|null $url + * + * @return array|null + */ + public function getPageRequestParameters(?string $url): ?array; + /** * Get the next page from previous answer (if next page exists). *