[TASK] Add random string method

This commit is contained in:
Philipp Dieter 2021-03-04 10:56:21 +01:00
parent b9542e5e1b
commit a3cb4325bf

View File

@ -45,4 +45,23 @@ class StringUtility
return $string;
}
function getRandomString(
int $length = 64,
string $keyspace = null
): string {
if (!$keyspace) {
$keyspace = '0123456789'
. 'abcdefghijklmnopqrstuvwxyz'
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
if ($length < 1) {
throw new \RangeException("Length must be a positive integer");
}
$pieces = [];
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$pieces []= $keyspace[random_int(0, $max)];
}
return implode('', $pieces);
}
}