1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Sluggable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
trait HasSlug |
9
|
|
|
{ |
10
|
|
|
/** @var \Spatie\Sluggable\SlugOptions */ |
11
|
|
|
protected $slugOptions; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get the options for generating the slug. |
15
|
|
|
*/ |
16
|
|
|
abstract public function getSlugOptions(): SlugOptions; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Boot the trait. |
20
|
|
|
*/ |
21
|
|
|
protected static function bootHasSlug() |
22
|
|
|
{ |
23
|
|
|
static::creating(function (Model $model) { |
24
|
|
|
$model->generateSlugOnCreate(); |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
static::updating(function (Model $model) { |
28
|
|
|
$model->generateSlugOnUpdate(); |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handle adding slug on model creation. |
34
|
|
|
*/ |
35
|
|
|
protected function generateSlugOnCreate() |
36
|
|
|
{ |
37
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
38
|
|
|
|
39
|
|
|
if (! $this->slugOptions->generateSlugsOnCreate) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->addSlug(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Handle adding slug on model update. |
48
|
|
|
*/ |
49
|
|
|
protected function generateSlugOnUpdate() |
50
|
|
|
{ |
51
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
52
|
|
|
|
53
|
|
|
if (! $this->slugOptions->generateSlugsOnUpdate) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->addSlug(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Handle setting slug on explicit request. |
62
|
|
|
*/ |
63
|
|
|
public function generateSlug() |
64
|
|
|
{ |
65
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
66
|
|
|
|
67
|
|
|
$this->addSlug(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add the slug to the model. |
72
|
|
|
*/ |
73
|
|
|
protected function addSlug() |
74
|
|
|
{ |
75
|
|
|
$this->guardAgainstInvalidSlugOptions(); |
76
|
|
|
|
77
|
|
|
$slug = $this->generateNonUniqueSlug(); |
78
|
|
|
|
79
|
|
|
if ($this->slugOptions->generateUniqueSlugs) { |
80
|
|
|
$slug = $this->makeSlugUnique($slug); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$slugField = $this->slugOptions->slugField; |
84
|
|
|
|
85
|
|
|
$this->$slugField = $slug; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Generate a non unique slug for this record. |
90
|
|
|
*/ |
91
|
|
|
protected function generateNonUniqueSlug(): string |
92
|
|
|
{ |
93
|
|
|
if ($this->hasCustomSlugBeenUsed()) { |
94
|
|
|
$slugField = $this->slugOptions->slugField; |
95
|
|
|
|
96
|
|
|
return $this->$slugField; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return Str::slug($this->getSlugSourceString(), $this->slugOptions->slugSeparator, $this->slugOptions->slugLanguage); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Determine if a custom slug has been saved. |
104
|
|
|
*/ |
105
|
|
|
protected function hasCustomSlugBeenUsed(): bool |
106
|
|
|
{ |
107
|
|
|
$slugField = $this->slugOptions->slugField; |
108
|
|
|
|
109
|
|
|
return $this->getOriginal($slugField) != $this->$slugField; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the string that should be used as base for the slug. |
114
|
|
|
*/ |
115
|
|
|
protected function getSlugSourceString(): string |
116
|
|
|
{ |
117
|
|
|
if (is_callable($this->slugOptions->generateSlugFrom)) { |
118
|
|
|
$slugSourceString = call_user_func($this->slugOptions->generateSlugFrom, $this); |
119
|
|
|
|
120
|
|
|
return substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$slugSourceString = collect($this->slugOptions->generateSlugFrom) |
124
|
|
|
->map(function (string $fieldName) : string { |
125
|
|
|
return $this->relationships($fieldName) ?? $this->$fieldName ?? ''; |
126
|
|
|
}) |
127
|
|
|
->implode($this->slugOptions->slugSeparator); |
128
|
|
|
|
129
|
|
|
return substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function relationships(string $fieldNames) |
133
|
|
|
{ |
134
|
|
|
if (strpos($fieldNames, '.') === false) { |
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$value = $this; |
139
|
|
|
foreach (explode('.', $fieldNames) as $fieldName) { |
140
|
|
|
$value = $value->$fieldName; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Make the given slug unique. |
148
|
|
|
*/ |
149
|
|
|
protected function makeSlugUnique(string $slug): string |
150
|
|
|
{ |
151
|
|
|
$originalSlug = $slug; |
152
|
|
|
$i = 1; |
153
|
|
|
|
154
|
|
|
while ($this->otherRecordExistsWithSlug($slug) || $slug === '') { |
155
|
|
|
$slug = $originalSlug.$this->slugOptions->slugSeparator.$i++; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $slug; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Determine if a record exists with the given slug. |
163
|
|
|
*/ |
164
|
|
|
protected function otherRecordExistsWithSlug(string $slug): bool |
165
|
|
|
{ |
166
|
|
|
$key = $this->getKey(); |
|
|
|
|
167
|
|
|
|
168
|
|
|
if ($this->incrementing) { |
|
|
|
|
169
|
|
|
$key = $key ?? '0'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return (bool) static::where($this->slugOptions->slugField, $slug) |
173
|
|
|
->where($this->getKeyName(), '!=', $key) |
|
|
|
|
174
|
|
|
->withoutGlobalScopes() |
175
|
|
|
->first(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* This function will throw an exception when any of the options is missing or invalid. |
180
|
|
|
*/ |
181
|
|
|
protected function guardAgainstInvalidSlugOptions() |
182
|
|
|
{ |
183
|
|
|
if (is_array($this->slugOptions->generateSlugFrom) && ! count($this->slugOptions->generateSlugFrom)) { |
184
|
|
|
throw InvalidOption::missingFromField(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (! strlen($this->slugOptions->slugField)) { |
188
|
|
|
throw InvalidOption::missingSlugField(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($this->slugOptions->maximumLength <= 0) { |
192
|
|
|
throw InvalidOption::invalidMaximumLength(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.