Added support to order short urls liusts using the <field>:<dir> notaiton as string

This commit is contained in:
Alejandro Celaya 2020-09-20 13:21:21 +02:00
parent cad8c7ed48
commit 25fbbee883
2 changed files with 19 additions and 3 deletions

View file

@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Model;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
use function explode;
use function is_array; use function is_array;
use function is_string; use function is_string;
use function key; use function key;
@ -40,15 +41,22 @@ final class ShortUrlsOrdering
return; return;
} }
// FIXME Providing the ordering as array is considered deprecated. To be removed in v3.0.0
$isArray = is_array($orderBy); $isArray = is_array($orderBy);
if (! $isArray && $orderBy !== null && ! is_string($orderBy)) { if (! $isArray && ! is_string($orderBy)) {
throw ValidationException::fromArray([ throw ValidationException::fromArray([
'orderBy' => '"Order by" must be an array, string or null', 'orderBy' => '"Order by" must be an array, string or null',
]); ]);
} }
$this->orderField = $isArray ? key($orderBy) : $orderBy; if (! $isArray) {
$this->orderDirection = $isArray ? $orderBy[$this->orderField] : self::DEFAULT_ORDER_DIRECTION; $parts = explode(':', $orderBy);
$this->orderField = $parts[0];
$this->orderDirection = $parts[1] ?? self::DEFAULT_ORDER_DIRECTION;
} else {
$this->orderField = key($orderBy);
$this->orderDirection = $orderBy[$this->orderField];
}
} }
public function orderField(): ?string public function orderField(): ?string

View file

@ -145,6 +145,14 @@ class ListShortUrlsTest extends ApiTestCase
self::SHORT_URL_CUSTOM_SLUG, self::SHORT_URL_CUSTOM_SLUG,
self::SHORT_URL_SHLINK, self::SHORT_URL_SHLINK,
]]; ]];
yield [['orderBy' => 'shortCode:DESC'], [
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_DOMAIN,
self::SHORT_URL_META,
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
self::SHORT_URL_CUSTOM_SLUG,
self::SHORT_URL_SHLINK,
]];
yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [ yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [
self::SHORT_URL_META, self::SHORT_URL_META,
self::SHORT_URL_CUSTOM_SLUG, self::SHORT_URL_CUSTOM_SLUG,