1111use IteratorAggregate ;
1212use PDO ;
1313use ReflectionClass ;
14+ use SortDirection ;
1415use SqlFormatter ;
1516use Stringable ;
1617use 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 ) {
0 commit comments