Skip to content

Commit b0acbdf

Browse files
committed
Refactor + docs
1 parent a6feb31 commit b0acbdf

File tree

7 files changed

+324
-141
lines changed

7 files changed

+324
-141
lines changed

src/ModelToSearchThrough.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Str;
9+
10+
class ModelToSearchThrough
11+
{
12+
/**
13+
* Builder to search through.
14+
*/
15+
private Builder $builder;
16+
17+
/**
18+
* The columns to search through.
19+
*/
20+
private Collection $columns;
21+
22+
/**
23+
* Order column.
24+
*/
25+
private string $orderByColumn;
26+
27+
/**
28+
* Unique key of this instance.
29+
*/
30+
private int $key;
31+
32+
/**
33+
* @param \Illuminate\Database\Eloquent\Builder $builder
34+
* @param \Illuminate\Support\Collection $columns
35+
* @param string $orderByColumn
36+
* @param integer $key
37+
*/
38+
public function __construct(Builder $builder, Collection $columns, string $orderByColumn, int $key)
39+
{
40+
$this->builder = $builder;
41+
$this->columns = $columns;
42+
$this->orderByColumn = $orderByColumn;
43+
$this->key = $key;
44+
}
45+
46+
/**
47+
* Get a cloned instance of the builder.
48+
*
49+
* @return \Illuminate\Database\Eloquent\Builder
50+
*/
51+
public function getFreshBuilder(): Builder
52+
{
53+
return clone $this->builder;
54+
}
55+
56+
/**
57+
* Get a collection with all qualified columns
58+
* to search through.
59+
*
60+
* @return \Illuminate\Support\Collection
61+
*/
62+
public function getQualifiedColumns(): Collection
63+
{
64+
return $this->columns->map(fn ($column) => $this->qualifyColumn($column));
65+
}
66+
67+
/**
68+
* Get the model instance being queried.
69+
*
70+
* @return \Illuminate\Database\Eloquent\Model
71+
*/
72+
private function getModel(): Model
73+
{
74+
return $this->builder->getModel();
75+
}
76+
77+
/**
78+
* Generates a key for the model with a suffix.
79+
*
80+
* @param string $suffix
81+
* @return string
82+
*/
83+
public function getModelKey($suffix = 'key'): string
84+
{
85+
return implode('_', [
86+
$this->key,
87+
Str::snake(class_basename($this->getModel())),
88+
$suffix,
89+
]);
90+
}
91+
92+
/**
93+
* Qualify a column by the model instance.
94+
*
95+
* @param string $column
96+
* @return string
97+
*/
98+
private function qualifyColumn(string $column): string
99+
{
100+
return $this->getModel()->qualifyColumn($column);
101+
}
102+
103+
/**
104+
* Get the qualified key name.
105+
*
106+
* @return string
107+
*/
108+
public function getQualifiedKeyName(): string
109+
{
110+
return $this->qualifyColumn($this->getModel()->getKeyName());
111+
}
112+
113+
/**
114+
* Get the qualified order name.
115+
*
116+
* @return string
117+
*/
118+
public function getQualifiedOrderByColumnName(): string
119+
{
120+
return $this->qualifyColumn($this->orderByColumn);
121+
}
122+
}

src/PendingQuery.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Search.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Search extends Facade
88
{
9+
/**
10+
* {@inheritdoc}
11+
*/
912
protected static function getFacadeAccessor()
1013
{
1114
return 'laravel-cross-eloquent-search';

src/SearchFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SearchFactory
99
use ForwardsCalls;
1010

1111
/**
12-
* Handle dynamic method calls into the a new Searcher.
12+
* Handle dynamic method calls into a new Searcher instance.
1313
*
1414
* @param string $method
1515
* @param array $parameters

0 commit comments

Comments
 (0)