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
|
|
|
* Class Sluggable |
9
|
|
|
* |
10
|
|
|
* @package Cviebrock\EloquentSluggable |
11
|
|
|
*/ |
12
|
|
|
trait Sluggable |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Hook into the Eloquent model events to create or |
17
|
|
|
* update the slug as required. |
18
|
|
|
*/ |
19
|
|
|
public static function bootSluggable() |
20
|
|
|
{ |
21
|
|
|
static::observe(app(SluggableObserver::class)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Register a slugging model event with the dispatcher. |
26
|
|
|
* |
27
|
|
|
* @param \Closure|string $callback |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public static function slugging($callback) |
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
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public static function slugged($callback) |
42
|
|
|
{ |
43
|
|
|
static::registerModelEvent('slugged', $callback); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Clone the model into a new, non-existing instance. |
48
|
|
|
* |
49
|
|
|
* @param array|null $except |
50
|
|
|
* @return Model |
51
|
|
|
*/ |
52
|
|
|
public function replicate(array $except = null) |
53
|
|
|
{ |
54
|
|
|
$instance = parent::replicate($except); |
55
|
|
|
(new SlugService())->slug($instance, true); |
56
|
|
|
|
57
|
|
|
return $instance; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Query scope for finding "similar" slugs, used to determine uniqueness. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
64
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
65
|
|
|
* @param string $attribute |
66
|
|
|
* @param array $config |
67
|
|
|
* @param string $slug |
68
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
69
|
|
|
*/ |
70
|
|
|
public function scopeFindSimilarSlugs(Builder $query, Model $model, $attribute, $config, $slug) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$separator = $config['separator']; |
73
|
|
|
|
74
|
|
|
return $query->where($attribute, '=', $slug) |
75
|
|
|
->orWhere($attribute, 'LIKE', $slug . $separator . '%'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the sluggable configuration array for this model. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
abstract public function sluggable(); |
84
|
|
|
} |
85
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.