Skip to content

Commit

Permalink
Merge pull request #38 from ans-group/not-like
Browse files Browse the repository at this point in the history
Add nlk filter and update filter documentation in README
  • Loading branch information
Gman98ish authored Mar 30, 2023
2 parents 6edf3fc + e745a2b commit 0faa6fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Filters are done in the query parameters with the format `property:operator=term

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

### Like & Not Like

The string filter also supports like and not like operators. To use these you should use `*` as the wildcard
rather than `%` as you would in a typical database query.

For instance `first_name:lk=Sam*` would match `Sam`, `Samuel` and `Samantha`.

## Sorting

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

### String

The basic filter. Ideal for textual data, implements `eq`, `neq`, `in`, and `nin`
The basic filter. Ideal for textual data, implements `eq`, `neq`, `in`, `nin`, `lk` and `nlk`

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

### Date
Expand Down
5 changes: 4 additions & 1 deletion src/Filters/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function modifyQuery($query, SearchTerm $search)
if ($search->operator() == 'lk') {
$query->where($search->column(), 'LIKE', $this->prepareLike($search->term()));
}
if ($search->operator() == 'nlk') {
$query->where($search->column(), 'NOT LIKE', $this->prepareLike($search->term()));
}
}

protected function prepareLike($term)
Expand Down Expand Up @@ -67,6 +70,6 @@ private function shouldEscape($string, $pos)

public function operators()
{
return ['eq', 'neq', 'in', 'nin', 'lk'];
return ['eq', 'neq', 'in', 'nin', 'lk', 'nlk'];
}
}
13 changes: 13 additions & 0 deletions tests/Filters/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ public function correctly_applies_lk_operator()
$this->assertEquals('select * where "name" LIKE ?', $builder->toSql());
$this->assertEquals(['%t\est*'], $builder->getBindings());
}

/**
* @test
*/
public function correctly_applies_nlk_operator()
{
$search = new SearchTerm('name', 'nlk', 'name', '*t\\\\est\\*');
$builder = app(Builder::class);

(new StringFilter)->modifyQuery($builder, $search);
$this->assertEquals('select * where "name" NOT LIKE ?', $builder->toSql());
$this->assertEquals(['%t\est*'], $builder->getBindings());
}
}

0 comments on commit 0faa6fa

Please sign in to comment.