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
{
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);
}
}

View file

@ -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,
);

View file

@ -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);

View file

@ -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()),
)), [