Skip to content

Commit 5add1f3

Browse files
committed
Updated README
1 parent b84ecad commit 5add1f3

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ By default, if no operator is specified, it will default to `eq` so `name=Bob` w
6363

6464
## Sorting
6565

66+
You can set the fields that can be sortable when setting up your model.
67+
You can pass an array of fields as the second parameter of configure.
68+
It does not have to match the filterable, allowing total control.
69+
70+
```php
71+
<?php
72+
73+
class Pet extends Model implements Searchable
74+
{
75+
public function sieve(Sieve $sieve)
76+
{
77+
$sieve->configure(fn ($filter) => [
78+
'name' => $filter->string(),
79+
'breed' => $filter->enum(['Beagle', 'Tiger']),
80+
],
81+
['name']
82+
);
83+
}
84+
}
85+
```
86+
6687
Sieve will also allow consumers of your API to specify sort order. You can do this by `sort=property:direction`
6788

6889
* `sort=age:asc`

src/Sieve.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public function __construct(Request $request)
1919
$this->request = $request;
2020
}
2121

22-
public function configure($callback, array $sortable=[])
22+
public function configure($callback, array $sortable = [])
2323
{
2424
foreach ($callback(new FilterBuilder) as $prop => $filter) {
2525
$this->addFilter($prop, $filter);
2626
}
2727

2828
foreach ($sortable as $sort) {
29-
$this->sortable[] = $sort;
29+
$this->addSort($sort);
3030
}
3131
}
3232

@@ -75,10 +75,10 @@ public function apply($queryBuilder)
7575
return $this;
7676
}
7777

78-
public function setDefaultSort($property='id', $direction='asc'): Sieve
78+
public function setDefaultSort($property = 'id', $direction = 'asc'): Sieve
7979
{
8080
$this->sortable[] = $property;
81-
$this->defaultSort = $property.':'.$direction;
81+
$this->defaultSort = $property . ':' . $direction;
8282

8383
return $this;
8484
}

tests/SieveTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function filters_and_sorts()
1616
{
1717
$request = Request::create('/', 'GET', [
1818
'name:in' => 'Snoopy,Hobbes',
19-
'sort' => 'name:desc'
19+
'sort' => 'name:desc'
2020
]);
2121

2222
$seive = new Sieve($request);
@@ -86,7 +86,7 @@ public function set_default_sort_filter()
8686
$request = Request::create('/');
8787

8888
$sieve = new Sieve($request);
89-
$sieve->setDefaultSort('name','desc');
89+
$sieve->setDefaultSort('name', 'desc');
9090

9191
$this->assertEquals($sieve->getSort(), ['sortBy' => 'name', 'sortDirection' => 'desc']);
9292
}
@@ -134,7 +134,6 @@ public function ignores_undefined_sort()
134134
$seive->apply($builder);
135135

136136
$this->assertEquals(null, $builder->orders);
137-
138137
}
139138

140139
/**
@@ -155,6 +154,5 @@ public function ignores_invalid_sort_direction()
155154
$seive->apply($builder);
156155

157156
$this->assertEquals(null, $builder->orders);
158-
159157
}
160158
}

0 commit comments

Comments
 (0)