Switch to using Order enum for sorting.
This commit is contained in:
parent
e376d4014c
commit
eed6811b38
|
@ -21,7 +21,7 @@ use App\Media\MimeType;
|
|||
use App\Paginator;
|
||||
use App\Utilities\Strings;
|
||||
use App\Utilities\Types;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\Common\Collections\Order;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use League\Flysystem\StorageAttributes;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
@ -429,7 +429,7 @@ final class ListAction implements SingleActionInterface
|
|||
PropertyAccessorInterface $propertyAccessor,
|
||||
?string $searchPhrase = null,
|
||||
?string $sort = null,
|
||||
string $sortOrder = Criteria::ASC
|
||||
Order $sortOrder = Order::Ascending
|
||||
): int {
|
||||
if ('special:duplicates' === $searchPhrase) {
|
||||
return $a->media->id <=> $b->media->id;
|
||||
|
@ -443,7 +443,7 @@ final class ListAction implements SingleActionInterface
|
|||
if (!$sort) {
|
||||
$aVal = $a->path;
|
||||
$bVal = $b->path;
|
||||
return (Criteria::ASC === $sortOrder) ? $aVal <=> $bVal : $bVal <=> $aVal;
|
||||
return (Order::Ascending === $sortOrder) ? $aVal <=> $bVal : $bVal <=> $aVal;
|
||||
}
|
||||
|
||||
return self::sortByDotNotation($a, $b, $propertyAccessor, $sort, $sortOrder);
|
||||
|
|
|
@ -16,6 +16,7 @@ use App\Http\ServerRequest;
|
|||
use App\OpenApi;
|
||||
use App\Paginator;
|
||||
use App\Service\Flow\UploadedFile;
|
||||
use Doctrine\Common\Collections\Order;
|
||||
use InvalidArgumentException;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
@ -237,7 +238,7 @@ final class PodcastEpisodesController extends AbstractApiCrudController
|
|||
'is_explicit' => 'is_explicit',
|
||||
],
|
||||
'id',
|
||||
'DESC'
|
||||
Order::Descending
|
||||
);
|
||||
|
||||
$paginator = Paginator::fromArray($episodes, $request);
|
||||
|
|
|
@ -14,6 +14,7 @@ use App\Http\Response;
|
|||
use App\Http\ServerRequest;
|
||||
use App\Paginator;
|
||||
use App\Utilities\Types;
|
||||
use Doctrine\Common\Collections\Order;
|
||||
use Doctrine\ORM\AbstractQuery;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
|
@ -58,7 +59,7 @@ final class RequestsController
|
|||
'genre' => 'sm.genre',
|
||||
],
|
||||
'sr.timestamp',
|
||||
'DESC'
|
||||
Order::Descending
|
||||
);
|
||||
|
||||
$qb = $this->searchQueryBuilder(
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace App\Controller\Api\Traits;
|
|||
|
||||
use App\Http\ServerRequest;
|
||||
use App\Utilities\Types;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\Common\Collections\Order;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
|
||||
|
@ -19,7 +19,7 @@ trait CanSortResults
|
|||
QueryBuilder $queryBuilder,
|
||||
array $sortLookup,
|
||||
?string $defaultSort,
|
||||
string $defaultSortOrder = Criteria::ASC
|
||||
Order $defaultSortOrder = Order::Ascending
|
||||
): QueryBuilder {
|
||||
[$sort, $sortOrder] = $this->getSortFromRequest($request, $defaultSortOrder);
|
||||
|
||||
|
@ -31,7 +31,7 @@ trait CanSortResults
|
|||
return $queryBuilder;
|
||||
}
|
||||
|
||||
return $queryBuilder->addOrderBy($sortValue, $sortOrder);
|
||||
return $queryBuilder->addOrderBy($sortValue, $sortOrder->value);
|
||||
}
|
||||
|
||||
protected function sortArray(
|
||||
|
@ -39,7 +39,7 @@ trait CanSortResults
|
|||
array $results,
|
||||
array $sortLookup,
|
||||
?string $defaultSort = null,
|
||||
string $defaultSortOrder = Criteria::ASC
|
||||
Order $defaultSortOrder = Order::Ascending
|
||||
): array {
|
||||
[$sort, $sortOrder] = $this->getSortFromRequest($request, $defaultSortOrder);
|
||||
|
||||
|
@ -62,18 +62,21 @@ trait CanSortResults
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array{string|null, Criteria::ASC|Criteria::DESC}
|
||||
* @return array{string|null, Order}
|
||||
*/
|
||||
protected function getSortFromRequest(
|
||||
ServerRequest $request,
|
||||
string $defaultSortOrder = Criteria::ASC
|
||||
Order $defaultSortOrder = Order::Ascending
|
||||
): array {
|
||||
$sortOrder = Types::stringOrNull($request->getParam('sortOrder'), true) ?? $defaultSortOrder;
|
||||
$sortOrder = Types::stringOrNull($request->getParam('sortOrder'), true);
|
||||
|
||||
$orderEnum = (null !== $sortOrder)
|
||||
? Order::tryFrom(strtoupper($sortOrder)) ?? $defaultSortOrder
|
||||
: $defaultSortOrder;
|
||||
|
||||
return [
|
||||
Types::stringOrNull($request->getParam('sort'), true),
|
||||
(Criteria::DESC === strtoupper($sortOrder))
|
||||
? Criteria::DESC
|
||||
: Criteria::ASC,
|
||||
$orderEnum,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -82,7 +85,7 @@ trait CanSortResults
|
|||
object|array $b,
|
||||
PropertyAccessorInterface $propertyAccessor,
|
||||
string $sortValue,
|
||||
string $sortOrder
|
||||
Order $sortOrder = Order::Ascending
|
||||
): int {
|
||||
$aVal = $propertyAccessor->getValue($a, $sortValue);
|
||||
$bVal = $propertyAccessor->getValue($b, $sortValue);
|
||||
|
@ -94,7 +97,7 @@ trait CanSortResults
|
|||
$bVal = mb_strtolower($bVal, 'UTF-8');
|
||||
}
|
||||
|
||||
return (Criteria::ASC === $sortOrder)
|
||||
return (Order::Ascending === $sortOrder)
|
||||
? $aVal <=> $bVal
|
||||
: $bVal <=> $aVal;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue