Skip to content

Commit 2027e89

Browse files
authored
Merge pull request #19 from j0k3r/2-pager
Update information about the pager
2 parents 531e853 + 84f7f98 commit 2027e89

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,38 @@ $client->api('image')->upload($imageData);
144144

145145
### Pagination
146146

147-
And about the **pagination**, for any API call that supports pagination and is not explicitly available via the method parameters, it can be achieved by using the `BasicPager` object and passing it as the second parameter in the `api()` call.
147+
For any API call that supports pagination and is not explicitly available via the method parameters, it can be achieved by using the `BasicPager` object and passing it as the second parameter in the `api()` call.
148148

149149
```php
150-
$pager = new \Imgur\Pager\BasicPager(0, 1);
150+
$pager = new \Imgur\Pager\BasicPager(1, 10);
151151
$images = $client->api('account', $pager)->images();
152152
```
153153

154+
Here is a real life example if you want to retrieve all your available images of an account:
155+
156+
```php
157+
$page = 1;
158+
$pager = new \Imgur\Pager\BasicPager();
159+
$res = $client->api('account', $pager)->images();
160+
161+
while (!empty($res)) {
162+
// var_dump(count($res));
163+
164+
$pager->setPage($page++);
165+
166+
$res = $client->api('account', $pager)->images();
167+
}
168+
```
169+
170+
This pager is really basic:
171+
172+
- You won't have information about how many pages are available
173+
- If you request a non-existant page, you'll get an empty array
174+
175+
NOTE: `/gallery` endpoints do not support the `perPage` query string, and `/album/{id}/images` is not paged.
176+
177+
Please, read the [Imgur doc about it](https://api.imgur.com/#paging_results).
178+
154179
### Image id or Album id ?
155180

156181
When you got an Imgur link it's almost impossible to be 100% sure if it's an image or an album.

lib/Imgur/Pager/BasicPager.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/**
66
* Basic Pager.
77
*
8+
* @see https://api.imgur.com/#paging_results
9+
*
810
* @author Adrian Ghiuta <adrian.ghiuta@gmail.com>
911
*/
1012
class BasicPager implements PagerInterface
@@ -14,13 +16,8 @@ class BasicPager implements PagerInterface
1416

1517
public function __construct($page = 1, $resultsPerPage = 10)
1618
{
17-
if (!empty($page)) {
18-
$this->setPage($page);
19-
}
20-
21-
if (!empty($resultsPerPage)) {
22-
$this->setResultsPerPage($resultsPerPage);
23-
}
19+
$this->setPage($page ?: 1);
20+
$this->setResultsPerPage($resultsPerPage ?: 10);
2421

2522
return $this;
2623
}

0 commit comments

Comments
 (0)