1 | <?php |
||||
2 | /** |
||||
3 | * Created by enea dhack - 29/12/2019 12:46. |
||||
4 | */ |
||||
5 | |||||
6 | namespace Vaened\Searcher; |
||||
7 | |||||
8 | use Closure; |
||||
9 | use Illuminate\Database\Eloquent\Builder; |
||||
10 | use Illuminate\Support\Collection; |
||||
11 | |||||
12 | abstract class Queryable |
||||
13 | { |
||||
14 | private ?Query $query = null; |
||||
15 | |||||
16 | abstract protected function builder(): Builder; |
||||
17 | |||||
18 | protected function apply(Constraint $constraint): void |
||||
19 | { |
||||
20 | $this->getQuery()->push($constraint); |
||||
21 | } |
||||
22 | |||||
23 | protected function removeNamedBinding(string $name): void |
||||
24 | { |
||||
25 | $this->getQuery()->attach(fn(Builder $builder) => $builder->withoutGlobalScope($name)); |
||||
26 | } |
||||
27 | |||||
28 | protected function getQuery(): Query |
||||
29 | { |
||||
30 | return $this->query ??= $this->createQueryBuilder(); |
||||
31 | } |
||||
32 | |||||
33 | private function createQueryBuilder(): Query |
||||
34 | { |
||||
35 | $bindings = $this->getBootableTraitsBindings(); |
||||
36 | return new Query($this->builder(), $bindings->merge($this->defaultBindings())->toArray()); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
37 | } |
||||
38 | |||||
39 | private function getBootableTraitsBindings(): Collection |
||||
40 | { |
||||
41 | $methods = $this->filterBootableTraits(static::class, fn(string $name) => "get{$name}DefaultBindings"); |
||||
42 | return collect($methods)->map(fn(string $method): array => call_user_func([$this, $method]))->flatten(); |
||||
0 ignored issues
–
show
$methods of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
43 | } |
||||
44 | |||||
45 | private function filterBootableTraits(string $class, Closure $buildFunctionName): array |
||||
46 | { |
||||
47 | $booted = []; |
||||
48 | |||||
49 | foreach (class_uses_recursive($class) as $trait) { |
||||
50 | $method = $buildFunctionName(class_basename($trait)); |
||||
51 | if (method_exists($class, $method) && ! in_array($method, $booted)) { |
||||
52 | $booted[] = $method; |
||||
53 | } |
||||
54 | } |
||||
55 | |||||
56 | return $booted; |
||||
57 | } |
||||
58 | |||||
59 | protected function defaultBindings(): array |
||||
60 | { |
||||
61 | return []; |
||||
62 | } |
||||
63 | } |
||||
64 |