Skip to content

Commit f65a730

Browse files
committed
allowEmptySearchQuery() + new() method
1 parent 826bd3d commit f65a730

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

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

5+
## 1.4.0 - 2020-10-28
6+
7+
- Allow empty search terms
8+
- Added `new()` method method
9+
10+
## 1.3.1 - 2020-10-28
11+
12+
- Docs
13+
514
## 1.3.0 - 2020-09-24
615

716
- Support for Laravel 8.0

src/Search.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/*
8+
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher new()
89
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByAsc()
910
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByDesc()
1011
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher dontParseTerm()

src/SearchFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ class SearchFactory
88
{
99
use ForwardsCalls;
1010

11+
/**
12+
* Returns a new Searcher instance.
13+
*
14+
* @return \ProtoneMedia\LaravelCrossEloquentSearch\Searcher
15+
*/
16+
public function new(): Searcher
17+
{
18+
return new Searcher;
19+
}
20+
1121
/**
1222
* Handle dynamic method calls into a new Searcher instance.
1323
*
@@ -18,7 +28,7 @@ class SearchFactory
1828
public function __call($method, $parameters)
1929
{
2030
return $this->forwardCallTo(
21-
new Searcher,
31+
$this->new(),
2232
$method,
2333
$parameters
2434
);

src/Searcher.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Searcher
2626
*/
2727
private bool $startWithWildcard = false;
2828

29+
/**
30+
* Allow an empty search query.
31+
*/
32+
private bool $allowEmptySearchQuery = false;
33+
2934
/**
3035
* Collection of search terms.
3136
*/
@@ -97,6 +102,16 @@ public function dontParseTerm(): self
97102
return $this;
98103
}
99104

105+
/**
106+
* Allow empty search terms.
107+
*/
108+
public function allowEmptySearchQuery(): self
109+
{
110+
$this->allowEmptySearchQuery = true;
111+
112+
return $this;
113+
}
114+
100115
/**
101116
* Add a model to search through.
102117
*
@@ -180,7 +195,7 @@ private function initializeTerms(string $terms): self
180195
->filter()
181196
->map(fn ($term) => ($this->startWithWildcard ? '%' : '') . "{$term}%");
182197

183-
if ($this->terms->isEmpty()) {
198+
if (!$this->allowEmptySearchQuery && $this->terms->isEmpty()) {
184199
throw new EmptySearchQueryException;
185200
}
186201

tests/SearchTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ public function it_throws_an_exception_when_the_query_is_empty()
119119
$this->fail('Should have thrown EmptySearchQueryException.');
120120
}
121121

122+
/** @test */
123+
public function it_can_search_without_a_term()
124+
{
125+
Post::create(['title' => 'foo']);
126+
Post::create(['title' => 'bar']);
127+
Video::create(['title' => 'foo']);
128+
Video::create(['title' => 'bar']);
129+
130+
$results = Search::new()
131+
->add(Post::class, 'title')
132+
->add(Video::class, 'title')
133+
->allowEmptySearchQuery()
134+
->get();
135+
136+
$this->assertCount(4, $results);
137+
}
138+
122139
/** @test */
123140
public function it_can_search_on_the_left_side_of_the_term()
124141
{

0 commit comments

Comments
 (0)