Skip to content

Commit 69c15af

Browse files
Merge 5.8 into 5.x (#3520)
2 parents e7ffd4f + 4afafeb commit 69c15af

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

src/Query/Builder.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,16 @@ public function distinct($column = false)
664664
#[Override]
665665
public function orderBy($column, $direction = 'asc')
666666
{
667-
if (is_string($direction) || $direction instanceof SortDirection) {
667+
if ($direction instanceof SortDirection) {
668668
$direction = match ($direction) {
669-
'asc', 'ASC', SortDirection::Ascending => 1,
670-
'desc', 'DESC', SortDirection::Descending => -1,
669+
SortDirection::Ascending => 1,
670+
SortDirection::Descending => -1,
671+
default => throw new InvalidArgumentException('Unexpected SortDirection enum case.'),
672+
};
673+
} elseif (is_string($direction)) {
674+
$direction = match ($direction) {
675+
'asc', 'ASC' => 1,
676+
'desc', 'DESC' => -1,
671677
default => throw new InvalidArgumentException('Order direction must be "asc", "desc" or a case from the SortDirection enum.'),
672678
};
673679
}

tests/Query/BuilderTest.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use SortDirection;
2525
use stdClass;
2626

27+
use function class_exists;
2728
use function collect;
2829
use function method_exists;
2930
use function now;
@@ -567,20 +568,22 @@ function (Builder $builder) {
567568
fn (Builder $builder) => $builder->orderByDesc('email'),
568569
];
569570

570-
yield 'orderBy SortDirection::Ascending' => [
571-
['find' => [[], ['sort' => ['email' => 1]]]],
572-
fn (Builder $builder) => $builder->orderBy('email', SortDirection::Ascending),
573-
];
571+
if (class_exists(SortDirection::class)) {
572+
yield 'orderBy SortDirection::Ascending' => [
573+
['find' => [[], ['sort' => ['email' => 1]]]],
574+
fn (Builder $builder) => $builder->orderBy('email', SortDirection::Ascending),
575+
];
574576

575-
yield 'orderBy SortDirection::Descending' => [
576-
['find' => [[], ['sort' => ['email' => -1]]]],
577-
fn (Builder $builder) => $builder->orderBy('email', SortDirection::Descending),
578-
];
577+
yield 'orderBy SortDirection::Descending' => [
578+
['find' => [[], ['sort' => ['email' => -1]]]],
579+
fn (Builder $builder) => $builder->orderBy('email', SortDirection::Descending),
580+
];
579581

580-
yield 'orderBy SortDirection on natural' => [
581-
['find' => [[], ['sort' => ['$natural' => -1]]]],
582-
fn (Builder $builder) => $builder->orderBy('natural', SortDirection::Descending),
583-
];
582+
yield 'orderBy SortDirection on natural' => [
583+
['find' => [[], ['sort' => ['$natural' => -1]]]],
584+
fn (Builder $builder) => $builder->orderBy('natural', SortDirection::Descending),
585+
];
586+
}
584587

585588
/** @see DatabaseQueryBuilderTest::testReorder() */
586589
yield 'reorder reset' => [

0 commit comments

Comments
 (0)