Skip to content

Commit

Permalink
refactor: change KeyTransformer to instance object, not static class (
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jul 9, 2024
1 parent c7b450f commit 98e16d7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* feat: default items per page
* feat: different default count strategies for full & minimal classes
* chore: cleanup typehints
* refactor: change `KeyTransformer` to instance object, not static class

## 0.8.0

Expand Down
31 changes: 28 additions & 3 deletions packages/collections-common/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Doctrine\Common\Collections\Order;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\KeyTransformer\DefaultKeyTransformer;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;

final class Configuration
Expand All @@ -41,15 +40,30 @@ private function __construct()
public static int $defaultHardLimit = 2000;

/**
* If the result of a count exceeds this number, a deprecation notice will
* be emitted. Used by `SafeDelegatedCountStrategy`. Not applicable with
* other count strategies.
*
* @var int<1,max>
*/
public static int $defaultCountSoftLimit = 5000;

/**
* If the result of a count exceeds this number, an exception will be
* thrown. Used by `SafeDelegatedCountStrategy`. Not applicable with other
* count strategies.
*
* @var int<1,max>
*/
public static int $defaultCountHardLimit = 50000;

/**
* If the duration of a count operation exceeds this number of seconds, a
* deprecation notice will be emitted. Used by `SafeDelegatedCountStrategy`,
* Not applicable with other count strategies.
*
* @var float
*/
public static float $defaultCountDurationThreshold = 2;

/**
Expand All @@ -60,23 +74,34 @@ private function __construct()
public static array $defaultOrderBy = ['id' => Order::Descending];

/**
* @var class-string<KeyTransformer>
* The default key transformer for the collection.
*
* @var null|\Closure(): KeyTransformer
*/
public static string $defaultKeyTransformer = DefaultKeyTransformer::class;
public static ?\Closure $defaultKeyTransformer = null;

/**
* Default count strategy for full, non-minimal, classes.
*
* @var null|\Closure(): CountStrategy
*/
public static ?\Closure $defaultCountStrategyForFullClasses = null;

/**
* Default count strategy for minimal classes.
*
* @var null|\Closure(): CountStrategy
*/
public static ?\Closure $defaultCountStrategyForMinimalClasses = null;

/**
* Default columns used for indexing.
*/
public static ?string $defaultIndexBy = null;

/**
* Default items per page for pagination.
*
* @var int<1,max>
*/
public static int $defaultItemsPerPage = 50;
Expand Down
13 changes: 11 additions & 2 deletions packages/collections-common/src/Internal/ParameterUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\Count\DisabledCountStrategy;
use Rekalogika\Domain\Collections\Common\Count\SafeDelegatedCountStrategy;
use Rekalogika\Domain\Collections\Common\KeyTransformer\DefaultKeyTransformer;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;

/**
Expand Down Expand Up @@ -46,13 +47,21 @@ public static function getDefaultCountStrategyForMinimalClasses(): CountStrategy
return $closure();
}

public static function getDefaultKeyTransformer(): KeyTransformer
{
$closure = Configuration::$defaultKeyTransformer
?? fn (): KeyTransformer => DefaultKeyTransformer::create();

return $closure();
}

public static function transformInputToKey(
?KeyTransformer $keyTransformer,
mixed $input
): int|string {
$keyTransformer ??= Configuration::$defaultKeyTransformer;
$keyTransformer ??= self::getDefaultKeyTransformer();

return $keyTransformer::transformToKey($input);
return $keyTransformer->transformToKey($input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@

class DefaultKeyTransformer implements KeyTransformer
{
public static function transformToKey(mixed $key): int|string
private static ?self $instance = null;

public static function create(): self
{
return self::$instance ??= new self();
}

private function __construct()
{
}

public function transformToKey(mixed $key): int|string
{
if ($key instanceof \Stringable) {
return (string) $key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

interface KeyTransformer
{
public static function transformToKey(mixed $key): int|string;
public function transformToKey(mixed $key): int|string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@

class UuidKeyTransformer implements KeyTransformer
{
public static function transformToKey(mixed $key): int|string
private static ?self $instance = null;

public static function create(): self
{
return self::$instance ??= new self();
}

private function __construct()
{
}

public function transformToKey(mixed $key): int|string
{
if ($key instanceof AbstractUid) {
return $key->toRfc4122();
Expand Down

0 comments on commit 98e16d7

Please sign in to comment.