Skip to content

Commit

Permalink
Added 'dontParseTerm' method
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Aug 10, 2020
1 parent 787b1fc commit 119be46
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-cross-eloquent-search` will be documented in this file

## 1.1.0 - 2020-08-10

- option to disable the parsing of the search term

## 1.0.0 - 2020-07-08

- initial release
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ Search::add(Post::class, 'title')
->get('"macos big sur"');
```

You can disable the parsing of the search term by calling the `dontParseTerm` method, which gives you the same results as using double-quotes.

```php
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->dontParseTerm()
->get('macos big sur');
```

### Pagination

We highly recommend to paginate your results. Call the `paginate` method before the `get` method, and you'll get an instance of `\Illuminate\Contracts\Pagination\LengthAwarePaginator` as a result. The `paginate` method takes three (optional) parameters to customize the paginator. These arguments are [the same](https://laravel.com/docs/master/pagination#introduction) as Laravel's database paginator.
Expand Down
19 changes: 18 additions & 1 deletion src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Searcher
*/
private string $pageName = 'page';

/**
* Parse the search term into multiple terms.
*/
private bool $parseTerm = true;

/**
* Current page.
*
Expand Down Expand Up @@ -82,6 +87,16 @@ public function orderByDesc(): self
return $this;
}

/**
* Disable the parsing of the search term.
*/
public function dontParseTerm(): self
{
$this->parseTerm = false;

return $this;
}

/**
* Add a model to search through.
*
Expand Down Expand Up @@ -142,7 +157,9 @@ public function paginate($perPage = 15, $pageName = 'page', $page = null): self
*/
private function initializeTerms(string $terms): self
{
$this->terms = Collection::make(str_getcsv($terms, ' ', '"'))
$terms = $this->parseTerm ? str_getcsv($terms, ' ', '"') : $terms;

$this->terms = Collection::wrap($terms)
->filter()
->map(fn ($term) => ($this->startWithWildcard ? '%' : '') . "{$term}%");

Expand Down
18 changes: 18 additions & 0 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public function it_can_search_for_a_phrase()
$this->assertTrue($results->contains($postB));
}

/** @test */
public function it_has_an_option_to_dont_split_the_search_term()
{
$postA = Post::create(['title' => 'foo']);
$postB = Post::create(['title' => 'bar bar']);
$videoA = Video::create(['title' => 'foo']);
$videoB = Video::create(['title' => 'bar']);

$results = Search::add(Post::class, 'title')
->add(Video::class, 'title')
->dontParseTerm()
->get('bar bar');

$this->assertCount(1, $results);

$this->assertTrue($results->contains($postB));
}

/** @test */
public function it_throws_an_exception_when_the_query_is_empty()
{
Expand Down

0 comments on commit 119be46

Please sign in to comment.