Merge pull request #840 from acelaya-forks/feature/extended-ordering-support

Feature/extended ordering support
This commit is contained in:
Alejandro Celaya 2020-09-21 22:19:55 +02:00 committed by GitHub
commit 319b790628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 9 deletions

View File

@ -4,6 +4,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased]
#### Added
* *Nothing*
#### Changed
* [#836](https://github.com/shlinkio/shlink/issues/836) Added support for the `<field>-<dir>` notation while determining how to order the short URLs list, as in `?orderBy=shortCode-DESC`. This effectively deprecates the array notation (`?orderBy[shortCode]=DESC`), that will be removed in Shlink 3.0.0
#### Deprecated
* *Nothing*
#### Removed
* *Nothing*
#### Fixed
* *Nothing*
## 2.3.0 - 2020-08-09
#### Added

View File

@ -31,7 +31,7 @@
{
"name": "tags[]",
"in": "query",
"description": "A list of tags used to filter the resultset. Only short URLs tagged with at least one of the provided tags will be returned. (Since v1.3.0)",
"description": "A list of tags used to filter the result set. Only short URLs tagged with at least one of the provided tags will be returned. (Since v1.3.0)",
"required": false,
"schema": {
"type": "array",
@ -48,10 +48,14 @@
"schema": {
"type": "string",
"enum": [
"longUrl",
"shortCode",
"dateCreated",
"visits"
"longUrl-ASC",
"longUrl-DESC",
"shortCode-ASC",
"shortCode-DESC",
"dateCreated-ASC",
"dateCreated-DESC",
"visits-ASC",
"visits-DESC"
]
}
},

View File

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

View File

@ -137,7 +137,15 @@ class ListShortUrlsTest extends ApiTestCase
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_DOMAIN,
]];
yield [['orderBy' => ['shortCode' => 'DESC']], [
yield [['orderBy' => ['shortCode' => 'DESC']], [ // Deprecated
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 [['orderBy' => 'shortCode-DESC'], [
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_DOMAIN,
self::SHORT_URL_META,