Skip to content

Commit

Permalink
feat: offset pagination support (#132)
Browse files Browse the repository at this point in the history
* feat: offset pagination support

* add tests

* orm

* update
  • Loading branch information
priyadi authored Jul 22, 2024
1 parent b253cc6 commit 1668337
Show file tree
Hide file tree
Showing 39 changed files with 393 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 0.10.1
## 0.11.0

* docs: update readme
* feat: offset pagination support

## 0.10.0

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test: composer-dump phpstan psalm phpunit

.PHONY: composer-dump
composer-dump:
$(COMPOSER) dump-autoload
$(COMPOSER) dump-autoload --optimize

.PHONY: phpstan
phpstan:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"doctrine/collections": "^2.2.2 || ^2.3",
"doctrine/orm": "^2.14 || ^3.0",
"php": "^8.2",
"rekalogika/rekapager-contracts": "^0.14",
"rekalogika/rekapager-doctrine-collections-adapter": "^0.14",
"rekalogika/rekapager-doctrine-orm-adapter": "^0.14",
"rekalogika/rekapager-keyset-pagination": "^0.14"
"rekalogika/rekapager-contracts": "^0.15",
"rekalogika/rekapager-doctrine-collections-adapter": "^0.15",
"rekalogika/rekapager-doctrine-orm-adapter": "^0.15",
"rekalogika/rekapager-keyset-pagination": "^0.15"
},
"require-dev": {
"ekino/phpstan-banned-code": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/collections-common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"require": {
"php": "^8.2",
"doctrine/collections": "^2.2.2 || ^2.3",
"rekalogika/rekapager-contracts": "^0.14"
"rekalogika/rekapager-contracts": "^0.15"
}
}
2 changes: 2 additions & 0 deletions packages/collections-common/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ private function __construct()
* @var int<1,max>
*/
public static int $defaultItemsPerPage = 50;

public static Pagination $defaultPagination = Pagination::Keyset;
}
20 changes: 20 additions & 0 deletions packages/collections-common/src/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/collections 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\Domain\Collections\Common;

