Completed
Push — master ( 605762...f098ed )
by Vasyl
02:13
created

SlugModelScopesTrait::scopeByTypesByNames()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 2
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\SlugMaker\Models;
4
5
trait SlugModelScopesTrait
6
{
7
    public function scopeByTypesByNames($query, array $attributes)
8
    {
9
        foreach ($attributes as $type => $slugs) {
10
            if (! empty($slugs)) {
11
                $names = is_array($slugs) ? $slugs : [$slugs];
12
                $query->orWhere('slugable_type', $type)->whereIn('name', $names);
13
            }
14
        }
15
16
        return $query;
17
    }
18
19
    public static function getArrayIdsByTypesByNames(array $attributes): array
20
    {
21
        return self::byTypesByNames($attributes)->pluck('slugable_id')->toArray();
22
    }
23
24
    public static function getGropedArrayIdsByTypesByNames(array $attributes): array
25
    {
26
        $slugs = self::byTypesByNames($attributes)->pluck('slugable_type', 'slugable_id');
27
28
        return self::groupByType($slugs);
29
    }
30
31
    private static function groupByType($slugs): array
32
    {
33
        $res = [];
34
        foreach ($slugs as $id => $type) {
35
            $res[$type][] = $id;
36
        }
37
38
        return $res;
39
    }
40
}