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
|
|
|
abstract public function getSlugOptions(): SlugOptions; |
14
|
|
|
|
15
|
|
|
protected static function bootHasSlug() |
16
|
|
|
{ |
17
|
|
|
static::creating(function (Model $model) { |
18
|
|
|
$model->generateSlugOnCreate(); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
static::updating(function (Model $model) { |
22
|
|
|
$model->generateSlugOnUpdate(); |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function generateSlugOnCreate() |
27
|
|
|
{ |
28
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
29
|
|
|
|
30
|
|
|
if (! $this->slugOptions->generateSlugsOnCreate) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->addSlug(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function generateSlugOnUpdate() |
38
|
|
|
{ |
39
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
40
|
|
|
|
41
|
|
|
if (! $this->slugOptions->generateSlugsOnUpdate) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->addSlug(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function generateSlug() |
49
|
|
|
{ |
50
|
|
|
$this->slugOptions = $this->getSlugOptions(); |
51
|
|
|
|
52
|
|
|
$this->addSlug(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function addSlug() |
56
|
|
|
{ |
57
|
|
|
$this->ensureValidSlugOptions(); |
58
|
|
|
|
59
|
|
|
$slug = $this->generateNonUniqueSlug(); |
60
|
|
|
|
61
|
|
|
if ($this->slugOptions->generateUniqueSlugs) { |
62
|
|
|
$slug = $this->makeSlugUnique($slug); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$slugField = $this->slugOptions->slugField; |
66
|
|
|
|
67
|
|
|
$this->$slugField = $slug; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function generateNonUniqueSlug(): string |
71
|
|
|
{ |
72
|
|
|
$slugField = $this->slugOptions->slugField; |
73
|
|
|
|
74
|
|
|
if ($this->hasCustomSlugBeenUsed() && ! empty($this->$slugField)) { |
75
|
|
|
return $this->$slugField; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$slugSourceString = $this->getSlugSourceString(); |
79
|
|
|
|
80
|
|
|
if (!empty($this->slugOptions->prefixSlug) || !empty($this->slugOptions->suffixSlug)) { |
81
|
|
|
|
82
|
|
View Code Duplication |
if (!empty($this->slugOptions->prefixSlug)) { |
|
|
|
|
83
|
|
|
$slugSourceString = $this->slugOptions->prefixSlug . $this->slugOptions->slugSeparator . $slugSourceString; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
if (!empty($this->slugOptions->suffixSlug)) { |
|
|
|
|
87
|
|
|
$slugSourceString = $slugSourceString . $this->slugOptions->slugSeparator . $this->slugOptions->suffixSlug; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return Str::slug($slugSourceString, $this->slugOptions->slugSeparator, $this->slugOptions->slugLanguage); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function hasCustomSlugBeenUsed(): bool |
96
|
|
|
{ |
97
|
|
|
$slugField = $this->slugOptions->slugField; |
98
|
|
|
|
99
|
|
|
return $this->getOriginal($slugField) != $this->$slugField; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getSlugSourceString(): string |
103
|
|
|
{ |
104
|
|
|
if (is_callable($this->slugOptions->generateSlugFrom)) { |
105
|
|
|
$slugSourceString = call_user_func($this->slugOptions->generateSlugFrom, $this); |
106
|
|
|
|
107
|
|
|
return substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$slugSourceString = collect($this->slugOptions->generateSlugFrom) |
111
|
|
|
->map(function (string $fieldName) : string { |
112
|
|
|
return data_get($this, $fieldName, ''); |
113
|
|
|
}) |
114
|
|
|
->implode($this->slugOptions->slugSeparator); |
115
|
|
|
|
116
|
|
|
return substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function makeSlugUnique(string $slug): string |
120
|
|
|
{ |
121
|
|
|
$originalSlug = $slug; |
122
|
|
|
$i = 1; |
123
|
|
|
|
124
|
|
|
while ($this->otherRecordExistsWithSlug($slug) || $slug === '') { |
125
|
|
|
$slug = $originalSlug.$this->slugOptions->slugSeparator.$i++; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $slug; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function otherRecordExistsWithSlug(string $slug): bool |
132
|
|
|
{ |
133
|
|
|
$key = $this->getKey(); |
|
|
|
|
134
|
|
|
|
135
|
|
|
if ($this->incrementing) { |
|
|
|
|
136
|
|
|
$key = $key ?? '0'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$query = static::where($this->slugOptions->slugField, $slug) |
140
|
|
|
->where($this->getKeyName(), '!=', $key) |
|
|
|
|
141
|
|
|
->withoutGlobalScopes(); |
142
|
|
|
|
143
|
|
|
if ($this->usesSoftDeletes()) { |
144
|
|
|
$query->withTrashed(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $query->exists(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function usesSoftDeletes() |
151
|
|
|
{ |
152
|
|
|
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this))) { |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function ensureValidSlugOptions() |
160
|
|
|
{ |
161
|
|
|
if (is_array($this->slugOptions->generateSlugFrom) && ! count($this->slugOptions->generateSlugFrom)) { |
162
|
|
|
throw InvalidOption::missingFromField(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (! strlen($this->slugOptions->slugField)) { |
166
|
|
|
throw InvalidOption::missingSlugField(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($this->slugOptions->maximumLength <= 0) { |
170
|
|
|
throw InvalidOption::invalidMaximumLength(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.