Skip to content

Commit 566bb23

Browse files
committed
Fixed Slugable
1 parent 960a9ca commit 566bb23

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/Traits/HasSlugable.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,57 @@
88
trait HasSlugable
99
{
1010
/**
11-
* Boot the trait Slugable.
12-
*
13-
* @return void
14-
*/
11+
* Boot the trait HasSlugable.
12+
**/
1513
public static function bootHasSlugable(): void
1614
{
1715
static::saving(function (Model $model) {
1816
$source = $model->slugSourceField ?? 'title';
1917
$destination = $model->slugDestinationField ?? 'slug';
2018

2119
if (empty($model->$destination) && !empty($model->$source)) {
22-
$model->$destination = static::makeMultilingualSlug($model->$source);
20+
$slug = static::generateSlugFrom($model->$source);
21+
$model->$destination = static::makeSlugUnique($model, $slug, $destination);
2322
}
2423
});
2524
}
2625

2726
/**
2827
* Supported From Persian and Arabic to translate slug
29-
*
30-
* @param string $string
31-
* @return string
32-
*/
33-
protected static function makeMultilingualSlug(string $string): string
28+
**/
29+
30+
protected static function generateSlugFrom(string $value): string
3431
{
35-
$transliterationMap = [
32+
$value = static::convertToAscii($value);
33+
return Str::slug($value);
34+
}
3635

37-
// Persian and Arabic
36+
protected static function convertToAscii(string $string): string
37+
{
38+
$map = [
39+
'ا' => 'a', 'أ' => 'a', 'آ' => 'a', 'إ' => 'e', 'ب' => 'b', 'پ' => 'p', 'ت' => 't', 'ث' => 'th', 'ج' => 'j', 'چ' => 'ch',
40+
'ح' => 'h', 'خ' => 'kh', 'د' => 'd', 'ذ' => 'dh', 'ر' => 'r', 'ز' => 'z', 'ژ' => 'zh', 'س' => 's', 'ش' => 'sh',
41+
'ص' => 's', 'ض' => 'z', 'ط' => 't', 'ظ' => 'z', 'ع' => 'a', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'gh', 'ک' => 'k',
42+
'گ' => 'g', 'ل' => 'l', 'م' => 'm', 'ن' => 'n', 'و' => 'v', 'ه' => 'h', 'ی' => 'y', 'ي' => 'y', 'ئ' => 'y', 'ة' => 'h',
43+
'' => '-',
3844

39-
'آ' => 'a', 'ا' => 'a', 'ب' => 'b', 'پ' => 'p', 'ت' => 't', 'ث' => 's', 'ج' => 'j',
40-
'چ' => 'ch', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd', 'ذ' => 'z', 'ر' => 'r', 'ز' => 'z',
41-
'ژ' => 'zh', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'z', 'ط' => 't', 'ظ' => 'z',
42-
'ع' => 'a', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'gh', 'ک' => 'k', 'گ' => 'g', 'ل' => 'l',
43-
'م' => 'm', 'ن' => 'n', 'و' => 'v', 'ه' => 'h', 'ی' => 'y', 'ء' => '', 'ئ' => 'y',
44-
'ؤ' => 'v', 'ة' => 'h', 'ي' => 'y',
45+
'۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4', '۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
46+
'٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4', '٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9'
4547
];
4648

47-
$string = preg_replace('/[\p{Mn}\p{Pd}\p{Zs}]/u', ' ', $string);
49+
return strtr($string, $map);
50+
}
4851

49-
$slug = str_replace(array_keys($transliterationMap), array_values($transliterationMap), $string);
52+
protected static function makeSlugUnique(Model $model, string $slug, string $column): string
53+
{
54+
$original = $slug;
55+
$i = 1;
5056

51-
if (function_exists('transliterator_transliterate')) {
52-
$slug = transliterator_transliterate('Any-Latin; Latin-ASCII', $slug);
57+
while ($model->newQuery()->where($column, $slug)->exists()) {
58+
$slug = $original . '-' . $i++;
5359
}
5460

55-
return Str::slug($slug);
61+
return $slug;
5662
}
63+
5764
}

0 commit comments

Comments
 (0)