X-Git-Url: https://git.rapsys.eu/userbundle/blobdiff_plain/9fc280d8e9c1d65b9cddda1ce1644fc03322ffb8..bbdd05e4ae572d5a4fe7d676e2cc361387220f74:/Utils/Slugger.php diff --git a/Utils/Slugger.php b/Utils/Slugger.php index 2172515..e781fa5 100644 --- a/Utils/Slugger.php +++ b/Utils/Slugger.php @@ -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 @@ -66,7 +66,7 @@ class Slugger { } //Short the string - public function short($string) { + public function short(string $string): string { //Return string $ret = ''; @@ -83,7 +83,7 @@ class Slugger { } //Unshort the string - public function unshort($string) { + public function unshort(string $string): string { //Return string $ret = ''; @@ -98,12 +98,17 @@ class Slugger { } //Crypt and base64uri encode string - public function hash($string) { + public function hash(string $string): string { return str_replace(['+','/'], ['-','_'], base64_encode(crypt($string, $this->secret))); } //Convert string to safe slug - function slug($string) { + 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))), '-'))); } }