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