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

SlugModelScopesTrait::groupByType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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
}