@@ -12,8 +12,6 @@ class Sieve
12
12
13
13
protected $ defaultSort = null ;
14
14
15
- protected $ sortable = [];
16
-
17
15
public function __construct (Request $ request )
18
16
{
19
17
$ this ->request = $ request ;
@@ -57,8 +55,41 @@ public function apply($queryBuilder)
57
55
$ search = new SearchTerm ($ property , $ operator , $ property , $ term );
58
56
$ filter ->modifyQuery ($ queryBuilder , $ search );
59
57
}
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
+ }
60
90
61
91
$ column = $ property ;
92
+ $ filter = $ filterRule ['filter ' ];
62
93
while ($ filter instanceof WrapsFilter) {
63
94
if ($ filter instanceof MapFilter) {
64
95
$ column = $ filter ->target ();
@@ -68,47 +99,33 @@ public function apply($queryBuilder)
68
99
$ filter = $ filter ->getWrapped ();
69
100
}
70
101
71
- if (strpos ($ column , ". " ) !== false ) {
102
+ if (str_contains ($ column , ". " )) {
72
103
continue ;
73
104
}
74
105
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
+ }
80
109
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
+ }
84
113
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
+ }
89
118
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 ' );
94
122
}
95
123
}
96
-
97
- return $ this ;
98
124
}
99
125
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
106
127
{
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 );
111
130
}
112
-
113
-
114
131
}
0 commit comments