diff --git a/module/Core/src/Model/Ordering.php b/module/Core/src/Model/Ordering.php index 12cf7dd5..69a1429a 100644 --- a/module/Core/src/Model/Ordering.php +++ b/module/Core/src/Model/Ordering.php @@ -6,7 +6,9 @@ namespace Shlinkio\Shlink\Core\Model; final readonly class Ordering { - private const DEFAULT_DIR = 'ASC'; + private const DESC_DIR = 'DESC'; + private const ASC_DIR = 'ASC'; + private const DEFAULT_DIR = self::ASC_DIR; private function __construct(public ?string $field, public string $direction) { @@ -23,6 +25,16 @@ final readonly class Ordering public static function none(): self { - return self::fromTuple([null, null]); + return new self(null, self::DEFAULT_DIR); + } + + public static function fromFieldAsc(string $field): self + { + return new self($field, self::ASC_DIR); + } + + public static function fromFieldDesc(string $field): self + { + return new self($field, self::DESC_DIR); } } diff --git a/module/Core/src/Tag/Model/TagsParams.php b/module/Core/src/Tag/Model/TagsParams.php index 422f9da1..d094bcc0 100644 --- a/module/Core/src/Tag/Model/TagsParams.php +++ b/module/Core/src/Tag/Model/TagsParams.php @@ -24,7 +24,7 @@ final class TagsParams extends AbstractInfinitePaginableListParams { return new self( $query['searchTerm'] ?? null, - Ordering::fromTuple(isset($query['orderBy']) ? parseOrderBy($query['orderBy']) : [null, null]), + isset($query['orderBy']) ? Ordering::fromTuple(parseOrderBy($query['orderBy'])) : Ordering::none(), isset($query['page']) ? (int) $query['page'] : null, isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null, ); diff --git a/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php b/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php index 6508b0e2..315491d8 100644 --- a/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php +++ b/module/Core/test-db/ShortUrl/Repository/ShortUrlListRepositoryTest.php @@ -110,15 +110,13 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering(2, 2, Ordering::none()))); $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'DESC'])), + new ShortUrlsListFiltering(null, null, Ordering::fromFieldDesc(OrderableField::VISITS->value)), ); self::assertCount(3, $result); self::assertSame($bar, $result[0]); $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple( - [OrderableField::NON_BOT_VISITS->value, 'DESC'], - )), + new ShortUrlsListFiltering(null, null, Ordering::fromFieldDesc(OrderableField::NON_BOT_VISITS->value)), ); self::assertCount(3, $result); self::assertSame($foo2, $result[0]); @@ -155,7 +153,7 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase $this->getEntityManager()->flush(); $result = $this->repo->findList( - new ShortUrlsListFiltering(null, null, Ordering::fromTuple(['longUrl', 'ASC'])), + new ShortUrlsListFiltering(null, null, Ordering::fromFieldAsc('longUrl')), ); self::assertCount(count($urls), $result); diff --git a/module/Core/test-db/Tag/Repository/TagRepositoryTest.php b/module/Core/test-db/Tag/Repository/TagRepositoryTest.php index 77077142..77f6aa6a 100644 --- a/module/Core/test-db/Tag/Repository/TagRepositoryTest.php +++ b/module/Core/test-db/Tag/Repository/TagRepositoryTest.php @@ -135,11 +135,11 @@ class TagRepositoryTest extends DatabaseTestCase ['baz', 1, 3, 2], ]]; yield 'ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::TAG->value, 'ASC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::TAG->value)), $defaultList, ]; - yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::TAG->value, 'DESC'], + yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromFieldDesc( + OrderableField::TAG->value, )), [ ['foo', 2, 4, 3], ['baz', 1, 3, 2], @@ -147,9 +147,7 @@ class TagRepositoryTest extends DatabaseTestCase ['another', 0, 0, 0], ]]; yield 'short URLs count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'ASC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::SHORT_URLS_COUNT->value)), [ ['another', 0, 0, 0], ['baz', 1, 3, 2], @@ -158,9 +156,7 @@ class TagRepositoryTest extends DatabaseTestCase ], ]; yield 'short URLs count DESC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'DESC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(OrderableField::SHORT_URLS_COUNT->value)), [ ['bar', 3, 3, 2], ['foo', 2, 4, 3], @@ -169,7 +165,7 @@ class TagRepositoryTest extends DatabaseTestCase ], ]; yield 'visits count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'ASC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::VISITS->value)), [ ['another', 0, 0, 0], ['bar', 3, 3, 2], @@ -178,9 +174,7 @@ class TagRepositoryTest extends DatabaseTestCase ], ]; yield 'non-bot visits count ASC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple( - [OrderableField::NON_BOT_VISITS->value, 'ASC'], - )), + new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::NON_BOT_VISITS->value)), [ ['another', 0, 0, 0], ['bar', 3, 3, 2], @@ -189,7 +183,7 @@ class TagRepositoryTest extends DatabaseTestCase ], ]; yield 'visits count DESC ordering' => [ - new TagsListFiltering(null, null, null, Ordering::fromTuple([OrderableField::VISITS->value, 'DESC'])), + new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(OrderableField::VISITS->value)), [ ['foo', 2, 4, 3], ['bar', 3, 3, 2], @@ -204,8 +198,8 @@ class TagRepositoryTest extends DatabaseTestCase ['baz', 1, 3, 2], ['foo', 1, 3, 2], ]]; - yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromTuple( - [OrderableField::SHORT_URLS_COUNT->value, 'DESC'], + yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromFieldDesc( + OrderableField::SHORT_URLS_COUNT->value, ), ApiKey::fromMeta( ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()), )), [