Skip to content

Commit 2dd27e0

Browse files
committed
Use 8.6 SortDirection enum #1315
1 parent f7db89a commit 2dd27e0

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"paragonie/constant_time_encoding": "^2.8||^3.1",
2828
"composer/ca-bundle": "^1.2",
2929
"symfony/options-resolver": "^6.0||^7.0||^8.0",
30-
"asika/object-metadata": "^1.0"
30+
"asika/object-metadata": "^1.0",
31+
"symfony/polyfill-php86": "^1.38"
3132
},
3233
"require-dev": {
3334
"asika/sql-splitter": "^1.0",

packages/query/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"license": "MIT",
1414
"require": {
1515
"php": ">=8.4.6",
16-
"windwalker/utilities": "^4.0"
16+
"windwalker/utilities": "^4.0",
17+
"symfony/polyfill-php86": "^1.38"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^10.0||^11.0||^12.0",

packages/query/src/Query.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use IteratorAggregate;
1212
use PDO;
1313
use ReflectionClass;
14+
use SortDirection;
1415
use SqlFormatter;
1516
use Stringable;
1617
use WeakReference;
@@ -577,13 +578,16 @@ private function val(mixed $value): ValueClause|QuoteNameClause
577578

578579
/**
579580
* @param array|string|ClauseInterface $column
580-
* @param string|null $dir
581+
* @param \SortDirection|string|null $dir
581582
* @param ClausePosition $pos
582583
*
583584
* @return static
584585
*/
585-
public function order(mixed $column, ?string $dir = null, ClausePosition $pos = ClausePosition::APPEND): static
586-
{
586+
public function order(
587+
mixed $column,
588+
\SortDirection|string|null $dir = null,
589+
ClausePosition $pos = ClausePosition::APPEND
590+
): static {
587591
if (is_array($column)) {
588592
foreach ($column as $col) {
589593
if (!is_array($col)) {
@@ -599,20 +603,32 @@ public function order(mixed $column, ?string $dir = null, ClausePosition $pos =
599603
$order = [$this->resolveColumn($column)];
600604

601605
if ($dir !== null) {
602-
ArgumentsAssert::assert(
603-
in_array($dir = strtoupper($dir), ['ASC', 'DESC'], true),
604-
'{caller} argument 2 should be one of ASC/DESC, %s given',
605-
$dir
606-
);
607-
608-
$order[] = $dir;
606+
$order[] = static::unwrapDirectionEnum($dir);
609607
}
610608

611609
$this->orderRaw($this->clause('', $order), $pos);
612610

613611
return $this;
614612
}
615613

614+
private static function unwrapDirectionEnum(\SortDirection|string $dir): string
615+
{
616+
if ($dir instanceof \SortDirection) {
617+
return match ($dir) {
618+
SortDirection::Ascending => 'ASC',
619+
SortDirection::Descending => 'DESC',
620+
};
621+
}
622+
623+
$dir = strtoupper($dir);
624+
625+
if ($dir !== 'ASC' && $dir !== 'DESC') {
626+
throw new \InvalidArgumentException("Invalid sort direction: $dir");
627+
}
628+
629+
return $dir;
630+
}
631+
616632
public function orderRaw(string|Clause $order, mixed ...$args): static
617633
{
618634
if (!$this->order) {

packages/query/test/QueryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,29 @@ public function testOrder(): void
14681468
);
14691469
}
14701470

1471+
public function testOrderByPhp86Enum(): void
1472+
{
1473+
$q = self::createQuery()
1474+
->select('*')
1475+
->from('foo')
1476+
->order(
1477+
[
1478+
['id', \SortDirection::Ascending],
1479+
'f1',
1480+
['f2', \SortDirection::Descending],
1481+
'f3',
1482+
]
1483+
)
1484+
->order('f4', \SortDirection::Descending)
1485+
->order('p1', \SortDirection::Ascending, Query::PREPEND)
1486+
->order(raw('COUNT(f5)'));
1487+
1488+
self::assertSqlEquals(
1489+
'SELECT * FROM "foo" ORDER BY "p1" ASC, "id" ASC, "f1", "f2" DESC, "f3", "f4" DESC, COUNT(f5)',
1490+
$q->render()
1491+
);
1492+
}
1493+
14711494
public function testGroup()
14721495
{
14731496
$q = self::createQuery()

0 commit comments

Comments
 (0)