generated from cerbero90/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support paginations aware of their total items
- Loading branch information
Showing
4 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cerbero\LazyJsonPages\Paginations; | ||
|
||
use Cerbero\LazyJsonPages\Exceptions\InvalidKeyException; | ||
use Traversable; | ||
|
||
/** | ||
* The pagination aware of the total number of items. | ||
*/ | ||
class TotalItemsAwarePagination extends LengthAwarePagination | ||
{ | ||
/** | ||
* Determine whether the configuration matches this pagination. | ||
*/ | ||
public function matches(): bool | ||
{ | ||
return $this->config->totalItemsKey !== null | ||
&& $this->config->totalPagesKey === null | ||
&& $this->config->perPage === null; | ||
} | ||
|
||
/** | ||
* Yield the paginated items. | ||
* | ||
* @return Traversable<int, mixed> | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
$perPage = 0; | ||
$generator = $this->yieldItemsAndReturnKey($this->source->response(), $this->config->totalItemsKey); | ||
|
||
foreach ($generator as $item) { | ||
yield $item; | ||
++$perPage; | ||
} | ||
|
||
if (!is_numeric($totalItems = $generator->getReturn())) { | ||
throw new InvalidKeyException($this->config->totalItemsKey); | ||
} | ||
|
||
$totalPages = $perPage > 0 ? (int) ceil(intval($totalItems) / $perPage) : 0; | ||
|
||
yield from $this->yieldItemsUntilPage($totalPages); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Cerbero\LazyJsonPages\LazyJsonPages; | ||
|
||
it('supports paginations aware of their total pages', function () { | ||
$expectedItems = require fixture('items.php'); | ||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users') | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests($expectedItems, [ | ||
'https://example.com/api/v1/users' => 'lengthAware/page1.json', | ||
'https://example.com/api/v1/users?page=2' => 'lengthAware/page2.json', | ||
'https://example.com/api/v1/users?page=3' => 'lengthAware/page3.json', | ||
]); | ||
}); | ||
|
||
it('supports paginations aware of their total items', function () { | ||
$expectedItems = require fixture('items.php'); | ||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users') | ||
->totalItems('meta.total_items') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests($expectedItems, [ | ||
'https://example.com/api/v1/users' => 'lengthAware/page1.json', | ||
'https://example.com/api/v1/users?page=2' => 'lengthAware/page2.json', | ||
'https://example.com/api/v1/users?page=3' => 'lengthAware/page3.json', | ||
]); | ||
}); |