|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fomvasss\SlugMaker; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
trait HasSlugMaker |
|
8
|
|
|
{ |
|
9
|
|
|
protected $slugOptions; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Get the options for generating the slug. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract public function getSlugMakerOptions(): SlugMakerOptions; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The morph relations (has one) for the model. |
|
18
|
|
|
* |
|
19
|
|
|
* @return mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
public function slug() |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->morphOne(\Fomvasss\SlugMaker\Models\Slug::class, 'slugable'); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The boot trait events in class (model). |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static function bootSlugMakerTrait() |
|
30
|
|
|
{ |
|
31
|
|
|
static::created(function (Model $model) { |
|
32
|
|
|
$model->generateSlugOnCreate(); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
static::updated(function (Model $model) { |
|
36
|
|
|
$model->generateSlugOnUpdate(); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
static::deleted(function (Model $model) { |
|
40
|
|
|
$model->removeSlugsOnDeleted(); |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handle adding slug on model creation. |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
protected function generateSlugOnCreate() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->slugOptions = $this->getSlugMakerOptions(); |
|
50
|
|
|
|
|
51
|
|
|
if (! $this->slugOptions->generateSlugsOnCreate) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$slug = $this->getSlug(); |
|
56
|
|
|
$this->storeSlug($slug); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Handle adding slug on model update. |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
protected function generateSlugOnUpdate() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$this->slugOptions = $this->getSlugMakerOptions(); |
|
65
|
|
|
|
|
66
|
|
|
if (! $this->slugOptions->generateSlugsOnUpdate) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$slug = $this->getSlug(); |
|
71
|
|
|
$this->storeSlug($slug); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The store (create or update) slug in database. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $slug |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function storeSlug($slug) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->slug) { |
|
|
|
|
|
|
82
|
|
|
//$this->slug()->updateOrCreate(['name' => $this->slug->name], [$this->slugField => $slug]); |
|
83
|
|
|
$this->slug()->update(['name' => $slug]); |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->slug()->create(['name' => $slug]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Remove the slug in database. Default migration not using softdelete. |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function removeSlugsOnDeleted() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->slugOptions = $this->getSlugMakerOptions(); |
|
95
|
|
|
|
|
96
|
|
|
if ($this->slug && $this->slugOptions->removeSlugsOnDelete) { |
|
|
|
|
|
|
97
|
|
|
$this->slug()->delete(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the slug with the model. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getSlug() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->guardAgainstInvalidSlugOptions(); |
|
107
|
|
|
|
|
108
|
|
|
$slug = $this->generateNonUniqueSlug(); |
|
109
|
|
|
|
|
110
|
|
|
if ($this->slugOptions->generateUniqueSlugs) { |
|
111
|
|
|
$slug = $this->makeSlugUnique($slug); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $slug; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Generate a non unique slug for this record. |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function generateNonUniqueSlug(): string |
|
121
|
|
|
{ |
|
122
|
|
|
$this->slugOptions = $this->getSlugMakerOptions(); |
|
123
|
|
|
|
|
124
|
|
|
return str_slug($this->getSlugSourceString(), $this->slugOptions->slugSeparator); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get the string that should be used as base for the slug. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getSlugSourceString(): string |
|
131
|
|
|
{ |
|
132
|
|
|
if (is_callable($this->slugOptions->generateSlugFrom)) { |
|
133
|
|
|
$slugSourceString = call_user_func($this->slugOptions->generateSlugFrom, $this); |
|
134
|
|
|
|
|
135
|
|
|
return $this->getClippedSlugWithPrefixSuffix($slugSourceString); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$slugSourceString = collect($this->slugOptions->generateSlugFrom) |
|
139
|
|
|
->map(function (string $fieldName) : string { |
|
140
|
|
|
return $this->$fieldName ?? ''; |
|
141
|
|
|
}) |
|
142
|
|
|
->implode('-'); |
|
143
|
|
|
|
|
144
|
|
|
return $this->getClippedSlugWithPrefixSuffix($slugSourceString); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Add prefix and suffix to slug. Clipped string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getClippedSlugWithPrefixSuffix(string $slugSourceString): string |
|
151
|
|
|
{ |
|
152
|
|
|
$prefix = $this->slugOptions->slugPrefix; |
|
153
|
|
|
$suffix = $this->slugOptions->slugSuffix; |
|
154
|
|
|
$lengthPrefix = strlen($prefix); |
|
155
|
|
|
$lengthSuffix = strlen($suffix); |
|
156
|
|
|
|
|
157
|
|
|
if ($lengthPrefix + $lengthSuffix) { |
|
158
|
|
|
$limitWithoutPrefixSuffix = $this->slugOptions->maximumLength - (++$lengthPrefix + ++$lengthSuffix); |
|
159
|
|
|
|
|
160
|
|
|
if ($limitWithoutPrefixSuffix < 1) { |
|
161
|
|
|
return str_limit($prefix.' '.$suffix, $this->slugOptions->maximumLength); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $prefix.' '.str_limit($slugSourceString, $limitWithoutPrefixSuffix, '').' '.$suffix; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return str_limit($slugSourceString, $this->slugOptions->maximumLength); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Make the given slug unique. |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function makeSlugUnique(string $slug): string |
|
174
|
|
|
{ |
|
175
|
|
|
$originalSlug = $slug; |
|
176
|
|
|
$i = 1; |
|
177
|
|
|
while ($this->otherRecordExistsWithSlug($slug) || $slug === '') { |
|
178
|
|
|
$slug = $originalSlug.'-'.$i++; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $slug; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Determine if a record exists with the given slug. |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function otherRecordExistsWithSlug(string $slug): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return (bool) \Fomvasss\SlugMaker\Models\Slug::where('name', $slug) |
|
190
|
|
|
->withoutGlobalScopes() |
|
191
|
|
|
->first(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* This function will throw an exception when any of the options is missing or invalid. |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function guardAgainstInvalidSlugOptions() |
|
198
|
|
|
{ |
|
199
|
|
|
if (! count($this->slugOptions->generateSlugFrom)) { |
|
200
|
|
|
throw InvalidOption::missingFromField(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
//if (! strlen($this->slugField)) { |
|
204
|
|
|
// throw InvalidOption::missingSlugField(); |
|
205
|
|
|
//} |
|
206
|
|
|
|
|
207
|
|
|
if ($this->slugOptions->maximumLength <= 0) { |
|
208
|
|
|
throw InvalidOption::invalidMaximumLength(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* The scope for fet slug name. |
|
214
|
|
|
*/ |
|
215
|
|
|
public function scopeGetSlugName() |
|
216
|
|
|
{ |
|
217
|
|
|
if ($this->slug) { |
|
|
|
|
|
|
218
|
|
|
return $this->slug->name; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return null; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Models by slugs. |
|
226
|
|
|
* |
|
227
|
|
|
* @param $query |
|
228
|
|
|
* @param string $slug |
|
229
|
|
|
* @return mixed |
|
230
|
|
|
*/ |
|
231
|
|
|
public function scopeBySlugs($query, $slug) |
|
232
|
|
|
{ |
|
233
|
|
|
$slugs = is_array($slug) ? $slug : [$slug]; |
|
234
|
|
|
|
|
235
|
|
|
return $query->whereHas('slug', function ($q) use ($slugs) { |
|
236
|
|
|
$q->whereIn('name', $slugs); |
|
237
|
|
|
}); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Find first model by slug. |
|
242
|
|
|
* |
|
243
|
|
|
* @param $query |
|
244
|
|
|
* @param string $slug |
|
245
|
|
|
* @return mixed |
|
246
|
|
|
*/ |
|
247
|
|
|
public function scopeFindBySlug($query, string $slug) |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->scopeBySlugs($query, $slug)->first(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Find first model by slug or throw exciption. |
|
254
|
|
|
* |
|
255
|
|
|
* @param $query |
|
256
|
|
|
* @param string $slug |
|
257
|
|
|
* @return mixed |
|
258
|
|
|
*/ |
|
259
|
|
|
public function scopeFindOrFailBySlug($query, string $slug) |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->scopeBySlugs($query, $slug)->firstOrFail(); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Get models by slugs. |
|
266
|
|
|
* |
|
267
|
|
|
* @param $query |
|
268
|
|
|
* @param $slugs |
|
269
|
|
|
* @return mixed |
|
270
|
|
|
*/ |
|
271
|
|
|
public function scopeGetBySlugs($query, $slugs) |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->scopeBySlugs($query, $slugs)->get(); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Get array ids by slugs. |
|
278
|
|
|
* |
|
279
|
|
|
* @param $query |
|
280
|
|
|
* @param array $slugs |
|
281
|
|
|
* @return array |
|
282
|
|
|
*/ |
|
283
|
|
|
public function scopeGetArrayIdsBySlugs($query, array $slugs): array |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->scopeBySlugs($query, $slugs)->pluck('id')->toArray(); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths