<?php
// namespace AppBundle\Helper;
/**
* @author Bouchez Guillaume <guillaume.bouchez@infolegale.fr>
*/
class ShortIdGenerator
{
/*
* Version du générateur
*/
const GENERATOR_VERSION = '1';
/**
* Identifiant de version, prefix
*/
const GENERATOR_VERSION_PREFIX = 'a';
/**
* @return string
*/
public static function generate()
{
$microtime = microtime(true);
$numericId = intval(implode('', explode('.', $microtime)));
$ret = self::numToAlpha($numericId, self::GENERATOR_VERSION_PREFIX);
var_dump($numericId, $ret);
return $ret;
}
/**
* @see http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/
* @param mixed $in int, bigint input to translate
* @param mixed $pad_up Number or boolean pads the result up to a specified length
* @param string $pass_key Supplying a password makes it harder to calculate the original ID
*/
protected static function numToAlpha(
$in,
$prefix = '',
$pad_up = false,
$pass_key = null
) {
$out = '';
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base = strlen($index);
if ($pass_key !== null) {
for ($n = 0; $n < strlen($index); $n++) {
$i[] = substr($index, $n, 1);
}
$pass_hash = hash('sha256', $pass_key);
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash);
for ($n = 0; $n < strlen($index); $n++) {
$p[] = substr($pass_hash, $n, 1);
}
array_multisort($p, SORT_DESC, $i);
$index = implode($i);
}
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$in += pow($base, $pad_up);
}
}
for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
return $prefix.$out;
}
}
ShortIdGenerator::generate();
- Output for 7.2.0
- int(14993306960037)
string(9) "aepXRGcr5"
- Output for 7.1.10
- int(14993306960039)
string(9) "aepXRGcr7"
- Output for 7.1.7
- int(14993306960094)
string(9) "aepXRGcs0"
- Output for 7.1.6
- int(14993306960297)
string(9) "aepXRGcvH"
- Output for 7.1.5
- int(149933069602)
string(8) "acDEPSAk"
- Output for 7.1.4
- int(14993306960219)
string(9) "aepXRGcu1"
- Output for 7.1.3
- int(14993306960295)
string(9) "aepXRGcvF"
- Output for 7.1.2
- int(14993306960339)
string(9) "aepXRGcwx"
- Output for 7.1.0 - 7.1.1
- int(14993306960146)
string(9) "aepXRGctq"
- Output for 7.0.19 - 7.0.20
- int(14993306960048)
string(9) "aepXRGcrG"
- Output for 7.0.18
- int(14993306960208)
string(9) "aepXRGcuq"
- Output for 7.0.17
- int(14993306960051)
string(9) "aepXRGcrJ"
- Output for 7.0.16
- int(14993306960283)
string(9) "aepXRGcv3"
- Output for 7.0.15
- int(14993306960312)
string(9) "aepXRGcvW"
- Output for 7.0.14
- int(14993306960558)
string(9) "aepXRGczU"
- Output for 7.0.13
- int(14993306960172)
string(9) "aepXRGctG"
- Output for 7.0.12
- int(14993306960271)
string(9) "aepXRGcvr"
- Output for 7.0.11
- int(1499330696022)
string(8) "a0yAwNPE"
- Output for 7.0.10
- int(14993306960201)
string(9) "aepXRGcuj"
- Output for 7.0.9
- int(14993306960305)
string(9) "aepXRGcvP"
- Output for 7.0.8
- int(14993306960109)
string(9) "aepXRGcsF"
- Output for 7.0.7
- int(14993306960077)
string(9) "aepXRGcsj"
- Output for 7.0.6
- int(14993306960157)
string(9) "aepXRGct1"
- Output for 7.0.5
- int(14993306960069)
string(9) "aepXRGcsb"
- Output for 7.0.4
- int(14993306960096)
string(9) "aepXRGcs2"
- Output for 7.0.0, 7.0.3
- int(1499330696007)
string(8) "a0yAwNPz"
- Output for 7.0.2
- int(14993306960066)
string(9) "aepXRGcrY"
- Output for 7.0.1
- int(14993306960099)
string(9) "aepXRGcs5"
preferences:
77.77 ms | 401 KiB | 36 Q