Passed
Pull Request — master (#116)
by Zing
09:46 queued 04:37
created

WithSearchable::applySearchable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 2
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 2
rs 10
c 0
b 0
f 0
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<array<string>|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) {
48 2
                        if (is_numeric($key)) {
49 1
                            return $query->orWhere($value, 'like', sprintf('%%%s%%', $search));
50
                        }
51
52 1
                        return $this->applyRelationSearchable($query, $key, (array) $value, $search);
53 2
                    }
54
                );
55 2
            }
56
        );
57
58 2
        return $this;
59
    }
60
61
    /**
62
     * @param array<string> $fields
63
     * @param mixed $search
64
     */
65 1
    protected function applyRelationSearchable(Builder $query, string $relation, array $fields, $search): Builder
66
    {
67 1
        return $query->orWhereHas(
68 1
            $relation,
69 1
            function (Builder $query) use ($fields, $search): void {
70 1
                $query->where(
71 1
                    function (Builder $query) use ($fields, $search): void {
72 1
                        foreach ($fields as $field) {
73 1
                            $query->orWhere($field, 'like', sprintf('%%%s%%', $search));
74
                        }
75 1
                    }
76
                );
77 1
            }
78
        );
79
    }
80
81
    /**
82
     * @param array<string> $searchable
83
     *
84
     * @return array<array<string>|string>
85
     */
86 2
    private function resolveNestedSearchable(array $searchable)
87
    {
88 2
        $results = [];
89 2
        foreach ($searchable as $singleSearchable) {
90 2
            if (Str::contains($singleSearchable, '.')) {
91 1
                [$relation, $property] = $this->resolveNestedRelation($singleSearchable);
92
93 1
                $results[$relation][] = $property;
94
            } else {
95 1
                $results[] = $singleSearchable;
96
            }
97
        }
98
99 2
        return $results;
100
    }
101
}
102