From 98e16d7f3c12b25a675618d2d7e2eafc65e00c9c Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:45:38 +0700 Subject: [PATCH] refactor: change `KeyTransformer` to instance object, not static class (#118) --- CHANGELOG.md | 1 + .../collections-common/src/Configuration.php | 31 +++++++++++++++++-- .../src/Internal/ParameterUtil.php | 13 ++++++-- .../KeyTransformer/DefaultKeyTransformer.php | 13 +++++++- .../src/KeyTransformer/KeyTransformer.php | 2 +- .../src/KeyTransformer/UuidKeyTransformer.php | 13 +++++++- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cc5429..a051eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/collections-common/src/Configuration.php b/packages/collections-common/src/Configuration.php index 52f847f..f8129f4 100644 --- a/packages/collections-common/src/Configuration.php +++ b/packages/collections-common/src/Configuration.php @@ -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 @@ -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; /** @@ -60,23 +74,34 @@ private function __construct() public static array $defaultOrderBy = ['id' => Order::Descending]; /** - * @var class-string + * 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; diff --git a/packages/collections-common/src/Internal/ParameterUtil.php b/packages/collections-common/src/Internal/ParameterUtil.php index 43c5caf..fca60c5 100644 --- a/packages/collections-common/src/Internal/ParameterUtil.php +++ b/packages/collections-common/src/Internal/ParameterUtil.php @@ -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; /** @@ -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); } /** diff --git a/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php b/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php index f05a16a..20d121c 100644 --- a/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php +++ b/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php @@ -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; diff --git a/packages/collections-common/src/KeyTransformer/KeyTransformer.php b/packages/collections-common/src/KeyTransformer/KeyTransformer.php index 4c4b981..0c79cc2 100644 --- a/packages/collections-common/src/KeyTransformer/KeyTransformer.php +++ b/packages/collections-common/src/KeyTransformer/KeyTransformer.php @@ -15,5 +15,5 @@ interface KeyTransformer { - public static function transformToKey(mixed $key): int|string; + public function transformToKey(mixed $key): int|string; } diff --git a/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php b/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php index a8fa69b..04da420 100644 --- a/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php +++ b/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php @@ -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();