]> Raphaƫl G. Git Repositories - userbundle/blobdiff - Utils/Slugger.php
Use Transliterator from root namespace
[userbundle] / Utils / Slugger.php
index 8a2856dbede56b0b19e63fa9ef92906455531af5..8422f2c5715d20aa31b9ce7e764722704151242a 100644 (file)
@@ -23,7 +23,7 @@ class Slugger {
        //Retrieve secret and set offset from reduction
        public function __construct(ContainerInterface $container) {
                //Set secret
-               $this->secret = $container->getParameter('kernel.secret');
+               $this->secret = $_SERVER['APP_SECRET'] ?? $container->getParameter('kernel.secret');
 
                //Pseudo-random alphabet
                //XXX: use array flip and keys to workaround php "smart" that cast range('0', '9') as int instead of string
@@ -104,6 +104,11 @@ class Slugger {
 
        //Convert string to safe slug
        function slug(string $string): string {
+               //Use Transliterator if available
+               if (class_exists('Transliterator')) {
+                       $trans = \Transliterator::create('Any-Latin; Latin-ASCII; Lower()');
+                       return preg_replace(['/[^a-zA-Z0-9]+/', '/(^-+|-+$)/'], ['-', ''], $trans->transliterate($string));
+               }
                return preg_replace('/[\/_|+ -]+/', '-', strtolower(trim(preg_replace('/[^a-zA-Z0-9\/_|+ -]/', '', str_replace(['\'', '"'], ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $string))), '-')));
        }
 }