File tree 3 files changed +61
-2
lines changed
3 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -180,6 +180,7 @@ $search = \JustBetter\MagentoClient\Query\SearchCriteria::make()
180
180
->orWhere('price', '>', 10),
181
181
->whereIn('sku', ['123', '456'])
182
182
->orWhereNull('name')
183
+ ->orderBy('sku')
183
184
->paginate(1, 50)
184
185
->get();
185
186
```
Original file line number Diff line number Diff line change @@ -10,12 +10,15 @@ class SearchCriteria
10
10
11
11
public array $ wheres = [];
12
12
13
+ public array $ orders = [];
14
+
13
15
protected int $ currentFilterGroup = 0 ;
14
16
15
17
protected int $ currentFilterIndex = 0 ;
16
18
17
- public function __construct (protected Grammar $ grammar )
18
- {
19
+ public function __construct (
20
+ protected Grammar $ grammar
21
+ ) {
19
22
}
20
23
21
24
public function paginate (int $ page , int $ pageSize ): static
@@ -169,11 +172,27 @@ protected function addWhere(
169
172
return $ this ;
170
173
}
171
174
175
+ public function orderBy (string $ field , string $ direction = 'ASC ' ): static
176
+ {
177
+ $ index = count ($ this ->orders ) / 2 ;
178
+
179
+ $ this ->orders ["searchCriteria[sortOrders][ $ index][field] " ] = $ field ;
180
+ $ this ->orders ["searchCriteria[sortOrders][ $ index][direction] " ] = $ direction ;
181
+
182
+ return $ this ;
183
+ }
184
+
185
+ public function orderByDesc (string $ field ): static
186
+ {
187
+ return $ this ->orderBy ($ field , 'DESC ' );
188
+ }
189
+
172
190
public function get (): array
173
191
{
174
192
return array_merge (
175
193
$ this ->select ,
176
194
$ this ->wheres ,
195
+ $ this ->orders ,
177
196
$ this ->pagination ,
178
197
);
179
198
}
Original file line number Diff line number Diff line change @@ -260,4 +260,43 @@ public function test_it_can_add_orWhereNotNull(): void
260
260
'searchCriteria[filter_groups][0][filters][1][condition_type] ' => 'notnull ' ,
261
261
], $ searchCriteria );
262
262
}
263
+
264
+ public function test_it_can_orderBy (): void
265
+ {
266
+ $ searchCriteria = SearchCriteria::make ()
267
+ ->orderBy ('sku ' )
268
+ ->get ();
269
+
270
+ $ this ->assertEquals ([
271
+ 'searchCriteria[sortOrders][0][field] ' => 'sku ' ,
272
+ 'searchCriteria[sortOrders][0][direction] ' => 'ASC ' ,
273
+ ], $ searchCriteria );
274
+ }
275
+
276
+ public function test_it_can_orderByDesc (): void
277
+ {
278
+ $ searchCriteria = SearchCriteria::make ()
279
+ ->orderByDesc ('sku ' )
280
+ ->get ();
281
+
282
+ $ this ->assertEquals ([
283
+ 'searchCriteria[sortOrders][0][field] ' => 'sku ' ,
284
+ 'searchCriteria[sortOrders][0][direction] ' => 'DESC ' ,
285
+ ], $ searchCriteria );
286
+ }
287
+
288
+ public function test_it_can_add_multiple_orders (): void
289
+ {
290
+ $ searchCriteria = SearchCriteria::make ()
291
+ ->orderBy ('sku ' )
292
+ ->orderByDesc ('entity_id ' )
293
+ ->get ();
294
+
295
+ $ this ->assertEquals ([
296
+ 'searchCriteria[sortOrders][0][field] ' => 'sku ' ,
297
+ 'searchCriteria[sortOrders][0][direction] ' => 'ASC ' ,
298
+ 'searchCriteria[sortOrders][1][field] ' => 'entity_id ' ,
299
+ 'searchCriteria[sortOrders][1][direction] ' => 'DESC ' ,
300
+ ], $ searchCriteria );
301
+ }
263
302
}
You can’t perform that action at this time.
0 commit comments