Completed
Pull Request — master (#260)
by Colin
06:42
created

Sluggable::replicate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Cviebrock\EloquentSluggable;
2
3
use Cviebrock\EloquentSluggable\Services\SlugService;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
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()
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
     * @return void
30
     */
31
    public static function slugging($callback)
32
    {
33
        static::registerModelEvent('slugging', $callback);
34
    }
35
36
    /**
37
     * Register a slugged model event with the dispatcher.
38
     *
39
     * @param \Closure|string $callback
40
     * @return void
41
     */
42
    public static function slugged($callback)
43
    {
44
        static::registerModelEvent('slugged', $callback);
45
    }
46
47
    /**
48
     * Clone the model into a new, non-existing instance.
49
     *
50
     * @param  array|null $except
51
     * @return Model
52
     */
53
    public function replicate(array $except = null)
54
    {
55
        $instance = parent::replicate($except);
56
        (new SlugService())->slug($instance, true);
57
58
        return $instance;
59
    }
60
61
    /**
62
     * Query scope for finding "similar" slugs, used to determine uniqueness.
63
     *
64
     * @param \Illuminate\Database\Eloquent\Builder $query
65
     * @param \Illuminate\Database\Eloquent\Model $model
66
     * @param string $attribute
67
     * @param array $config
68
     * @param string $slug
69
     * @return \Illuminate\Database\Eloquent\Builder
70
     */
71
    public function scopeFindSimilarSlugs(Builder $query, Model $model, $attribute, $config, $slug)
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...
72
    {
73
        $separator = $config['separator'];
74
75
        return $query->where($attribute, '=', $slug)
76
            ->orWhere($attribute, 'LIKE', $slug . $separator . '%');
77
    }
78
79
    /**
80
     * Return the sluggable configuration array for this model.
81
     *
82
     * @return array
83
     */
84
    abstract public function sluggable();
85
}
86