1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Searchable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Auth\User; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
class Search |
9
|
|
|
{ |
10
|
|
|
protected $aspects = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string|\Spatie\Searchable\SearchAspect $searchAspect |
14
|
|
|
* |
15
|
|
|
* @return \Spatie\Searchable\Search |
16
|
|
|
*/ |
17
|
|
|
public function registerAspect($searchAspect): self |
18
|
|
|
{ |
19
|
|
|
if (is_string($searchAspect)) { |
20
|
|
|
$searchAspect = app($searchAspect); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->aspects[$searchAspect->getType()] = $searchAspect; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function registerModel(string $modelClass, ...$attributes): self |
29
|
|
|
{ |
30
|
|
|
if (isset($attributes[0]) && is_callable($attributes[0])) { |
31
|
|
|
$attributes = $attributes[0]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (is_array(Arr::get($attributes, 0))) { |
|
|
|
|
35
|
|
|
$attributes = $attributes[0]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$searchAspect = new ModelSearchAspect($modelClass, $attributes); |
39
|
|
|
|
40
|
|
|
$this->registerAspect($searchAspect); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getSearchAspects(): array |
46
|
|
|
{ |
47
|
|
|
return $this->aspects; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function search(string $query, ?User $user = null): SearchResultCollection |
51
|
|
|
{ |
52
|
|
|
return $this->perform($query, $user); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function perform(string $query, ?User $user = null): SearchResultCollection |
56
|
|
|
{ |
57
|
|
|
$searchResults = new SearchResultCollection(); |
58
|
|
|
|
59
|
|
|
collect($this->getSearchAspects()) |
60
|
|
|
->each(function (SearchAspect $aspect) use ($query, $user, $searchResults) { |
61
|
|
|
$searchResults->addResults($aspect->getType(), $aspect->getResults($query, $user)); |
|
|
|
|
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return $searchResults; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: