Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 2, 2021
1 parent e895d67 commit 9944485
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(?string $keyword, array $fields = [])
*/
public function searchKeyword(): Value\Keyword
{
return $this->keyword;
return new Value\Keyword($this->keyword);
}

/**
Expand All @@ -65,25 +65,21 @@ public function apply($query)
return $field instanceof Contracts\Search;
});

$query
->when($filters->isNotEmpty(), function ($query) use ($filters, $likeOperator) {
foreach ($filters as $filter) {
$filter->apply(
$query,
$this->keyword->all(
$this->wildcardCharacter,
$this->wildcardReplacement,
$this->wildcardSearching ?? true
),
$likeOperator
);
}
})
->where(function ($query) use ($fields, $likeOperator) {
foreach ($fields as $field) {
$this->queryOnColumn($query, Value\Field::make($field), $likeOperator);
}
});
$query->where(function ($query) use ($fields, $filters, $likeOperator) {
$keywords = $this->searchKeyword()->all(
$this->wildcardCharacter,
$this->wildcardReplacement,
$this->wildcardSearching ?? true
);

foreach ($filters as $filter) {
$filter->apply($query, $keywords, $likeOperator);
}

foreach ($fields as $field) {
$this->queryOnColumn($query, Value\Field::make($field), $likeOperator);
}
});

return $query;
}
Expand Down Expand Up @@ -127,7 +123,7 @@ protected function queryOnColumnUsing(

return (new Filters\FieldSearch())->field($field)->apply(
$query,
$this->keyword->all(
$this->searchKeyword()->all(
$this->wildcardCharacter,
$this->wildcardReplacement,
$field->wildcardSearching ?? $this->wildcardSearching ?? true
Expand All @@ -154,7 +150,7 @@ protected function queryOnJsonColumnUsing(
->field($field)
->apply(
$query,
$this->keyword->allLowerCased(
$this->searchKeyword()->allLowerCased(
$this->wildcardCharacter,
$this->wildcardReplacement,
$field->wildcardSearching ?? $this->wildcardSearching ?? true
Expand All @@ -176,7 +172,7 @@ protected function queryOnColumnUsingRelation(
->field($field)
->apply(
$query,
$this->keyword->all(
$this->searchKeyword()->all(
$this->wildcardCharacter,
$this->wildcardReplacement,
$field->wildcardSearching ?? $this->wildcardSearching ?? true
Expand Down
8 changes: 8 additions & 0 deletions src/Value/Keyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public function validate(): bool
return ! empty($this->value);
}

/**
* Get keyword value.
*/
public function getValue(): string
{
return $this->value;
}

/**
* Get searchable strings as lowercase.
*/
Expand Down

0 comments on commit 9944485

Please sign in to comment.