Page MenuHomeDevCentral

D2277.diff
No OneTemporary

D2277.diff

diff --git a/src/Identifiers/Random.php b/src/Identifiers/Random.php
--- a/src/Identifiers/Random.php
+++ b/src/Identifiers/Random.php
@@ -6,6 +6,8 @@
use Closure;
use InvalidArgumentException;
+use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
+
class Random {
/**
@@ -40,6 +42,13 @@
return self::getPicker(self::normalizeFormat($format))();
}
+
+ public static function generateIdentifier (int $bytes_count) : string {
+ $bytes = random_bytes($bytes_count);
+
+ return StringUtilities::encodeInBase64($bytes);
+ }
+
///
/// Helper methods for pickers
///
diff --git a/tests/Identifiers/RandomTest.php b/tests/Identifiers/RandomTest.php
--- a/tests/Identifiers/RandomTest.php
+++ b/tests/Identifiers/RandomTest.php
@@ -40,6 +40,13 @@
$this->assertRegExp($re, $string);
}
+ public function testGenerateIdentifier() : void {
+ $identifier = Random::generateIdentifier(20);
+
+ $this->assertEquals(27, strlen($identifier));
+ $this->assertRegExp("/^[A-Z0-9\-_]*$/i", $identifier);
+ }
+
///
/// Data providers
///

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 04:31 (20 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250246
Default Alt Text
D2277.diff (1 KB)

Event Timeline