enum Pagination
{
case Keyset;
case Offset;
}
2 changes: 1 addition & 1 deletion packages/collections-contracts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"require": {
"php": "^8.2",
"doctrine/collections": "^2.2.2 || ^2.3",
"rekalogika/rekapager-contracts": "^0.14"
"rekalogika/rekapager-contracts": "^0.15"
}
}
6 changes: 3 additions & 3 deletions packages/collections-domain/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"doctrine/collections": "^2.2.2 || ^2.3",
"rekalogika/collections-common": "^0.10",
"rekalogika/collections-contracts": "^0.10",
"rekalogika/rekapager-contracts": "^0.14",
"rekalogika/rekapager-doctrine-collections-adapter": "^0.14",
"rekalogika/rekapager-keyset-pagination": "^0.14"
"rekalogika/rekapager-contracts": "^0.15",
"rekalogika/rekapager-doctrine-collections-adapter": "^0.15",
"rekalogika/rekapager-keyset-pagination": "^0.15"
}
}
5 changes: 5 additions & 0 deletions packages/collections-domain/src/CriteriaPageable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\PageableTrait;
use Rekalogika\Domain\Collections\Common\Trait\RefreshCountTrait;
use Rekalogika\Domain\Collections\Trait\RecollectionPageableTrait;
Expand Down Expand Up @@ -69,6 +70,7 @@ final private function __construct(
?string $indexBy = null,
?int $itemsPerPage = null,
private readonly ?CountStrategy $count = null,
private readonly ?Pagination $pagination = null,
) {
$this->indexBy = $indexBy ?? Configuration::$defaultIndexBy;
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;
Expand Down Expand Up @@ -105,6 +107,7 @@ final public static function create(
?string $indexBy = null,
int $itemsPerPage = 50,
?CountStrategy $count = null,
?Pagination $pagination = null,
): static {
if (self::$instances === null) {
/** @var \WeakMap<object,array<string,self<array-key,mixed>>> */
Expand All @@ -117,6 +120,7 @@ final public static function create(
$instanceId ?? $criteria,
$indexBy,
$itemsPerPage,
$pagination,
]));

if (isset(self::$instances[$collection][$cacheKey])) {
Expand All @@ -131,6 +135,7 @@ final public static function create(
indexBy: $indexBy,
itemsPerPage: $itemsPerPage,
count: $count,
pagination: $pagination,
);

if (!isset(self::$instances[$collection])) {
Expand Down
5 changes: 5 additions & 0 deletions packages/collections-domain/src/CriteriaRecollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\ReadableRecollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\SafeCollectionTrait;
use Rekalogika\Domain\Collections\Trait\CriteriaReadableTrait;
Expand Down Expand Up @@ -90,6 +91,7 @@ final private function __construct(
private readonly ?int $softLimit = null,
private readonly ?int $hardLimit = null,
private readonly ?KeyTransformer $keyTransformer = null,
private readonly ?Pagination $pagination = null,
) {
$this->indexBy = $indexBy ?? Configuration::$defaultIndexBy;
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;
Expand Down Expand Up @@ -132,6 +134,7 @@ final public static function create(
?int $softLimit = null,
?int $hardLimit = null,
?KeyTransformer $keyTransformer = null,
?Pagination $pagination = null,
): ReadableRecollection {
if (self::$instances === null) {
/** @var \WeakMap<object,array<string,self<array-key,mixed>>> */
Expand All @@ -144,6 +147,7 @@ final public static function create(
$instanceId ?? $criteria,
$indexBy,
$itemsPerPage,
$pagination,
]));

if (isset(self::$instances[$collection][$cacheKey])) {
Expand All @@ -161,6 +165,7 @@ final public static function create(
softLimit: $softLimit,
hardLimit: $hardLimit,
keyTransformer: $keyTransformer,
pagination: $pagination,
);

if (!isset(self::$instances[$collection])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\MinimalReadableRecollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\SafeCollectionTrait;
use Rekalogika\Domain\Collections\Trait\RecollectionPageableTrait;
Expand Down Expand Up @@ -72,6 +73,7 @@ final private function __construct(
?int $itemsPerPage = null,
private readonly ?CountStrategy $count = null,
private readonly ?KeyTransformer $keyTransformer = null,
private readonly ?Pagination $pagination = null,
) {
$this->indexBy = $indexBy ?? Configuration::$defaultIndexBy;
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;
Expand Down Expand Up @@ -128,6 +130,7 @@ final public static function create(
int $itemsPerPage = 50,
?CountStrategy $count = null,
?KeyTransformer $keyTransformer = null,
?Pagination $pagination = null,
): MinimalReadableRecollection {
if (self::$instances === null) {
/** @var \WeakMap<object,array<string,self<array-key,mixed>>> */
Expand All @@ -140,6 +143,7 @@ final public static function create(
$instanceId ?? $criteria,
$indexBy,
$itemsPerPage,
$pagination,
]));

if (isset(self::$instances[$collection][$cacheKey])) {
Expand All @@ -155,6 +159,7 @@ final public static function create(
itemsPerPage: $itemsPerPage,
count: $count,
keyTransformer: $keyTransformer,
pagination: $pagination,
);

if (!isset(self::$instances[$collection])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\MinimalRecollectionTrait;
use Rekalogika\Domain\Collections\Trait\RecollectionPageableTrait;

Expand Down Expand Up @@ -76,6 +77,7 @@ final private function __construct(
?int $itemsPerPage = null,
private readonly ?CountStrategy $count = null,
private readonly ?KeyTransformer $keyTransformer = null,
private readonly ?Pagination $pagination = null,
) {
$this->indexBy = $indexBy ?? Configuration::$defaultIndexBy;
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;
Expand Down Expand Up @@ -113,6 +115,7 @@ final public static function create(
int $itemsPerPage = 50,
?CountStrategy $count = null,
?KeyTransformer $keyTransformer = null,
?Pagination $pagination = null,
): MinimalRecollection {
if (self::$instances === null) {
/** @var \WeakMap<object,array<string,self<array-key,mixed>>> */
Expand All @@ -125,6 +128,7 @@ final public static function create(
$orderBy,
$indexBy,
$itemsPerPage,
$pagination,
]));

if (isset(self::$instances[$collection][$cacheKey])) {
Expand All @@ -140,6 +144,7 @@ final public static function create(
itemsPerPage: $itemsPerPage,
count: $count,
keyTransformer: $keyTransformer,
pagination: $pagination,
);

if (!isset(self::$instances[$collection])) {
Expand Down
5 changes: 5 additions & 0 deletions packages/collections-domain/src/RecollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\RecollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\SafeCollectionTrait;
use Rekalogika\Domain\Collections\Trait\ExtraLazyTrait;
Expand Down Expand Up @@ -100,6 +101,7 @@ final private function __construct(
private readonly ?int $softLimit = null,
private readonly ?int $hardLimit = null,
private readonly ?KeyTransformer $keyTransformer = null,
private readonly ?Pagination $pagination = null,
) {
$this->indexBy = $indexBy ?? Configuration::$defaultIndexBy;
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;
Expand Down Expand Up @@ -141,6 +143,7 @@ final public static function create(
?int $softLimit = null,
?int $hardLimit = null,
?KeyTransformer $keyTransformer = null,
?Pagination $pagination = null,
): Recollection {
if (self::$instances === null) {
/** @var \WeakMap<object,array<string,self<array-key,mixed>>> */
Expand All @@ -153,6 +156,7 @@ final public static function create(
$orderBy,
$indexBy,
$itemsPerPage,
$pagination,
]));

if (isset(self::$instances[$collection][$cacheKey])) {
Expand All @@ -170,6 +174,7 @@ final public static function create(
softLimit: $softLimit,
hardLimit: $hardLimit,
keyTransformer: $keyTransformer,
pagination: $pagination,
);

if (!isset(self::$instances[$collection])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
namespace Rekalogika\Domain\Collections\Trait;

use Rekalogika\Contracts\Rekapager\PageableInterface;
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\Exception\GettingCountUnsupportedException;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Rekapager\Doctrine\Collections\SelectableAdapter;
use Rekalogika\Rekapager\Keyset\KeysetPageable;
use Rekalogika\Rekapager\Offset\OffsetPageable;

/**
* @template TKey of array-key
Expand Down Expand Up @@ -59,11 +62,18 @@ private function getPageable(): PageableInterface
}
};

$this->pageable = new KeysetPageable(
adapter: $adapter,
itemsPerPage: $this->itemsPerPage,
count: $count,
);
$this->pageable = match ($this->pagination ?? Configuration::$defaultPagination) {
Pagination::Keyset => new KeysetPageable(
adapter: $adapter,
itemsPerPage: $this->itemsPerPage,
count: $count,
),
Pagination::Offset => new OffsetPageable(
adapter: $adapter,
itemsPerPage: $this->itemsPerPage,
count: $count,
),
};

return $this->pageable;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/collections-orm/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"rekalogika/collections-common": "^0.10",
"rekalogika/collections-contracts": "^0.10",
"rekalogika/collections-domain": "^0.10",
"rekalogika/rekapager-contracts": "^0.14",
"rekalogika/rekapager-doctrine-orm-adapter": "^0.14",
"rekalogika/rekapager-keyset-pagination": "^0.14"
"rekalogika/rekapager-contracts": "^0.15",
"rekalogika/rekapager-doctrine-orm-adapter": "^0.15",
"rekalogika/rekapager-keyset-pagination": "^0.15"
}
}
2 changes: 2 additions & 0 deletions packages/collections-orm/src/AbstractMinimalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\Pagination;

/**
* @template TKey of array-key
Expand Down Expand Up @@ -76,6 +77,7 @@ public function __construct(
array|string|null $orderBy = null,
?int $itemsPerPage = null,
private readonly ?CountStrategy $count = null,
private readonly ?Pagination $pagination = null,
) {
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;

Expand Down
2 changes: 2 additions & 0 deletions packages/collections-orm/src/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\ParameterUtil;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Pagination;
use Rekalogika\Domain\Collections\Common\Trait\SafeCollectionTrait;

/**
Expand Down Expand Up @@ -88,6 +89,7 @@ public function __construct(
private readonly ?int $softLimit = null,
private readonly ?int $hardLimit = null,
private readonly ?KeyTransformer $keyTransformer = null,
private readonly ?Pagination $pagination = null,
) {
$this->itemsPerPage = $itemsPerPage ?? Configuration::$defaultItemsPerPage;

Expand Down
Loading

0 comments on commit 1668337

Please sign in to comment.