Skip to content

Commit

Permalink
Refactor + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Jul 13, 2020
1 parent a6feb31 commit b0acbdf
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 141 deletions.
122 changes: 122 additions & 0 deletions src/ModelToSearchThrough.php
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);
}
}
73 changes: 0 additions & 73 deletions src/PendingQuery.php

This file was deleted.

3 changes: 3 additions & 0 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Search extends Facade
{
/**
* {@inheritdoc}
*/
protected static function getFacadeAccessor()
{
return 'laravel-cross-eloquent-search';
Expand Down
2 changes: 1 addition & 1 deletion src/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SearchFactory
use ForwardsCalls;

/**
* Handle dynamic method calls into the a new Searcher.
* Handle dynamic method calls into a new Searcher instance.
*
* @param string $method
* @param array $parameters
Expand Down
Loading

0 comments on commit b0acbdf

Please sign in to comment.