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) |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.