3 namespace Rapsys\UserBundle\Utils
; 
   5 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  17         //The alpha array key number 
  20         //The offset reduced from secret 
  23         //Retrieve secret and set offset from reduction 
  24         public function __construct(ContainerInterface 
$container) { 
  26                 $this->secret 
= $_SERVER['APP_SECRET'] ?? $container->getParameter('kernel.secret'); 
  28                 //Pseudo-random alphabet 
  29                 //XXX: use array flip and keys to workaround php "smart" that cast range('0', '9') as int instead of string 
  30                 //XXX: The key count mismatch, count(alpha)>count(rev), resulted in a data corruption due to duplicate numeric values 
  31                 //TODO: set this as a parameter generated once in a command ? 
  32                 $this->alpha 
= array_keys(array_flip(array_merge( 
  59                 $this->count = count($rev = $this->rev = array_flip($this->alpha)); 
  62                 $split = str_split($this->secret); 
  65                 $this->offset = array_reduce($split, function ($res, $a) use ($rev) { return $res += $rev[$a]; }, count($split)) % $this->count; 
  69         public function short(string $string): string { 
  73                 //Iterate on each character 
  74                 foreach(str_split($string) as $k => $c) { 
  75                         if (isset($this->rev[$c]) && isset($this->alpha[($this->rev[$c]+$this->offset)%$this->count])) { 
  76                                 //XXX: Remap char to an other one 
  77                                 $ret .= chr(($this->rev[$c] - $this->offset + $this->count) % $this->count); 
  82                 return str_replace(['+
','/'], ['-','_
'], base64_encode($ret)); 
  86         public function unshort(string $string): string { 
  90                 //Iterate on each character 
  91                 foreach(str_split(base64_decode(str_replace(['-','_
'], ['+
','/'], $string))) as $c) { 
  92                         //XXX: Reverse map char to an other one 
  93                         $ret .= $this->alpha[(ord($c) + $this->offset) % $this->count]; 
 100         //Crypt and base64uri encode string 
 101         public function hash(string $string): string { 
 102                 return str_replace(['+
','/'], ['-','_
'], base64_encode(crypt($string, $this->secret))); 
 105         //Convert string to safe slug 
 106         function slug(string $string): string { 
 107                 //Use Transliterator if available 
 108                 if (class_exists('Transliterator
')) { 
 109                         $trans = Transliterator::create('Any
-Latin
; Latin
-ASCII
; Lower()'); 
 110                         return preg_replace(['/[^a
-zA
-Z0
-9]+
/', '/(^
-+
|-+$
)/'], ['-', ''], $trans->transliterate($string)); 
 112                 return preg_replace('/[\
/_
|+ -]+
/', '-', strtolower(trim(preg_replace('/[^a
-zA
-Z0
-9\
/_
|+ -]/', '', str_replace(['\'
', '"'], ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $string))), '-')));