-
-
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.
refactor:
PageableInterface::getPages()
now returns Iterator
inst…
…ead of `Traversable` (non-backward compatible for implementors) (#247)
- Loading branch information
Showing
6 changed files
with
92 additions
and
10 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
76 changes: 76 additions & 0 deletions
76
packages/rekapager-contracts/src/Internal/PageableIterator.php
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Contracts\Rekapager\Internal; | ||
|
||
use Rekalogika\Contracts\Rekapager\Exception\LogicException; | ||
use Rekalogika\Contracts\Rekapager\PageInterface; | ||
|
||
/** | ||
* @template TKey of array-key | ||
* @template-covariant T | ||
* @implements \Iterator<int,PageInterface<TKey,T>> | ||
*/ | ||
final class PageableIterator implements \Iterator | ||
{ | ||
private int $position = 0; | ||
|
||
/** | ||
* @var PageInterface<TKey,T> | ||
*/ | ||
private ?PageInterface $currentPage; | ||
|
||
/** | ||
* @param PageInterface<TKey,T> $startPage | ||
*/ | ||
public function __construct( | ||
private readonly PageInterface $startPage, | ||
) { | ||
$this->currentPage = $this->startPage; | ||
} | ||
|
||
public function current(): mixed | ||
{ | ||
if ($this->currentPage === null) { | ||
throw new LogicException('The iterator is not valid'); | ||
} | ||
|
||
return $this->currentPage; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
if ($this->currentPage === null) { | ||
return; | ||
} | ||
|
||
$this->currentPage = $this->currentPage->getNextPage(); | ||
$this->position++; | ||
} | ||
|
||
public function key(): mixed | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return $this->currentPage !== null; | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->currentPage = $this->startPage; | ||
$this->position = 0; | ||
} | ||
} |
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
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