-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduction of "Search" classes
- Loading branch information
Showing
5 changed files
with
272 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Titasgailius\SearchRelations\Contracts; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
interface Search | ||
{ | ||
/** | ||
* Apply search for the given relation. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $relation | ||
* @param string $search | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function apply(Builder $query, string $relation, string $search): Builder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Titasgailius\SearchRelations\Searches; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Titasgailius\SearchRelations\Contracts\Search; | ||
|
||
class ColumnSearch implements Search | ||
{ | ||
/** | ||
* Searchable columns. | ||
* | ||
* @var array | ||
*/ | ||
protected $columns; | ||
|
||
/** | ||
* Instantiate a new search query. | ||
* | ||
* @param array $columns | ||
*/ | ||
public function __construct(array $columns) | ||
{ | ||
$this->columns = $columns; | ||
} | ||
|
||
/** | ||
* Apply search for the given relation. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $relation | ||
* @param string $search | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function apply(Builder $query, string $relation, string $search): Builder | ||
{ | ||
return $query->where(function ($query) use ($search) { | ||
return $this->applySearchQuery($query, $search); | ||
}); | ||
} | ||
|
||
/** | ||
* Apply search query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $search | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
protected function applySearchQuery(Builder $query, string $search): Builder | ||
{ | ||
$model = $query->getModel(); | ||
$operator = $this->operator($query); | ||
|
||
foreach ($this->columns as $column) { | ||
$query->orWhere($model->qualifyColumn($column), $operator, '%'.$search.'%'); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* Get the like operator for the given query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @return string | ||
*/ | ||
protected function operator(Builder $query): string | ||
{ | ||
if ($query->getModel()->getConnection()->getDriverName() === 'pgsql') { | ||
return 'ILIKE'; | ||
} | ||
|
||
return 'LIKE'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Titasgailius\SearchRelations\Searches; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Titasgailius\SearchRelations\Contracts\Search; | ||
use Illuminate\Database\Eloquent\RelationNotFoundException; | ||
|
||
class RelationSearch implements Search | ||
{ | ||
/** | ||
* Searchable columns. | ||
* | ||
* @var array | ||
*/ | ||
protected $columns; | ||
|
||
/** | ||
* Instantiate a new search query instance. | ||
* | ||
* @param array $columns | ||
*/ | ||
public function __construct(array $columns) | ||
{ | ||
$this->columns = $columns; | ||
} | ||
|
||
/** | ||
* Apply search for the given relation. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $relation | ||
* @param string $search | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function apply(Builder $query, string $relation, string $search): Builder | ||
{ | ||
$this->ensureRelationshipExists($query, $relation); | ||
|
||
$query->orWhereHas($relation, function ($query) use ($relation, $search) { | ||
return $this->columnSearch()->apply($query, $relation, $search); | ||
}); | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* Ensure that the specified relationship exists. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $relation | ||
* @return void | ||
*/ | ||
protected function ensureRelationshipExists(Builder $query, string $relation) | ||
{ | ||
$query->getRelation($relation); | ||
} | ||
|
||
/** | ||
* Apply column search. | ||
* | ||
* @return \Titasgailius\SearchRelations\Contracts\Search | ||
*/ | ||
protected function columnSearch(): Search | ||
{ | ||
return new ColumnSearch($this->columns); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters