1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Spatie\QueryBuilder\Searches\SearchesEnds; |
8
|
|
|
use Spatie\QueryBuilder\Searches\SearchesExact; |
9
|
|
|
use Spatie\QueryBuilder\Searches\SearchesSplit; |
10
|
|
|
use Spatie\QueryBuilder\Searches\SearchesBegins; |
11
|
|
|
use Spatie\QueryBuilder\Searches\SearchesPartial; |
12
|
|
|
use Spatie\QueryBuilder\Searches\SearchesSplitEnds; |
13
|
|
|
use Spatie\QueryBuilder\Searches\SearchesSplitBegins; |
14
|
|
|
use Spatie\QueryBuilder\Searches\Search as CustomSearch; |
15
|
|
|
use Spatie\QueryBuilder\Searches\SearchesModifierResolver; |
16
|
|
|
|
17
|
|
|
class Search |
18
|
|
|
{ |
19
|
|
|
/** @var string|\Spatie\QueryBuilder\Searches\Search */ |
20
|
|
|
protected $searchClass; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $property; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $columnName; |
27
|
|
|
|
28
|
|
|
/** @var Collection */ |
29
|
|
|
protected $ignored; |
30
|
|
|
|
31
|
|
|
public function __construct(string $property, $searchClass, ?string $columnName = null) |
32
|
|
|
{ |
33
|
|
|
$this->property = $property; |
34
|
|
|
|
35
|
|
|
$this->searchClass = $searchClass; |
36
|
|
|
|
37
|
|
|
$this->ignored = Collection::make(); |
38
|
|
|
|
39
|
|
|
$this->columnName = $columnName ?? $property; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function search(Builder $builder, $value, $modifier = null) |
43
|
|
|
{ |
44
|
|
|
$valueToSearch = $this->resolveValueForSearching($value); |
45
|
|
|
|
46
|
|
|
if (is_null($valueToSearch)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$searchClass = $this->resolveSearchClass(); |
51
|
|
|
|
52
|
|
|
($searchClass)($builder, $valueToSearch, $this->columnName, $modifier); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function begins(string $property, ?string $columnName = null): self |
56
|
|
|
{ |
57
|
|
|
return new static($property, SearchesBegins::class, $columnName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function ends(string $property, ?string $columnName = null): self |
61
|
|
|
{ |
62
|
|
|
return new static($property, SearchesEnds::class, $columnName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function exact(string $property, ?string $columnName = null): self |
66
|
|
|
{ |
67
|
|
|
return new static($property, SearchesExact::class, $columnName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function partial(string $property, $columnName = null): self |
71
|
|
|
{ |
72
|
|
|
return new static($property, SearchesPartial::class, $columnName); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function split(string $property, $columnName = null): self |
76
|
|
|
{ |
77
|
|
|
return new static($property, SearchesSplit::class, $columnName); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function splitBegins(string $property, ?string $columnName = null): self |
81
|
|
|
{ |
82
|
|
|
return new static($property, SearchesSplitBegins::class, $columnName); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function splitEnds(string $property, ?string $columnName = null): self |
86
|
|
|
{ |
87
|
|
|
return new static($property, SearchesSplitEnds::class, $columnName); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function resolver(string $property, $columnName = null): self |
91
|
|
|
{ |
92
|
|
|
return new static($property, SearchesModifierResolver::class, $columnName); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function custom(string $property, $searchClass, $columnName = null): self |
96
|
|
|
{ |
97
|
|
|
return new static($property, $searchClass, $columnName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getProperty(): string |
101
|
|
|
{ |
102
|
|
|
return $this->property; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isForProperty(string $property): bool |
106
|
|
|
{ |
107
|
|
|
return $this->property === $property; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function ignore(...$values): self |
111
|
|
|
{ |
112
|
|
|
$this->ignored = $this->ignored |
113
|
|
|
->merge($values) |
114
|
|
|
->flatten(); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getIgnored(): array |
120
|
|
|
{ |
121
|
|
|
return $this->ignored->toArray(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getColumnName(): string |
125
|
|
|
{ |
126
|
|
|
return $this->columnName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function resolveSearchClass(): CustomSearch |
130
|
|
|
{ |
131
|
|
|
if ($this->searchClass instanceof CustomSearch) { |
132
|
|
|
return $this->searchClass; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new $this->searchClass; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function resolveValueForSearching($property) |
139
|
|
|
{ |
140
|
|
|
if (is_array($property)) { |
141
|
|
|
$remainingProperties = array_diff($property, $this->ignored->toArray()); |
142
|
|
|
|
143
|
|
|
return ! empty($remainingProperties) ? $remainingProperties : null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return ! $this->ignored->contains($property) ? $property : null; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|