diff --git a/CHANGELOG.md b/CHANGELOG.md index b99aeec..bbf96c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * refactor: rename the term 'safe' to 'large' * refactor(`RecollectionDecorator`): rename `withCriteria` to `applyCriteria` * refactor: rename the term 'large' to 'basic' +* cleanup: Basic classes do not require soft and hard limits +* cleanup: orderBy should be non-empty-array ## 0.3.0 diff --git a/packages/collections-common/src/Configuration.php b/packages/collections-common/src/Configuration.php index a12fe70..7b479d8 100644 --- a/packages/collections-common/src/Configuration.php +++ b/packages/collections-common/src/Configuration.php @@ -36,7 +36,7 @@ final class Configuration /** * The default order by clause for the collection. * - * @var array + * @var non-empty-array */ public static array $defaultOrderBy = ['id' => Order::Descending]; } diff --git a/packages/collections-domain/src/BasicCriteriaRecollection.php b/packages/collections-domain/src/BasicCriteriaRecollection.php index 18c5ca1..fab644f 100644 --- a/packages/collections-domain/src/BasicCriteriaRecollection.php +++ b/packages/collections-domain/src/BasicCriteriaRecollection.php @@ -23,7 +23,6 @@ use Rekalogika\Domain\Collections\Common\CountStrategy; use Rekalogika\Domain\Collections\Common\Trait\BasicReadableCollectionTrait; use Rekalogika\Domain\Collections\Common\Trait\CountableTrait; -use Rekalogika\Domain\Collections\Common\Trait\ItemsWithSafeguardTrait; use Rekalogika\Domain\Collections\Common\Trait\PageableTrait; use Rekalogika\Domain\Collections\Common\Trait\ReadableRecollectionTrait; use Rekalogika\Domain\Collections\Trait\ExtraLazyDetectorTrait; @@ -42,9 +41,6 @@ class BasicCriteriaRecollection implements BasicReadableRecollection, \Countable /** @use PageableTrait */ use PageableTrait; - /** @use ItemsWithSafeguardTrait */ - use ItemsWithSafeguardTrait; - /** @use BasicReadableCollectionTrait */ use BasicReadableCollectionTrait; @@ -66,8 +62,6 @@ class BasicCriteriaRecollection implements BasicReadableRecollection, \Countable * @param ReadableCollection $collection * @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( ReadableCollection $collection, @@ -75,8 +69,6 @@ public function __construct( 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, ) { // save collection @@ -101,17 +93,13 @@ public function __construct( * @param null|Collection $collection * @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 with( ?ReadableCollection $collection = null, ?Criteria $criteria = null, ?int $itemsPerPage = 50, - ?CountStrategy $countStrategy = CountStrategy::Restrict, + ?CountStrategy $countStrategy = null, ?int &$count = null, - ?int $softLimit = null, - ?int $hardLimit = null, ): static { $count = $count ?? $this->count; @@ -122,8 +110,6 @@ protected function with( itemsPerPage: $itemsPerPage ?? $this->itemsPerPage, countStrategy: $countStrategy ?? $this->countStrategy, count: $count, - softLimit: $softLimit ?? $this->softLimit, - hardLimit: $hardLimit ?? $this->hardLimit, ); } diff --git a/packages/collections-domain/src/BasicRecollectionDecorator.php b/packages/collections-domain/src/BasicRecollectionDecorator.php index d2ecd02..b3aac80 100644 --- a/packages/collections-domain/src/BasicRecollectionDecorator.php +++ b/packages/collections-domain/src/BasicRecollectionDecorator.php @@ -24,7 +24,6 @@ use Rekalogika\Domain\Collections\Common\Trait\BasicReadableCollectionTrait; use Rekalogika\Domain\Collections\Common\Trait\BasicWritableCollectionTrait; use Rekalogika\Domain\Collections\Common\Trait\CountableTrait; -use Rekalogika\Domain\Collections\Common\Trait\ItemsWithSafeguardTrait; use Rekalogika\Domain\Collections\Common\Trait\PageableTrait; use Rekalogika\Domain\Collections\Common\Trait\ReadableRecollectionTrait; use Rekalogika\Domain\Collections\Trait\ExtraLazyDetectorTrait; @@ -43,9 +42,6 @@ class BasicRecollectionDecorator implements BasicRecollection, \Countable /** @use PageableTrait */ use PageableTrait; - /** @use ItemsWithSafeguardTrait */ - use ItemsWithSafeguardTrait; - /** @use BasicWritableCollectionTrait */ use BasicWritableCollectionTrait; @@ -65,7 +61,7 @@ class BasicRecollectionDecorator implements BasicRecollection, \Countable private readonly Collection&Selectable $collection; /** - * @var array + * @var non-empty-array */ private readonly array $orderBy; @@ -73,11 +69,9 @@ class BasicRecollectionDecorator implements BasicRecollection, \Countable /** * @param Collection $collection - * @param null|array|string $orderBy + * @param null|non-empty-array|string $orderBy * @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( Collection $collection, @@ -85,8 +79,6 @@ public function __construct( 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, ) { // handle collection @@ -106,6 +98,10 @@ public function __construct( $orderBy = [$orderBy => Order::Ascending]; } + if (empty($orderBy)) { + throw new UnexpectedValueException('The order by clause cannot be empty.'); + } + $this->orderBy = $orderBy; $this->criteria = Criteria::create()->orderBy($this->orderBy); @@ -121,11 +117,9 @@ protected function getDefaultOrderBy(): array|string /** * @param null|Collection $collection - * @param null|array|string $orderBy + * @param null|non-empty-array|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 with( ?Collection $collection = null, @@ -133,8 +127,6 @@ protected function with( ?int $itemsPerPage = 50, ?CountStrategy $countStrategy = CountStrategy::Restrict, ?int &$count = null, - ?int $softLimit = null, - ?int $hardLimit = null, ): static { $count = $count ?? $this->count; @@ -145,8 +137,6 @@ protected function with( itemsPerPage: $itemsPerPage ?? $this->itemsPerPage, countStrategy: $countStrategy ?? $this->countStrategy, count: $count, - softLimit: $softLimit ?? $this->softLimit, - hardLimit: $hardLimit ?? $this->hardLimit, ); } @@ -170,8 +160,6 @@ protected function applyCriteria( itemsPerPage: $this->itemsPerPage, countStrategy: $countStrategy, count: $count, - softLimit: $this->softLimit, - hardLimit: $this->hardLimit, ); } diff --git a/packages/collections-domain/src/RecollectionDecorator.php b/packages/collections-domain/src/RecollectionDecorator.php index 312a2a8..b638ca7 100644 --- a/packages/collections-domain/src/RecollectionDecorator.php +++ b/packages/collections-domain/src/RecollectionDecorator.php @@ -58,7 +58,7 @@ class RecollectionDecorator implements Recollection private readonly Collection&Selectable $collection; /** - * @var array + * @var non-empty-array */ private readonly array $orderBy; @@ -99,13 +99,17 @@ public function __construct( $orderBy = [$orderBy => Order::Ascending]; } + if (empty($orderBy)) { + throw new UnexpectedValueException('The order by clause cannot be empty.'); + } + $this->orderBy = $orderBy; $this->criteria = Criteria::create()->orderBy($this->orderBy); } /** - * @return array|string + * @return non-empty-array|string */ protected function getDefaultOrderBy(): array|string { @@ -123,8 +127,8 @@ protected function getDefaultOrderBy(): array|string protected function with( ?Collection $collection = null, array|string|null $orderBy = null, - ?int $itemsPerPage = 50, - ?CountStrategy $countStrategy = CountStrategy::Restrict, + ?int $itemsPerPage = null, + ?CountStrategy $countStrategy = null, ?int &$count = null, ?int $softLimit = null, ?int $hardLimit = null,