Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consolidate repeated orderBy logic to OrderByUtil #40

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* cleanup: Basic classes do not require soft and hard limits
* cleanup: orderBy should be non-empty-array
* feat: initial version of basic repository
* refactor: consolidate repeated orderBy logic to `OrderByUtil`

## 0.3.0

Expand Down
52 changes: 52 additions & 0 deletions packages/collections-common/src/Internal/OrderByUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Internal;

use Doctrine\Common\Collections\Order;
use Rekalogika\Contracts\Collections\Exception\UnexpectedValueException;
use Rekalogika\Domain\Collections\Common\Configuration;

/**
* @internal
*/
final class OrderByUtil
{
private function __construct()
{
}

/**
* @param null|non-empty-array<string,Order>|string $orderBy
* @param null|non-empty-array<string,Order>|string $defaultOrderBy
* @return non-empty-array<string,Order>
*/
public static function normalizeOrderBy(
array|string|null $orderBy = null,
array|string|null $defaultOrderBy = null,
): array {
if ($orderBy === null) {
$orderBy = $defaultOrderBy ?? Configuration::$defaultOrderBy;
}

if (\is_string($orderBy)) {
$orderBy = [$orderBy => Order::Ascending];
}

if (empty($orderBy)) {
throw new UnexpectedValueException('The order by clause cannot be empty.');
}

return $orderBy;
}
}
20 changes: 6 additions & 14 deletions packages/collections-domain/src/BasicRecollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rekalogika\Contracts\Collections\Exception\UnexpectedValueException;
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\OrderByUtil;
use Rekalogika\Domain\Collections\Common\Trait\BasicReadableCollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\BasicWritableCollectionTrait;
use Rekalogika\Domain\Collections\Common\Trait\CountableTrait;
Expand Down Expand Up @@ -90,25 +91,16 @@ public function __construct(

// handle orderBy

if ($orderBy === null) {
$orderBy = $this->getDefaultOrderBy();
}

if (\is_string($orderBy)) {
$orderBy = [$orderBy => Order::Ascending];
}

if (empty($orderBy)) {
throw new UnexpectedValueException('The order by clause cannot be empty.');
}

$this->orderBy = $orderBy;
$this->orderBy = OrderByUtil::normalizeOrderBy(
orderBy: $orderBy,
defaultOrderBy: $this->getDefaultOrderBy()
);

$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 Down
22 changes: 7 additions & 15 deletions packages/collections-domain/src/RecollectionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rekalogika\Contracts\Collections\Recollection;
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\OrderByUtil;
use Rekalogika\Domain\Collections\Common\Trait\ItemsWithSafeguardTrait;
use Rekalogika\Domain\Collections\Common\Trait\PageableTrait;
use Rekalogika\Domain\Collections\Common\Trait\ReadableRecollectionTrait;
Expand Down Expand Up @@ -66,7 +67,7 @@ class RecollectionDecorator implements Recollection

/**
* @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
Expand All @@ -91,19 +92,10 @@ public function __construct(

// handle orderBy

if ($orderBy === null) {
$orderBy = $this->getDefaultOrderBy();
}

if (\is_string($orderBy)) {
$orderBy = [$orderBy => Order::Ascending];
}

if (empty($orderBy)) {
throw new UnexpectedValueException('The order by clause cannot be empty.');
}

$this->orderBy = $orderBy;
$this->orderBy = OrderByUtil::normalizeOrderBy(
orderBy: $orderBy,
defaultOrderBy: $this->getDefaultOrderBy()
);

$this->criteria = Criteria::create()->orderBy($this->orderBy);
}
Expand All @@ -118,7 +110,7 @@ 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
Expand Down
23 changes: 5 additions & 18 deletions packages/collections-orm/src/AbstractBasicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
use Rekalogika\Collections\ORM\Trait\QueryBuilderTrait;
use Rekalogika\Contracts\Collections\BasicRepository;
use Rekalogika\Contracts\Collections\Exception\NotFoundException;
use Rekalogika\Contracts\Collections\Exception\UnexpectedValueException;
use Rekalogika\Domain\Collections\Common\Configuration;
use Rekalogika\Domain\Collections\Common\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\OrderByUtil;
use Rekalogika\Domain\Collections\Common\Trait\PageableTrait;

/**
Expand Down Expand Up @@ -52,7 +51,7 @@ abstract class AbstractBasicRepository implements BasicRepository
private readonly array $orderBy;

/**
* @param null|array<string,Order>|string $orderBy
* @param null|non-empty-array<string,Order>|string $orderBy
* @param int<1,max> $itemsPerPage
*/
public function __construct(
Expand All @@ -63,26 +62,14 @@ public function __construct(
) {
// handle orderBy

if ($orderBy === null) {
$orderBy = Configuration::$defaultOrderBy;
}

if (\is_string($orderBy)) {
$orderBy = [$orderBy => Order::Ascending];
}

if (empty($orderBy)) {
throw new UnexpectedValueException('The order by clause cannot be empty.');
}

$this->orderBy = $orderBy;
$this->orderBy = OrderByUtil::normalizeOrderBy($orderBy);

$criteria = Criteria::create()->orderBy($orderBy);
$criteria = Criteria::create()->orderBy($this->orderBy);
$this->queryBuilder = $this->createQueryBuilder('e')->addCriteria($criteria);
}

/**
* @param null|array<string,Order>|string $orderBy
* @param null|non-empty-array<string,Order>|string $orderBy
* @param int<1,max> $itemsPerPage
*/
protected function with(
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<file name="packages/collections-domain/src/CriteriaRecollection.php" />
</errorLevel>
</LessSpecificReturnStatement>
<TypeDoesNotContainType>
<errorLevel type="suppress">
<file name="packages/collections-common/src/Internal/OrderByUtil.php" />
</errorLevel>
</TypeDoesNotContainType>
</issueHandlers>

<plugins>
Expand Down