Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1.      */
  2.     public function handle(NewCategory $event)
  3.     {
  4.         foreach ($event as $product) {
  5.             $text = $product->name;
  6.             // replace non letter or digits by -
  7.             $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  8.  
  9.             // trim
  10.             $text = trim($text, '-');
  11.  
  12.             // transliterate
  13.             $cyr  = ['а','б','в','г','д','е','ж','з','и','ѝ','й','к','л','м','н','о','п','р','с','т','у',
  14.                 'ф','х','ц','ч','ш','щ','ъ','ь', 'ю','я','А','Б','В','Г','Д','Е','Ж',
  15.                 'З','И','Ѝ','Й','К','Л','М','Н','О','П','Р','С','Т','У',
  16.                 'Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь', 'Ю', 'Я'];
  17.             $lat = ['a','b','v','g','d','e','zh','z','i','i','y','k','l','m','n','o','p','r','s','t','u',
  18.                 'f' ,'h' ,'ts' ,'ch','sh' ,'sht' ,'a' ,'y' ,'yu' ,'ya','A','B','V','G','D','E','Zh',
  19.                 'Z','I','I','Y','K','L','M','N','O','P','R','S','T','U',
  20.                 'F' ,'H' ,'Ts' ,'Ch','Sh' ,'Sht' ,'A' ,'Y' ,'Yu' , 'Ya'];
  21.             $text = str_replace($cyr, $lat, $text);
  22.  
  23.             // lowercase
  24.             $text = strtolower($text);
  25.  
  26.             // remove unwanted characters
  27.             $text = preg_replace('~[^-\w]+~', '', $text);
  28.  
  29.             if (empty($text)) {
  30.                 return 'n-a';
  31.             }
  32.             return Product::where('name', $product->name)->update(['slug' => $text]);
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement