Completed
Push — master ( 4b253c...0bc4e9 )
by Vasyl
01:52
created

SlugScopes::makeSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\SlugMaker\ServiceTraits;
4
5
trait SlugScopes
6
{
7
    /**
8
     * The scope for fet slug name.
9
     */
10
    public function getSlugName()
11
    {
12
        if ($this->slug) {
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Fomvasss\SlugMaker\ServiceTraits\SlugScopes. Did you maybe forget to declare it?
Loading history...
13
            return $this->slug->name;
14
        }
15
        return;
16
    }
17
18
    /**
19
     * @param $slug
20
     * @return mixed
21
     */
22
    public function makeSlug($slug)
23
    {
24
        return slug_make($this, $slug);
25
    }
26
27
    /**
28
     * Models by slugs.
29
     *
30
     * @param $query
31
     * @param string $slug
32
     * @return mixed
33
     */
34
    public function scopeBySlugs($query, $slug)
35
    {
36
        $slugs = is_array($slug) ? $slug : [$slug];
37
38
        return $query->whereHas('slug', function ($q) use ($slugs) {
39
            $q->whereIn('name', $slugs);
40
        });
41
    }
42
43
    /**
44
     * Find first model by slug.
45
     *
46
     * @param $query
47
     * @param string $slug
48
     * @return mixed
49
     */
50
    public function scopeFindBySlug($query, string $slug)
51
    {
52
        return $this->scopeBySlugs($query, $slug)->first();
53
    }
54
55
    /**
56
     * Find first model by slug or throw exciption.
57
     *
58
     * @param $query
59
     * @param string $slug
60
     * @return mixed
61
     */
62
    public function scopeFindOrFailBySlug($query, string $slug)
63
    {
64
        return $this->scopeBySlugs($query, $slug)->firstOrFail();
65
    }
66
67
    /**
68
     * Get models by slugs.
69
     *
70
     * @param $query
71
     * @param $slugs
72
     * @return mixed
73
     */
74
    public function scopeGetBySlugs($query, $slugs)
75
    {
76
        return $this->scopeBySlugs($query, $slugs)->get();
77
    }
78
79
    /**
80
     * Get array ids by slugs.
81
     *
82
     * @param $query
83
     * @param array $slugs
84
     * @return array
85
     */
86
    public function scopeGetArrayIdsBySlugs($query, array $slugs): array
87
    {
88
        return $this->scopeBySlugs($query, $slugs)->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
$slugs of type array is incompatible with the type string expected by parameter $slug of Fomvasss\SlugMaker\Servi...gScopes::scopeBySlugs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        return $this->scopeBySlugs($query, /** @scrutinizer ignore-type */ $slugs)->pluck('id')->toArray();
Loading history...
89
    }
90
}
91