1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Searchable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Auth\User; |
6
|
|
|
|
7
|
|
|
class Search |
8
|
|
|
{ |
9
|
|
|
protected $aspects = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string|\Spatie\Searchable\SearchAspect $searchAspect |
13
|
|
|
* |
14
|
|
|
* @return \Spatie\Searchable\Search |
15
|
|
|
*/ |
16
|
|
|
public function registerAspect($searchAspect): self |
17
|
|
|
{ |
18
|
|
|
if (is_string($searchAspect)) { |
19
|
|
|
$searchAspect = app($searchAspect); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$this->aspects[$searchAspect->getType()] = $searchAspect; |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function registerModel(string $modelClass, ...$attributes): self |
28
|
|
|
{ |
29
|
|
|
if (isset($attributes[0]) && is_callable($attributes[0])) { |
30
|
|
|
$attributes = $attributes[0]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (is_array(array_get($attributes, 0))) { |
|
|
|
|
34
|
|
|
$attributes = $attributes[0]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$searchAspect = new ModelSearchAspect($modelClass, $attributes); |
38
|
|
|
|
39
|
|
|
$this->registerAspect($searchAspect); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getSearchAspects(): array |
45
|
|
|
{ |
46
|
|
|
return $this->aspects; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function search(string $query, ?User $user = null): SearchResultCollection |
50
|
|
|
{ |
51
|
|
|
return $this->perform($query, $user); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function perform(string $query, ?User $user = null): SearchResultCollection |
55
|
|
|
{ |
56
|
|
|
$searchResults = new SearchResultCollection(); |
57
|
|
|
|
58
|
|
|
collect($this->getSearchAspects()) |
59
|
|
|
->each(function (SearchAspect $aspect) use ($query, $user, $searchResults) { |
60
|
|
|
$searchResults->addResults($aspect->getType(), $aspect->getResults($query, $user)); |
|
|
|
|
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
return $searchResults; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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: