From 8f24164f031c6a80b16cad64f30fdaada205a7d7 Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Mon, 5 Oct 2020 00:56:52 +0200 Subject: [PATCH] [TASK] add RandomStringUtility --- Classes/Utility/RandomStringUtility.php | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Classes/Utility/RandomStringUtility.php diff --git a/Classes/Utility/RandomStringUtility.php b/Classes/Utility/RandomStringUtility.php new file mode 100644 index 0000000..5a6d4b0 --- /dev/null +++ b/Classes/Utility/RandomStringUtility.php @@ -0,0 +1,32 @@ +, 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); + } + +}