Passed
Pull Request — master (#119)
by Zing
05:24
created

WithSearchable::applySearchable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 1
nop 2
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 2
rs 9.9666
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<int, non-empty-string>|array<non-empty-string, array<non-empty-string>> $searchable
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, non-empty-str...rray<non-empty-string>> at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array<int, non-empty-string>|array<non-empty-string, array<non-empty-string>>.
Loading history...
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<non-empty-string> $fields
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string>.
Loading history...
65
     * @param non-empty-string $relation
66
     * @param mixed $search
67
     */
68 1
    protected function applyRelationSearchable(Builder $query, string $relation, array $fields, $search): Builder
69
    {
70 1
        return $query->orWhereHas(
71 1
            $relation,
72 1
            function (Builder $query) use ($fields, $search): void {
73 1
                $query->where(
74 1
                    function (Builder $query) use ($fields, $search): void {
75 1
                        foreach ($fields as $field) {
76 1
                            $query->orWhere($field, 'like', sprintf('%%%s%%', $search));
77
                        }
78 1
                    }
79
                );
80 1
            }
81
        );
82
    }
83
84
    /**
85
     * @param array<string> $searchable
86
     *
87
     * @return array<int, non-empty-string>|array<non-empty-string, array<non-empty-string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, non-empty-str...rray<non-empty-string>> at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array<int, non-empty-string>|array<non-empty-string, array<non-empty-string>>.
Loading history...
88
     */
89 2
    private function resolveNestedSearchable(array $searchable)
90
    {
91 2
        $results = [];
92 2
        foreach ($searchable as $singleSearchable) {
93 2
            if (Str::contains($singleSearchable, '.')) {
94 1
                [$relation, $property] = $this->resolveNestedRelation($singleSearchable);
95
96 1
                $results[$relation][] = $property;
97
            } else {
98 1
                $results[] = $singleSearchable;
99
            }
100
        }
101
102 2
        return $results;
103
    }
104
}
105