Skip to content

Commit

Permalink
cleanup: Basic classes do not require soft and hard limits, orderBy s…
Browse files Browse the repository at this point in the history
…hould be non-empty-array (#38)

* cleanup: Basic classes do not require soft and hard limits, orderBy should be non-empty-array

* fix configuration
  • Loading branch information
priyadi authored Jun 20, 2024
1 parent a8db615 commit facd1d6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/collections-common/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class Configuration
/**
* The default order by clause for the collection.
*
* @var array<string,Order>
* @var non-empty-array<string,Order>
*/
public static array $defaultOrderBy = ['id' => Order::Descending];
}
16 changes: 1 addition & 15 deletions packages/collections-domain/src/BasicCriteriaRecollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,9 +41,6 @@ class BasicCriteriaRecollection implements BasicReadableRecollection, \Countable
/** @use PageableTrait<TKey,T> */
use PageableTrait;

/** @use ItemsWithSafeguardTrait<TKey,T> */
use ItemsWithSafeguardTrait;

/** @use BasicReadableCollectionTrait<TKey,T> */
use BasicReadableCollectionTrait;

Expand All @@ -66,17 +62,13 @@ class BasicCriteriaRecollection implements BasicReadableRecollection, \Countable
* @param ReadableCollection<TKey,T> $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,
?Criteria $criteria = null,
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

Expand All @@ -101,17 +93,13 @@ public function __construct(
* @param null|Collection<TKey,T> $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;

Expand All @@ -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,
);
}

Expand Down
26 changes: 7 additions & 19 deletions packages/collections-domain/src/BasicRecollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,9 +42,6 @@ class BasicRecollectionDecorator implements BasicRecollection, \Countable
/** @use PageableTrait<TKey,T> */
use PageableTrait;

/** @use ItemsWithSafeguardTrait<TKey,T> */
use ItemsWithSafeguardTrait;

/** @use BasicWritableCollectionTrait<TKey,T> */
use BasicWritableCollectionTrait;

Expand All @@ -65,28 +61,24 @@ class BasicRecollectionDecorator implements BasicRecollection, \Countable
private readonly Collection&Selectable $collection;

/**
* @var array<string,Order>
* @var non-empty-array<string,Order>
*/
private readonly array $orderBy;

private readonly Criteria $criteria;

/**
* @param Collection<TKey,T> $collection
* @param null|array<string,Order>|string $orderBy
* @param null|non-empty-array<string,Order>|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,
array|string|null $orderBy = null,
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

Expand All @@ -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);
Expand All @@ -121,20 +117,16 @@ protected function getDefaultOrderBy(): array|string

/**
* @param null|Collection<TKey,T> $collection
* @param null|array<string,Order>|string $orderBy
* @param null|non-empty-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 with(
?Collection $collection = null,
array|string|null $orderBy = null,
?int $itemsPerPage = 50,
?CountStrategy $countStrategy = CountStrategy::Restrict,
?int &$count = null,
?int $softLimit = null,
?int $hardLimit = null,
): static {
$count = $count ?? $this->count;

Expand All @@ -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,
);
}

Expand All @@ -170,8 +160,6 @@ protected function applyCriteria(
itemsPerPage: $this->itemsPerPage,
countStrategy: $countStrategy,
count: $count,
softLimit: $this->softLimit,
hardLimit: $this->hardLimit,
);
}

Expand Down
12 changes: 8 additions & 4 deletions packages/collections-domain/src/RecollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RecollectionDecorator implements Recollection
private readonly Collection&Selectable $collection;

/**
* @var array<string,Order>
* @var non-empty-array<string,Order>
*/
private readonly array $orderBy;

Expand Down Expand Up @@ -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,Order>|string
* @return non-empty-array<string,Order>|string
*/
protected function getDefaultOrderBy(): array|string
{
Expand All @@ -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,
Expand Down

0 comments on commit facd1d6

Please sign in to comment.