Skip to content

Commit 0faa6fa

Browse files
authored
Merge pull request #38 from ans-group/not-like
Add nlk filter and update filter documentation in README
2 parents 6edf3fc + e745a2b commit 0faa6fa

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ Filters are done in the query parameters with the format `property:operator=term
6363

6464
By default, if no operator is specified, it will default to `eq` so `name=Bob` will expand to `name:eq=Bob`
6565

66+
### Like & Not Like
67+
68+
The string filter also supports like and not like operators. To use these you should use `*` as the wildcard
69+
rather than `%` as you would in a typical database query.
70+
71+
For instance `first_name:lk=Sam*` would match `Sam`, `Samuel` and `Samantha`.
72+
6673
## Sorting
6774

6875
You can set the fields that can be sortable when setting up your model.
@@ -114,7 +121,7 @@ $sieve->setDefaultSort('name', 'asc')
114121

115122
### String
116123

117-
The basic filter. Ideal for textual data, implements `eq`, `neq`, `in`, and `nin`
124+
The basic filter. Ideal for textual data, implements `eq`, `neq`, `in`, `nin`, `lk` and `nlk`
118125

119126
```php
120127
<?php
@@ -147,6 +154,7 @@ Only provides `eq` and `neq`. Also takes two arguments to specify what your true
147154
```php
148155
<?php
149156
$filter->boolean() // defaults to 1 and 0
157+
$filter->boolean('Yes', 'No') // search for Yes and No in the database
150158
```
151159

152160
### Date

src/Filters/StringFilter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function modifyQuery($query, SearchTerm $search)
2424
if ($search->operator() == 'lk') {
2525
$query->where($search->column(), 'LIKE', $this->prepareLike($search->term()));
2626
}
27+
if ($search->operator() == 'nlk') {
28+
$query->where($search->column(), 'NOT LIKE', $this->prepareLike($search->term()));
29+
}
2730
}
2831

2932
protected function prepareLike($term)
@@ -67,6 +70,6 @@ private function shouldEscape($string, $pos)
6770

6871
public function operators()
6972
{
70-
return ['eq', 'neq', 'in', 'nin', 'lk'];
73+
return ['eq', 'neq', 'in', 'nin', 'lk', 'nlk'];
7174
}
7275
}

tests/Filters/StringFilterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ public function correctly_applies_lk_operator()
8585
$this->assertEquals('select * where "name" LIKE ?', $builder->toSql());
8686
$this->assertEquals(['%t\est*'], $builder->getBindings());
8787
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function correctly_applies_nlk_operator()
93+
{
94+
$search = new SearchTerm('name', 'nlk', 'name', '*t\\\\est\\*');
95+
$builder = app(Builder::class);
96+
97+
(new StringFilter)->modifyQuery($builder, $search);
98+
$this->assertEquals('select * where "name" NOT LIKE ?', $builder->toSql());
99+
$this->assertEquals(['%t\est*'], $builder->getBindings());
100+
}
88101
}

0 commit comments

Comments
 (0)