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
|
|
|
* Return the event name that should be listened to for generating slugs. |
58
|
|
|
* |
59
|
|
|
* Can be one of: |
60
|
|
|
* - SluggableObserver::SAVING (to generate the slug before the model is saved) |
61
|
|
|
* - SluggableObserver::SAVED (to generate the slug after the model is saved) |
62
|
|
|
* |
63
|
|
|
* The second option is required if the primary key is to be part of the slug |
64
|
|
|
* source, as it won't be set during the "saving" event. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function sluggableEvent(): string |
69
|
|
|
{ |
70
|
|
|
return SluggableObserver::SAVED; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Query scope for finding "similar" slugs, used to determine uniqueness. |
75
|
|
|
* |
76
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
77
|
|
|
* @param string $attribute |
78
|
|
|
* @param array $config |
79
|
|
|
* @param string $slug |
80
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
81
|
|
|
*/ |
82
|
|
|
public function scopeFindSimilarSlugs(Builder $query, string $attribute, array $config, string $slug): Builder |
83
|
|
|
{ |
84
|
|
|
$separator = $config['separator']; |
85
|
|
|
|
86
|
|
|
return $query->where(function(Builder $q) use ($attribute, $slug, $separator) { |
87
|
|
|
$q->where($attribute, '=', $slug) |
88
|
|
|
->orWhere($attribute, 'LIKE', $slug . $separator . '%'); |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the sluggable configuration array for this model. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
abstract public function sluggable(): array; |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Optionally customize the cocur/slugify engine. |
102
|
|
|
* |
103
|
|
|
* @param \Cocur\Slugify\Slugify $engine |
104
|
|
|
* @param string $attribute |
105
|
|
|
* @return \Cocur\Slugify\Slugify |
106
|
|
|
*/ |
107
|
|
|
public function customizeSlugEngine(Slugify $engine, string $attribute): Slugify |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return $engine; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Optionally add constraints to the query that determines uniqueness. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
116
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
117
|
|
|
* @param string $attribute |
118
|
|
|
* @param array $config |
119
|
|
|
* @param string $slug |
120
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
121
|
|
|
*/ |
122
|
|
|
public function scopeWithUniqueSlugConstraints( |
123
|
|
|
Builder $query, |
124
|
|
|
Model $model, |
|
|
|
|
125
|
|
|
string $attribute, |
|
|
|
|
126
|
|
|
array $config, |
|
|
|
|
127
|
|
|
string $slug |
|
|
|
|
128
|
|
|
): Builder |
129
|
|
|
{ |
130
|
|
|
return $query; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.