Completed
Push — master ( 762de8...ae630f )
by Colin
01:24
created

Sluggable::sluggable()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php namespace Cviebrock\EloquentSluggable;
2
3
use Cocur\Slugify\Slugify;
4
use Cviebrock\EloquentSluggable\Services\SlugService;
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Class Sluggable
10
 *
11
 * @package Cviebrock\EloquentSluggable
12
 */
13
trait Sluggable
14
{
15
16
    /**
17
     * Hook into the Eloquent model events to create or
18
     * update the slug as required.
19
     */
20
    public static function bootSluggable(): void
21
    {
22
        static::observe(app(SluggableObserver::class));
23
    }
24
25
    /**
26
     * Register a slugging model event with the dispatcher.
27
     *
28
     * @param \Closure|string $callback
29
     */
30
    public static function slugging($callback): void
31
    {
32
        static::registerModelEvent('slugging', $callback);
33
    }
34
35
    /**
36
     * Register a slugged model event with the dispatcher.
37
     *
38
     * @param \Closure|string $callback
39
     */
40
    public static function slugged($callback): void
41
    {
42
        static::registerModelEvent('slugged', $callback);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function replicate(array $except = null)
49
    {
50
        $instance = parent::replicate($except);
51
        (new SlugService())->slug($instance, true);
52
53
        return $instance;
54
    }
55
56
    /**
57
     * Query scope for finding "similar" slugs, used to determine uniqueness.
58
     *
59
     * @param \Illuminate\Database\Eloquent\Builder $query
60
     * @param string $attribute
61
     * @param array $config
62
     * @param string $slug
63
     * @return \Illuminate\Database\Eloquent\Builder
64
     */
65
    public function scopeFindSimilarSlugs(Builder $query, string $attribute, array $config, string $slug): Builder
66
    {
67
        $separator = $config['separator'];
68
69
        return $query->where(function(Builder $q) use ($attribute, $slug, $separator) {
70
            $q->where($attribute, '=', $slug)
71
                ->orWhere($attribute, 'LIKE', $slug . $separator . '%');
72
        });
73
    }
74
75
    /**
76
     * Return the sluggable configuration array for this model.
77
     *
78
     * @return array
79
     */
80
    abstract public function sluggable(): array;
81
82
83
    /**
84
     * Optionally customize the cocur/slugify engine.
85
     *
86
     * @param \Cocur\Slugify\Slugify $engine
87
     * @param string $attribute
88
     * @return \Cocur\Slugify\Slugify
89
     */
90
    public function customizeSlugEngine(Slugify $engine, string $attribute): Slugify
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        return $engine;
93
    }
94
95
    /**
96
     * Optionally add constraints to the query that determines uniqueness.
97
     *
98
     * @param \Illuminate\Database\Eloquent\Builder $query
99
     * @param \Illuminate\Database\Eloquent\Model $model
100
     * @param string $attribute
101
     * @param array $config
102
     * @param string $slug
103
     * @return \Illuminate\Database\Eloquent\Builder
104
     */
105
    public function scopeWithUniqueSlugConstraints(
106
        Builder $query,
107
        Model $model,
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
        string $attribute,
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
        array $config,
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
        string $slug
0 ignored issues
show
Unused Code introduced by
The parameter $slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    ): Builder
112
    {
113
        return $query;
114
    }
115
}
116