Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3750895
D2277.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D2277.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D2277: Allow to create an arbitrary length ASCII identifier
Attached
Detach File
Event Timeline
Log In to Comment