|
8 | 8 | trait HasSlugable |
9 | 9 | { |
10 | 10 | /** |
11 | | - * Boot the trait Slugable. |
12 | | - * |
13 | | - * @return void |
14 | | - */ |
| 11 | + * Boot the trait HasSlugable. |
| 12 | + **/ |
15 | 13 | public static function bootHasSlugable(): void |
16 | 14 | { |
17 | 15 | static::saving(function (Model $model) { |
18 | 16 | $source = $model->slugSourceField ?? 'title'; |
19 | 17 | $destination = $model->slugDestinationField ?? 'slug'; |
20 | 18 |
|
21 | 19 | 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); |
23 | 22 | } |
24 | 23 | }); |
25 | 24 | } |
26 | 25 |
|
27 | 26 | /** |
28 | 27 | * 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 |
34 | 31 | { |
35 | | - $transliterationMap = [ |
| 32 | + $value = static::convertToAscii($value); |
| 33 | + return Str::slug($value); |
| 34 | + } |
36 | 35 |
|
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 | + '' => '-', |
38 | 44 |
|
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' |
45 | 47 | ]; |
46 | 48 |
|
47 | | - $string = preg_replace('/[\p{Mn}\p{Pd}\p{Zs}]/u', ' ', $string); |
| 49 | + return strtr($string, $map); |
| 50 | + } |
48 | 51 |
|
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; |
50 | 56 |
|
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++; |
53 | 59 | } |
54 | 60 |
|
55 | | - return Str::slug($slug); |
| 61 | + return $slug; |
56 | 62 | } |
| 63 | + |
57 | 64 | } |
0 commit comments