-
-
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.
- Loading branch information
Showing
7 changed files
with
189 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# rekalogika/pager | ||
|
||
A pagination library for 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,24 @@ | ||
{ | ||
"name": "rekalogika/rekapager-pagerfanta-adapter", | ||
"description": "Adapts Pagerfanta and its adapters for use with Rekapager pagination library", | ||
"homepage": "https://rekalogika.dev/rekapager", | ||
"keywords": [], | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Priyadi Iman Nurcahyo", | ||
"email": "priyadi@rekalogika.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Rekalogika\\Rekapager\\Pagerfanta\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^8.2", | ||
"rekalogika/rekapager-contracts": "^0.5", | ||
"rekalogika/rekapager-offset-pagination": "^0.5" | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/rekapager-pagerfanta-adapter/src/PagerfantaAdapterAdapter.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,61 @@ | ||
<?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\Rekapager\Pagerfanta; | ||
|
||
use Pagerfanta\Adapter\AdapterInterface; | ||
use Rekalogika\Rekapager\Offset\OffsetPaginationAdapterInterface; | ||
|
||
/** | ||
* @template T | ||
* @implements OffsetPaginationAdapterInterface<array-key,T> | ||
*/ | ||
final class PagerfantaAdapterAdapter implements OffsetPaginationAdapterInterface | ||
{ | ||
/** | ||
* @param AdapterInterface<T> $pagerfanta | ||
*/ | ||
public function __construct( | ||
private readonly AdapterInterface $pagerfanta | ||
) { | ||
} | ||
|
||
public function getOffsetItems(int $offset, int $limit): array | ||
{ | ||
$this->pagerfanta->getSlice($offset, $limit); | ||
|
||
/** @psalm-suppress InvalidArgument */ | ||
return iterator_to_array($this->pagerfanta->getSlice($offset, $limit)); | ||
} | ||
|
||
public function countOffsetItems(int $offset = 0, ?int $limit = null): ?int | ||
{ | ||
if ($limit === null) { | ||
return null; | ||
} | ||
|
||
$slice = $this->pagerfanta->getSlice($offset, $limit); | ||
$count = 0; | ||
|
||
foreach ($slice as $item) { | ||
$count++; | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
public function countItems(): int | ||
{ | ||
return $this->pagerfanta->getNbResults(); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
tests/src/App/PageableGenerator/OffsetPageablePagerfantaAdapterAdapter.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,81 @@ | ||
<?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\Rekapager\Tests\App\PageableGenerator; | ||
|
||
use Doctrine\Common\Collections\Criteria; | ||
use Pagerfanta\Doctrine\Collections\SelectableAdapter; | ||
use Rekalogika\Collections\Decorator\LazyMatching\LazyMatchingCollection; | ||
use Rekalogika\Contracts\Rekapager\PageableInterface; | ||
use Rekalogika\Rekapager\Offset\Contracts\PageNumber; | ||
use Rekalogika\Rekapager\Offset\OffsetPageable; | ||
use Rekalogika\Rekapager\Pagerfanta\PagerfantaAdapterAdapter; | ||
use Rekalogika\Rekapager\Tests\App\Contracts\PageableGeneratorInterface; | ||
use Rekalogika\Rekapager\Tests\App\Entity\Post; | ||
use Rekalogika\Rekapager\Tests\App\Repository\UserRepository; | ||
|
||
/** | ||
* @implements PageableGeneratorInterface<int,Post,PageNumber> | ||
*/ | ||
class OffsetPageablePagerfantaAdapterAdapter implements PageableGeneratorInterface | ||
{ | ||
public function __construct(private UserRepository $userRepository) | ||
{ | ||
} | ||
|
||
public static function getKey(): string | ||
{ | ||
return 'offsetpageable-pagerfantaadapteradapter-collection'; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return 'OffsetPageable - PagerfantaAdapterAdapter - PagerfantaSelectableAdapter - Collection'; | ||
} | ||
|
||
public function generatePageable( | ||
int $itemsPerPage, | ||
bool|int $count, | ||
string $setName, | ||
?int $pageLimit = null, | ||
): PageableInterface { | ||
$user = $this->userRepository->findOneBy([]); | ||
if ($user === null) { | ||
throw new \RuntimeException('No user found'); | ||
} | ||
|
||
// @highlight-start | ||
$criteria = Criteria::create() | ||
->where(Criteria::expr()->eq('setName', $setName)); | ||
|
||
$pagerfantaAdapter = new SelectableAdapter($user->getPosts(), $criteria); | ||
$adapter = new PagerfantaAdapterAdapter($pagerfantaAdapter); | ||
|
||
$pageable = new OffsetPageable( | ||
adapter: $adapter, | ||
itemsPerPage: $itemsPerPage, | ||
count: $count, | ||
pageLimit: $pageLimit, | ||
); | ||
// @highlight-end | ||
|
||
// @phpstan-ignore-next-line | ||
return $pageable; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
/** @var int<0,max> */ | ||
return $this->userRepository->count([]); | ||
} | ||
} |
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