Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 3f7e3ad

Browse files
committed
Move build wheres logic to base mode class. Add support for operators in where clauses
1 parent d03996d commit 3f7e3ad

File tree

7 files changed

+62
-42
lines changed

7 files changed

+62
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ Simply call the `search()` method on your `Searchable` models:
7878

7979
`$beers = App\Drink::search('beer')->get();`
8080

81+
Or With pagination:
82+
83+
`$beers = App\Drink::search('beer')->paginate(15);`
84+
85+
Simple constraints can be applied using the `where()` builder method
86+
(each additional `WHERE` will be applied using `AND`).
87+
88+
`$beers = App\Drink::search('beer')->where('in_stock', 1)->get();`
89+
90+
The following operators can be applied to the `WHERE` statements: `<> != = <= < >= >`
91+
(`=` will be used if no operator is specified)
92+
93+
`$beers = App\Drink::search('beer')->where('abv >', 10)->get();`
94+
8195
For more usage information see the [Laravel Scout Documentation](https://laravel.com/docs/5.3/scout).
8296

8397
Modes <div id="modes"></div>

src/Engines/Modes/Boolean.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public function buildWhereRawString()
2121
{
2222
$queryString = '';
2323

24-
foreach ($this->builder->wheres as $field => $value) {
25-
$queryString .= "$field = :$field AND ";
26-
}
24+
$queryString .= $this->buildWheres();
2725

2826
$indexFields = implode(',', $this->modelService->getFullTextIndexFields());
2927

@@ -34,14 +32,9 @@ public function buildWhereRawString()
3432

3533
public function buildParams()
3634
{
37-
$params = [];
3835

39-
foreach ($this->builder->wheres as $field => $value) {
40-
$params[$field] = $value;
41-
}
42-
43-
$params['_search'] = $this->builder->query;
44-
return $params;
36+
$this->whereParams['_search'] = $this->builder->query;
37+
return $this->whereParams;
4538
}
4639

4740
public function isFullText()

src/Engines/Modes/Like.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public function buildWhereRawString()
2525

2626
$this->fields = $this->modelService->getSearchableFields();
2727

28-
foreach ($this->builder->wheres as $field => $value) {
29-
$queryString .= "$field = :$field AND ";
30-
}
28+
$queryString .= $this->buildWheres();
3129

3230
$queryString .= '(';
3331

@@ -46,17 +44,11 @@ public function buildWhereRawString()
4644

4745
public function buildParams()
4846
{
49-
$params = [];
50-
51-
foreach ($this->builder->wheres as $field => $value) {
52-
$params[$field] = $value;
53-
}
54-
5547
for ($itr = 0; $itr < count($this->fields); $itr++) {
56-
$params["_search$itr"] = '%' . $this->builder->query . '%';
48+
$this->whereParams["_search$itr"] = '%' . $this->builder->query . '%';
5749
}
5850

59-
return $params;
51+
return $this->whereParams;
6052
}
6153

6254
public function isFullText()

src/Engines/Modes/LikeExpanded.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public function buildWhereRawString()
2525

2626
$this->fields = $this->modelService->getSearchableFields();
2727

28-
foreach ($this->builder->wheres as $field => $value) {
29-
$queryString .= "$field = :$field AND ";
30-
}
28+
$queryString .= $this->buildWheres();
3129

3230
$words = explode(' ', $this->builder->query);
3331

@@ -53,21 +51,15 @@ public function buildParams()
5351
{
5452
$words = explode(' ', $this->builder->query);
5553

56-
$params = [];
57-
58-
foreach ($this->builder->wheres as $field => $value) {
59-
$params[$field] = $value;
60-
}
61-
6254
$itr = 0;
6355
for ($i = 0; $i < count($this->fields); $i++) {
6456
foreach ($words as $word) {
65-
$params["_search$itr"] = '%' . $word . '%';
57+
$this->whereParams["_search$itr"] = '%' . $word . '%';
6658
$itr ++;
6759
}
6860
}
6961

70-
return $params;
62+
return $this->whereParams;
7163

7264
}
7365

src/Engines/Modes/Mode.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ abstract class Mode
88
{
99
protected $builder;
1010

11+
protected $whereParams = [];
12+
1113
function __construct(Builder $builder)
1214
{
1315
$this->builder = $builder;
@@ -19,4 +21,37 @@ abstract public function buildParams();
1921

2022
abstract public function isFullText();
2123

24+
protected function buildWheres()
25+
{
26+
27+
$queryString = '';
28+
29+
$parsedWheres = $this->parseWheres($this->builder->wheres);
30+
31+
foreach ($parsedWheres as $parsedWhere) {
32+
33+
$field = $parsedWhere[0];
34+
$operator = $parsedWhere[1];
35+
$value = $parsedWhere[2];
36+
37+
$this->whereParams[$field] = $value;
38+
39+
$queryString .= "$field $operator :$field AND ";
40+
}
41+
42+
return $queryString;
43+
}
44+
45+
private function parseWheres($wheres)
46+
{
47+
$pattern = '/([A-Za-z_]+[A-Za-z_0-9]?)[ ]?(<>|!=|=|<=|<|>=|>)/';
48+
49+
$result = array();
50+
foreach($wheres as $field => $value) {
51+
preg_match($pattern, $field, $matches);
52+
$result []= !empty($matches) ? array($matches[1], $matches[2], $value) : array($field, '=', $value);
53+
}
54+
return $result;
55+
}
56+
2257
}

src/Engines/Modes/NaturalLanguage.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public function buildWhereRawString()
2121
{
2222
$queryString = '';
2323

24-
foreach ($this->builder->wheres as $field => $value) {
25-
$queryString .= "$field = :$field AND ";
26-
}
24+
$queryString .= $this->buildWheres();
2725

2826
$indexFields = implode(',', $this->modelService->getFullTextIndexFields());
2927

@@ -41,14 +39,9 @@ public function buildWhereRawString()
4139

4240
public function buildParams()
4341
{
44-
$params = [];
45-
46-
foreach ($this->builder->wheres as $field => $value) {
47-
$params[$field] = $value;
48-
}
4942

50-
$params['_search'] = $this->builder->query;
51-
return $params;
43+
$this->whereParams['_search'] = $this->builder->query;
44+
return $this->whereParams;
5245
}
5346

5447
public function isFullText()

0 commit comments

Comments
 (0)