Passed
Push — master ( 7c54d7...41926f )
by Zing
05:15
created

WithSearchable::addNestedRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Concerns;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Str;
9
10
trait WithSearchable
11
{
12
    use NestedRelation;
13
14
    /**
15
     * @param string|array<string> $searchable
16
     *
17
     * @return $this
18
     */
19 4
    public function searchable($searchable)
20
    {
21 4
        $searchable = is_array($searchable) ? $searchable : func_get_args();
22 4
        $search = $this->request->input('search');
23 4
        if ($search === null) {
24 1
            return $this;
25
        }
26
27 3
        if (is_string($search) && trim($search) === '') {
28 1
            return $this;
29
        }
30
31 2
        $searchable = $this->resolveNestedSearchable($searchable);
32
33 2
        return $this->applySearchable($search, $searchable);
34
    }
35
36
    /**
37
     * @param mixed $search
38
     * @param array<int|string, string|array<string>> $searchable
39
     *
40
     * @return $this
41
     */
42 2
    protected function applySearchable($search, array $searchable = [])
43
    {
44 2
        $this->where(
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $this->/** @scrutinizer ignore-call */ 
45
               where(
Loading history...
45 2
            function (Builder $query) use ($search, $searchable): void {
46 2
                collect($searchable)->each(
47 2
                    function ($value, $key) use ($query, $search): void {
48 2
                        if (is_numeric($key)) {
49 1
                            $query->orWhere($value, 'like', sprintf('%%%s%%', $search));
50
51 1
                            return;
52
                        }
53
54 1
                        $this->applyRelationSearchable($query, $key, (array) $value, $search);
55 2
                    }
56
                );
57 2
            }
58
        );
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * @param array<string> $fields
65
     * @param mixed $search
66
     */
67 1
    protected function applyRelationSearchable(Builder $query, string $relation, array $fields, $search): Builder
68
    {
69 1
        return $query->orWhereHas(
70 1
            $relation,
71 1
            function (Builder $query) use ($fields, $search): void {
72 1
                $query->where(
73 1
                    function (Builder $query) use ($fields, $search): void {
74 1
                        foreach ($fields as $field) {
75 1
                            $query->orWhere($field, 'like', sprintf('%%%s%%', $search));
76
                        }
77 1
                    }
78
                );
79 1
            }
80
        );
81
    }
82
83
    /**
84
     * @param array<string> $searchable
85
     *
86
     * @return array<int|string, string|array<string>>
87
     */
88 2
    private function resolveNestedSearchable(array $searchable)
89
    {
90 2
        $results = [];
91 2
        foreach ($searchable as $singleSearchable) {
92 2
            if (Str::contains($singleSearchable, '.')) {
93 1
                [$relation, $property] = $this->resolveNestedRelation($singleSearchable);
94
95 1
                $results[$relation][] = $property;
96
            } else {
97 1
                $results[] = $singleSearchable;
98
            }
99
        }
100
101 2
        return $results;
102
    }
103
}
104