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

SlugModelScopesTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArrayIdsByTypesByNames() 0 3 1
A scopeByTypesByNames() 0 10 4
A groupByType() 0 8 2
A getGropedArrayIdsByTypesByNames() 0 5 1
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
}