From 964e9ae2c62dca8c362cefa9ba1169b6056fa6be Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:24:22 +0700 Subject: [PATCH] feat: add `QueryPageable` (#30) --- CHANGELOG.md | 1 + .../collections-orm/src/QueryCollection.php | 7 +- .../collections-orm/src/QueryPageable.php | 82 +++++++++++++++++++ psalm.xml | 9 +- 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 packages/collections-orm/src/QueryPageable.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c2f6757..097dbd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * refactor: reorganize exceptions * feat: add `getOrFail()` +* feat: add `QueryPageable` ## 0.3.0 diff --git a/packages/collections-orm/src/QueryCollection.php b/packages/collections-orm/src/QueryCollection.php index 7f9e3ec..43bea12 100644 --- a/packages/collections-orm/src/QueryCollection.php +++ b/packages/collections-orm/src/QueryCollection.php @@ -69,7 +69,6 @@ public function __construct( } /** - * @param null|array|string $orderBy * @param null|int<1,max> $itemsPerPage * @param null|int<0,max> $count * @param null|int<1,max> $softLimit @@ -77,7 +76,6 @@ public function __construct( */ protected function createFrom( ?QueryBuilder $queryBuilder = null, - array|string|null $orderBy = null, ?int $itemsPerPage = 50, ?CountStrategy $countStrategy = CountStrategy::Restrict, ?int &$count = null, @@ -96,4 +94,9 @@ protected function createFrom( hardLimit: $hardLimit ?? $this->hardLimit, ); } + + final protected function getQueryBuilder(): QueryBuilder + { + return $this->queryBuilder; + } } diff --git a/packages/collections-orm/src/QueryPageable.php b/packages/collections-orm/src/QueryPageable.php new file mode 100644 index 0000000..c92db5c --- /dev/null +++ b/packages/collections-orm/src/QueryPageable.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Collections\ORM; + +use Doctrine\ORM\QueryBuilder; +use Rekalogika\Collections\ORM\Trait\QueryBuilderTrait; +use Rekalogika\Contracts\Rekapager\PageableInterface; +use Rekalogika\Domain\Collections\Common\CountStrategy; +use Rekalogika\Domain\Collections\Common\Trait\PageableTrait; + +/** + * @template TKey of array-key + * @template T + * @implements PageableInterface + */ +class QueryPageable implements PageableInterface +{ + /** @use QueryBuilderTrait */ + use QueryBuilderTrait; + + /** @use PageableTrait */ + use PageableTrait; + + /** + * @param int<1,max> $itemsPerPage + * @param null|int<0,max> $count + * @param null|int<1,max> $softLimit + * @param null|int<1,max> $hardLimit + */ + public function __construct( + private QueryBuilder $queryBuilder, + private readonly int $itemsPerPage = 50, + private readonly CountStrategy $countStrategy = CountStrategy::Restrict, + private ?int &$count = null, + private readonly ?int $softLimit = null, + private readonly ?int $hardLimit = null, + ) { + } + + /** + * @param null|int<1,max> $itemsPerPage + * @param null|int<0,max> $count + * @param null|int<1,max> $softLimit + * @param null|int<1,max> $hardLimit + */ + protected function createFrom( + ?QueryBuilder $queryBuilder = null, + ?int $itemsPerPage = 50, + ?CountStrategy $countStrategy = CountStrategy::Restrict, + ?int &$count = null, + ?int $softLimit = null, + ?int $hardLimit = null, + ): static { + $count = $count ?? $this->count; + + // @phpstan-ignore-next-line + return new static( + queryBuilder: $queryBuilder ?? $this->queryBuilder, + itemsPerPage: $itemsPerPage ?? $this->itemsPerPage, + countStrategy: $countStrategy ?? $this->countStrategy, + count: $count, + softLimit: $softLimit ?? $this->softLimit, + hardLimit: $hardLimit ?? $this->hardLimit, + ); + } + + final protected function getQueryBuilder(): QueryBuilder + { + return $this->queryBuilder; + } +} diff --git a/psalm.xml b/psalm.xml index 32ba794..fa31993 100644 --- a/psalm.xml +++ b/psalm.xml @@ -37,18 +37,11 @@ - - - - - - - - +