Passed
Push — master ( 214063...0a943e )
by Zing
01:46 queued 11s
created

WithSearchable::searchable()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 38
ccs 22
cts 22
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace Zing\QueryBuilder\Concerns;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
9
trait WithSearchable
10
{
11
    /**
12
     * @param string $field
13
     * @param array $results
14
     *
15
     * @return array
16
     */
17 1
    private function addNestedRelation($field, array $results)
18
    {
19 1
        [$relation, $property] = collect(explode('.', $field))
20
            ->pipe(function (Collection $parts) {
21
                return [
22 1
                    $parts->except(count($parts) - 1)->map([Str::class, 'camel'])->implode('.'),
23 1
                    $parts->last(),
24
                ];
25 1
            });
26
27 1
        $results[$relation][] = $property;
28
29 1
        return $results;
30
    }
31
32 2
    public function searchable($searchable)
33
    {
34 2
        $searchable = is_array($searchable) ? $searchable : func_get_args();
35 2
        $search = $this->request->input('search');
36 2
        if ($search) {
37 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

37
            $this->/** @scrutinizer ignore-call */ 
38
                   where(
Loading history...
38
                function (Builder $query) use ($search, $searchable) {
39 2
                    $results = [];
40 2
                    foreach ($searchable as $field) {
41 2
                        if (Str::contains($field, '.')) {
42 1
                            $results = $this->addNestedRelation($field, $results);
43
                        } else {
44 2
                            $results[] = $field;
45
                        }
46
                    }
47 2
                    foreach ($results as $key => $value) {
48 2
                        if (is_numeric($key)) {
49 1
                            $query->orWhere($value, 'like', "%{$search}%");
50
                        } else {
51 1
                            $query->orWhereHas(
52 1
                                $key,
53
                                function ($query) use ($value, $search) {
54
                                    /** @var \Illuminate\Database\Query\Builder $query */
55 1
                                    $query->where(
56
                                        function (Builder $query) use ($value, $search) {
57 1
                                            foreach ($value as $field) {
58 1
                                                $query->orWhere($field, 'like', "%{$search}%");
59
                                            }
60 1
                                        }
61
                                    );
62 2
                                }
63
                            );
64
                        }
65
                    }
66 2
                }
67
            );
68
        }//end if
69 2
        return $this;
70
    }
71
}
72