Skip to content

Commit

Permalink
feat: add QueryPageable (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jun 20, 2024
1 parent 5fe9211 commit 964e9ae
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* refactor: reorganize exceptions
* feat: add `getOrFail()`
* feat: add `QueryPageable`

## 0.3.0

Expand Down
7 changes: 5 additions & 2 deletions packages/collections-orm/src/QueryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ public function __construct(
}

/**
* @param null|array<string,Order>|string $orderBy
* @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,
array|string|null $orderBy = null,
?int $itemsPerPage = 50,
?CountStrategy $countStrategy = CountStrategy::Restrict,
?int &$count = null,
Expand All @@ -96,4 +94,9 @@ protected function createFrom(
hardLimit: $hardLimit ?? $this->hardLimit,
);
}

final protected function getQueryBuilder(): QueryBuilder
{
return $this->queryBuilder;
}
}
82 changes: 82 additions & 0 deletions packages/collections-orm/src/QueryPageable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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\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<TKey,T>
*/
class QueryPageable implements PageableInterface
{
/** @use QueryBuilderTrait<TKey,T> */
use QueryBuilderTrait;

/** @use PageableTrait<TKey,T> */
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;
}
}
9 changes: 1 addition & 8 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,11 @@
<UnsafeInstantiation>
<errorLevel type="suppress">
<file name="packages/collections-domain/src/RecollectionDecorator.php" />
</errorLevel>
<errorLevel type="suppress">
<file name="packages/collections-domain/src/CriteriaRecollection.php" />
</errorLevel>
<errorLevel type="suppress">
<file name="packages/collections-domain/src/SafeRecollectionDecorator.php" />
</errorLevel>
<errorLevel type="suppress">
<file name="packages/collections-domain/src/SafeCriteriaRecollection.php" />
</errorLevel>
<errorLevel type="suppress">
<file name="packages/collections-orm/src/QueryCollection.php" />
<file name="packages/collections-orm/src/QueryPageable.php" />
</errorLevel>
</UnsafeInstantiation>
<MoreSpecificReturnType>
Expand Down

0 comments on commit 964e9ae

Please sign in to comment.