From 13f09cc6629a140a34c5db768e9072a01d4e2907 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Sat, 13 Feb 2021 11:19:40 -0600 Subject: [PATCH] add helper method to generate a simple random string --- src/Support/Str.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Support/Str.php b/src/Support/Str.php index aa135647..bdab0577 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -9,6 +9,25 @@ namespace Pest\Support; */ final class Str { + /** + * Pool of alpha-numeric characters for generating (unsafe) random strings + * from. + */ + private const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + + /** + * Create a (unsecure & non-cryptographically safe) random alpha-numeric + * string value. + * + * @param int $length The length of the resulting randomized string. + * @return string + * @see https://github.com/laravel/framework/blob/4.2/src/Illuminate/Support/Str.php#L240-L242 + */ + public static function random(int $length = 16): string + { + return substr(str_shuffle(str_repeat(self::POOL, 5)), 0, $length); + } + /** * Checks if the given `$target` starts with the given `$search`. */