-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6feb31
commit b0acbdf
Showing
7 changed files
with
324 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ProtoneMedia\LaravelCrossEloquentSearch; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
|
||
class ModelToSearchThrough | ||
{ | ||
/** | ||
* Builder to search through. | ||
*/ | ||
private Builder $builder; | ||
|
||
/** | ||
* The columns to search through. | ||
*/ | ||
private Collection $columns; | ||
|
||
/** | ||
* Order column. | ||
*/ | ||
private string $orderByColumn; | ||
|
||
/** | ||
* Unique key of this instance. | ||
*/ | ||
private int $key; | ||
|
||
/** | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Support\Collection $columns | ||
* @param string $orderByColumn | ||
* @param integer $key | ||
*/ | ||
public function __construct(Builder $builder, Collection $columns, string $orderByColumn, int $key) | ||
{ | ||
$this->builder = $builder; | ||
$this->columns = $columns; | ||
$this->orderByColumn = $orderByColumn; | ||
$this->key = $key; | ||
} | ||
|
||
/** | ||
* Get a cloned instance of the builder. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function getFreshBuilder(): Builder | ||
{ | ||
return clone $this->builder; | ||
} | ||
|
||
/** | ||
* Get a collection with all qualified columns | ||
* to search through. | ||
* | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public function getQualifiedColumns(): Collection | ||
{ | ||
return $this->columns->map(fn ($column) => $this->qualifyColumn($column)); | ||
} | ||
|
||
/** | ||
* Get the model instance being queried. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Model | ||
*/ | ||
private function getModel(): Model | ||
{ | ||
return $this->builder->getModel(); | ||
} | ||
|
||
/** | ||
* Generates a key for the model with a suffix. | ||
* | ||
* @param string $suffix | ||
* @return string | ||
*/ | ||
public function getModelKey($suffix = 'key'): string | ||
{ | ||
return implode('_', [ | ||
$this->key, | ||
Str::snake(class_basename($this->getModel())), | ||
$suffix, | ||
]); | ||
} | ||
|
||
/** | ||
* Qualify a column by the model instance. | ||
* | ||
* @param string $column | ||
* @return string | ||
*/ | ||
private function qualifyColumn(string $column): string | ||
{ | ||
return $this->getModel()->qualifyColumn($column); | ||
} | ||
|
||
/** | ||
* Get the qualified key name. | ||
* | ||
* @return string | ||
*/ | ||
public function getQualifiedKeyName(): string | ||
{ | ||
return $this->qualifyColumn($this->getModel()->getKeyName()); | ||
} | ||
|
||
/** | ||
* Get the qualified order name. | ||
* | ||
* @return string | ||
*/ | ||
public function getQualifiedOrderByColumnName(): string | ||
{ | ||
return $this->qualifyColumn($this->orderByColumn); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.