@@ -12,8 +12,6 @@ class Sieve
1212
1313 protected $ defaultSort = null ;
1414
15- protected $ sortable = [];
16-
1715 public function __construct (Request $ request )
1816 {
1917 $ this ->request = $ request ;
@@ -57,8 +55,41 @@ public function apply($queryBuilder)
5755 $ search = new SearchTerm ($ property , $ operator , $ property , $ term );
5856 $ filter ->modifyQuery ($ queryBuilder , $ search );
5957 }
58+ }
59+
60+ if ($ this ->request ->has ('sort ' )) {
61+ $ this ->applyRequestSorts ($ queryBuilder );
62+ } elseif ($ this ->defaultSort ) {
63+ $ this ->applyDefaultSort ($ queryBuilder );
64+ }
65+
66+ return $ this ;
67+ }
68+
69+ public function getSort (): ?string
70+ {
71+ return $ this ->request ->get ("sort " ) ?? $ this ->defaultSort ;
72+ }
73+
74+ public function setDefaultSort ($ property = 'id ' , $ direction = 'asc ' ): Sieve
75+ {
76+ $ this ->defaultSort = $ property . ': ' . $ direction ;
77+
78+ return $ this ;
79+ }
80+
81+ protected function applyRequestSorts ($ queryBuilder ): void {
82+ $ sorts = explode (', ' , $ this ->getSort ());
83+ foreach ($ sorts as $ sort ) {
84+ $ property = explode (': ' , $ sort )[0 ];
85+
86+ $ filterRule = collect ($ this ->getFilters ())->firstWhere ('property ' , $ property );
87+ if (!$ filterRule ) {
88+ continue ;
89+ }
6090
6191 $ column = $ property ;
92+ $ filter = $ filterRule ['filter ' ];
6293 while ($ filter instanceof WrapsFilter) {
6394 if ($ filter instanceof MapFilter) {
6495 $ column = $ filter ->target ();
@@ -68,47 +99,33 @@ public function apply($queryBuilder)
6899 $ filter = $ filter ->getWrapped ();
69100 }
70101
71- if (strpos ($ column , ". " ) !== false ) {
102+ if (str_contains ($ column , ". " )) {
72103 continue ;
73104 }
74105
75- $ sorts = explode (', ' , $ this ->getSort ());
76- foreach ($ sorts as $ sort ) {
77- if ($ sort == "$ property:desc " ) {
78- $ queryBuilder ->orderBy ($ column , "desc " );
79- }
106+ if ($ sort == "$ property:desc " ) {
107+ $ queryBuilder ->orderBy ($ column , "desc " );
108+ }
80109
81- if ($ sort == "$ property:asc " || $ sort == $ property ) {
82- $ queryBuilder ->orderBy ($ column , "asc " );
83- }
110+ if ($ sort == "$ property:asc " || $ sort == $ property ) {
111+ $ queryBuilder ->orderBy ($ column , "asc " );
112+ }
84113
85- if ($ sort == "$ property:asc_nulls_last " ) {
86- $ queryBuilder ->orderByRaw ("ISNULL( $ column) asc " )
87- ->orderBy ($ column , 'asc ' );
88- }
114+ if ($ sort == "$ property:asc_nulls_last " ) {
115+ $ queryBuilder ->orderByRaw ("ISNULL( $ column) asc " )
116+ ->orderBy ($ column , 'asc ' );
117+ }
89118
90- if ($ sort == "$ property:desc_nulls_first " ) {
91- $ queryBuilder ->orderByRaw ("ISNULL( $ column) desc " )
92- ->orderBy ($ column , 'desc ' );
93- }
119+ if ($ sort == "$ property:desc_nulls_first " ) {
120+ $ queryBuilder ->orderByRaw ("ISNULL( $ column) desc " )
121+ ->orderBy ($ column , 'desc ' );
94122 }
95123 }
96-
97- return $ this ;
98124 }
99125
100- public function getSort (): ?string
101- {
102- return $ this ->request ->get ("sort " ) ?? $ this ->defaultSort ;
103- }
104-
105- public function setDefaultSort ($ property = 'id ' , $ direction = 'asc ' ): Sieve
126+ protected function applyDefaultSort ($ queryBuilder ): void
106127 {
107- $ this ->sortable [] = $ property ;
108- $ this ->defaultSort = $ property . ': ' . $ direction ;
109-
110- return $ this ;
128+ list ($ column , $ direction ) = explode (': ' , $ this ->defaultSort );
129+ $ queryBuilder ->orderBy ($ column , $ direction );
111130 }
112-
113-
114131}
0 commit comments