[TASK] add RandomStringUtility

This commit is contained in:
Philipp Dieter 2020-10-05 00:56:52 +02:00
parent c9bd34eb23
commit 8f24164f03

View File

@ -0,0 +1,32 @@
<?php
namespace Cjel\TemplatesAide\Utility;
/***
*
* This file is part of the "" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2019 Philipp Dieter <philipp@glanzstueck.agency>, Glanzstück GmbH
*
***/
class RandomStringUtility
{
public static function getToken(
int $length = 64,
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
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);
}
}