Add more named constructors to Ordering class

This commit is contained in:
Alejandro Celaya 2024-03-20 09:15:45 +01:00
parent b94a22e6a7
commit fbd35b7974
4 changed files with 28 additions and 24 deletions

View file

@ -6,7 +6,9 @@ namespace Shlinkio\Shlink\Core\Model;
final readonly class Ordering 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) private function __construct(public ?string $field, public string $direction)
{ {
@ -23,6 +25,16 @@ final readonly class Ordering
public static function none(): self 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);
} }
} }

View file

@ -24,7 +24,7 @@ final class TagsParams extends AbstractInfinitePaginableListParams
{ {
return new self( return new self(
$query['searchTerm'] ?? null, $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['page']) ? (int) $query['page'] : null,
isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null, isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null,
); );

View file

@ -110,15 +110,13 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering(2, 2, Ordering::none()))); self::assertCount(1, $this->repo->findList(new ShortUrlsListFiltering(2, 2, Ordering::none())));
$result = $this->repo->findList( $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::assertCount(3, $result);
self::assertSame($bar, $result[0]); self::assertSame($bar, $result[0]);
$result = $this->repo->findList( $result = $this->repo->findList(
new ShortUrlsListFiltering(null, null, Ordering::fromTuple( new ShortUrlsListFiltering(null, null, Ordering::fromFieldDesc(OrderableField::NON_BOT_VISITS->value)),
[OrderableField::NON_BOT_VISITS->value, 'DESC'],
)),
); );
self::assertCount(3, $result); self::assertCount(3, $result);
self::assertSame($foo2, $result[0]); self::assertSame($foo2, $result[0]);
@ -155,7 +153,7 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
$result = $this->repo->findList( $result = $this->repo->findList(
new ShortUrlsListFiltering(null, null, Ordering::fromTuple(['longUrl', 'ASC'])), new ShortUrlsListFiltering(null, null, Ordering::fromFieldAsc('longUrl')),
); );
self::assertCount(count($urls), $result); self::assertCount(count($urls), $result);

View file

@ -135,11 +135,11 @@ class TagRepositoryTest extends DatabaseTestCase
['baz', 1, 3, 2], ['baz', 1, 3, 2],
]]; ]];
yield 'ASC ordering' => [ 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, $defaultList,
]; ];
yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromTuple( yield 'DESC ordering' => [new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(
[OrderableField::TAG->value, 'DESC'], OrderableField::TAG->value,
)), [ )), [
['foo', 2, 4, 3], ['foo', 2, 4, 3],
['baz', 1, 3, 2], ['baz', 1, 3, 2],
@ -147,9 +147,7 @@ class TagRepositoryTest extends DatabaseTestCase
['another', 0, 0, 0], ['another', 0, 0, 0],
]]; ]];
yield 'short URLs count ASC ordering' => [ yield 'short URLs count ASC ordering' => [
new TagsListFiltering(null, null, null, Ordering::fromTuple( new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::SHORT_URLS_COUNT->value)),
[OrderableField::SHORT_URLS_COUNT->value, 'ASC'],
)),
[ [
['another', 0, 0, 0], ['another', 0, 0, 0],
['baz', 1, 3, 2], ['baz', 1, 3, 2],
@ -158,9 +156,7 @@ class TagRepositoryTest extends DatabaseTestCase
], ],
]; ];
yield 'short URLs count DESC ordering' => [ yield 'short URLs count DESC ordering' => [
new TagsListFiltering(null, null, null, Ordering::fromTuple( new TagsListFiltering(null, null, null, Ordering::fromFieldDesc(OrderableField::SHORT_URLS_COUNT->value)),
[OrderableField::SHORT_URLS_COUNT->value, 'DESC'],
)),
[ [
['bar', 3, 3, 2], ['bar', 3, 3, 2],
['foo', 2, 4, 3], ['foo', 2, 4, 3],
@ -169,7 +165,7 @@ class TagRepositoryTest extends DatabaseTestCase
], ],
]; ];
yield 'visits count ASC ordering' => [ 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], ['another', 0, 0, 0],
['bar', 3, 3, 2], ['bar', 3, 3, 2],
@ -178,9 +174,7 @@ class TagRepositoryTest extends DatabaseTestCase
], ],
]; ];
yield 'non-bot visits count ASC ordering' => [ yield 'non-bot visits count ASC ordering' => [
new TagsListFiltering(null, null, null, Ordering::fromTuple( new TagsListFiltering(null, null, null, Ordering::fromFieldAsc(OrderableField::NON_BOT_VISITS->value)),
[OrderableField::NON_BOT_VISITS->value, 'ASC'],
)),
[ [
['another', 0, 0, 0], ['another', 0, 0, 0],
['bar', 3, 3, 2], ['bar', 3, 3, 2],
@ -189,7 +183,7 @@ class TagRepositoryTest extends DatabaseTestCase
], ],
]; ];
yield 'visits count DESC ordering' => [ 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], ['foo', 2, 4, 3],
['bar', 3, 3, 2], ['bar', 3, 3, 2],
@ -204,8 +198,8 @@ class TagRepositoryTest extends DatabaseTestCase
['baz', 1, 3, 2], ['baz', 1, 3, 2],
['foo', 1, 3, 2], ['foo', 1, 3, 2],
]]; ]];
yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromTuple( yield 'combined' => [new TagsListFiltering(1, null, null, Ordering::fromFieldDesc(
[OrderableField::SHORT_URLS_COUNT->value, 'DESC'], OrderableField::SHORT_URLS_COUNT->value,
), ApiKey::fromMeta( ), ApiKey::fromMeta(
ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()), ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()),
)), [ )), [