Passed
Push — master ( c5b742...46b7b3 )
by Andrey
05:36 queued 02:56
created

Search::toArray()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.3906

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 5
eloc 7
c 2
b 2
f 0
nc 4
nop 1
dl 0
loc 11
ccs 6
cts 8
cp 0.75
crap 5.3906
rs 9.6111
1
<?php
2
3
namespace Helldar\Roles\Support\Database;
4
5
use Helldar\Roles\Models\BaseModel;
6
use Helldar\Roles\Models\Permission;
7
use Helldar\Roles\Models\Role;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Collection;
12
13
class Search
14
{
15
    /**
16
     * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation  $builder
17
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $values
18
     *
19
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
20
     */
21 66
    public function by($builder, $values)
22
    {
23 66
        $value = $this->map($values);
24
25 66
        $nums  = $this->filterNum($value);
26 66
        $slugs = $this->filterSlugs($value);
27
28
        return $builder->where(function (Builder $builder) use ($nums, $slugs) {
29
            $builder
30 66
                ->whereIn('id', $nums)
31 66
                ->orWhereIn('slug', $slugs);
32 66
        });
33
    }
34
35
    /**
36
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $value
37
     *
38
     * @return int|string
39
     */
40 66
    public function getId($value)
41
    {
42 66
        return $value instanceof BaseModel
43
            ? $value->id
44 66
            : $value;
45
    }
46
47
    /**
48
     * @param  array|Arrayable|Collection|int|string  $values
49
     *
50
     * @return array
51
     */
52 66
    protected function map($values): array
53
    {
54
        return array_map(function ($value) {
55 66
            return $this->getId($value);
56 66
        }, $this->toArray($values));
57
    }
58
59 66
    protected function filterNum(array $values): array
60
    {
61
        return array_filter($values, function ($value) {
62 66
            return is_numeric($value);
63 66
        });
64
    }
65
66 66
    protected function filterSlugs(array $values): array
67
    {
68
        return array_filter($values, function ($value) {
69 66
            return ! is_numeric($value);
70 66
        });
71
    }
72
73 66
    protected function toArray($values): array
74
    {
75 66
        if ($this->isRole($values) || $this->isPermission($values)) {
76
            return [$values];
77 66
        } elseif ($this->isArrayable($values)) {
78
            $values = $values->toArray();
79 66
        } elseif (! $this->isArray($values)) {
80 54
            $values = Arr::wrap($values);
81
        }
82
83 66
        return Arr::flatten($values);
84
    }
85
86 66
    protected function isArray($values): bool
87
    {
88 66
        return is_array($values);
89
    }
90
91 66
    protected function isArrayable($values): bool
92
    {
93 66
        return $values instanceof Arrayable || $values instanceof Collection;
94
    }
95
96 66
    protected function isRole($value): bool
97
    {
98 66
        return $value instanceof Role;
99
    }
100
101 66
    protected function isPermission($value): bool
102
    {
103 66
        return $value instanceof Permission;
104
    }
105
}
106