Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F4019750
D1629.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D1629.id.diff
View Options
diff --git a/src/Identifiers/Random.php b/src/Identifiers/Random.php
new file mode 100644
--- /dev/null
+++ b/src/Identifiers/Random.php
@@ -0,0 +1,119 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Identifiers;
+
+use Closure;
+use InvalidArgumentException;
+
+class Random {
+
+ /**
+ * @return string 32 random hexadecimal characters
+ */
+ public static function generateHexHash () : string {
+ return UUID::UUIDv4WithoutHyphens();
+ }
+
+ /**
+ * @param string $format A for letters, 1 for digits, e.g. AAA111
+ *
+ * @return string a random string based on the format e.g. ZCK530
+ */
+ public static function generateString (string $format) : string {
+ $randomString = "";
+
+ $len = strlen($format);
+ for ($i = 0 ; $i < $len ; $i++) {
+ $randomString .= self::generateCharacter($format[$i]);
+ }
+
+ return $randomString;
+ }
+
+ /**
+ * @param string $format A for letters, 1 for digits, e.g. A
+ *
+ * @return string a random string based on the format e.g. Z
+ */
+ public static function generateCharacter (string $format) : string {
+ return self::getPicker(self::normalizeFormat($format))();
+ }
+
+ ///
+ /// Helper methods for pickers
+ ///
+
+ public static function normalizeFormat (string $format) : string {
+ $normalizers = self::getNormalizers();
+
+ foreach ($normalizers as $normalizedFormat => $conditionClosure) {
+ if ($conditionClosure($format)) {
+ return (string)$normalizedFormat;
+ }
+ }
+
+ return $format;
+ }
+
+ private static function getNormalizers () : array {
+ /**
+ * <normalized format> => <method which returns true if format matches>
+ */
+
+ return [
+
+ 'A' => function ($format) : bool {
+ return ctype_upper($format);
+ },
+
+ 'a' => function ($format) : bool {
+ return ctype_lower($format);
+ },
+
+ '1' => function ($format) : bool {
+ return is_numeric($format);
+ },
+
+ ];
+ }
+
+ private static function getPickers () : array {
+ return [
+
+ 'A' => function () : string {
+ return Random::pickLetter();
+ },
+
+ 'a' => function () : string {
+ return strtolower(Random::pickLetter());
+ },
+
+ '1' => function () : string {
+ return (string)Random::pickDigit();
+ },
+
+ ];
+ }
+
+ public static function pickLetter () : string {
+ $asciiCode = 65 + mt_rand() % 26;
+
+ return chr($asciiCode);
+ }
+
+ public static function pickDigit (int $base = 10) : int {
+ return mt_rand() % $base;
+ }
+
+ private static function getPicker (string $format) : Closure {
+ $pickers = self::getPickers();
+
+ if (isset($pickers[$format])) {
+ return $pickers[$format];
+ }
+
+ throw new InvalidArgumentException();
+ }
+
+}
diff --git a/src/Identifiers/UUID.php b/src/Identifiers/UUID.php
--- a/src/Identifiers/UUID.php
+++ b/src/Identifiers/UUID.php
@@ -36,4 +36,8 @@
);
}
+ public static function UUIDv4WithoutHyphens () : string {
+ return str_replace("-", "", self::UUIDv4());
+ }
+
}
diff --git a/tests/Identifiers/RandomTest.php b/tests/Identifiers/RandomTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Identifiers/RandomTest.php
@@ -0,0 +1,46 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Identifiers;
+
+use Keruald\OmniTools\Identifiers\Random;
+use Phpunit\Framework\TestCase;
+
+class RandomTest extends TestCase {
+
+ public function testGenerateHexadecimalHash () : void {
+ $hash = Random::generateHexHash();
+
+ $this->assertEquals(
+ 32, strlen($hash),
+ "$hash size must be 32 characters"
+ );
+
+ $this->assertRegExp("/[0-9a-f]{32}/", $hash);
+ }
+
+ public function testHexadecimalHashesAreUnique() : void {
+ $this->assertNotEquals(
+ Random::generateHexHash(),
+ Random::generateHexHash()
+ );
+ }
+
+ /**
+ * @dataProvider provideRandomStringFormats
+ */
+ public function testRandomString($format, $re, $len) : void {
+ $string = Random::generateString($format);
+
+ $this->assertEquals($len, strlen($format));
+ $this->assertRegExp($re, $string);
+ }
+
+ public function provideRandomStringFormats() : iterable {
+ yield ["AAA111", "/^[A-Z]{3}[0-9]{3}$/", 6];
+ yield ["AAA123", "/^[A-Z]{3}[0-9]{3}$/", 6];
+ yield ["ABC123", "/^[A-Z]{3}[0-9]{3}$/", 6];
+ yield ["", "/^$/", 0];
+ }
+
+}
diff --git a/tests/Identifiers/UUIDTest.php b/tests/Identifiers/UUIDTest.php
--- a/tests/Identifiers/UUIDTest.php
+++ b/tests/Identifiers/UUIDTest.php
@@ -20,6 +20,18 @@
$this->assertRegExp($re, $uuid);
}
+ public function testUUIDv4WithoutHyphens () : void {
+ $uuid = UUID::UUIDv4WithoutHyphens();
+
+ $this->assertEquals(
+ 32, strlen($uuid),
+ "UUID size must be 36 characters, and there are 4 hyphens, so here 32 characters are expected."
+ );
+
+ $re = "/[0-9a-f]/";
+ $this->assertRegExp($re, $uuid);
+ }
+
public function testUUIDv4AreUnique () : void {
$this->assertNotEquals(UUID::UUIDv4(), UUID::UUIDv4());
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Jan 19, 18:05 (5 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2358690
Default Alt Text
D1629.id.diff (5 KB)
Attached To
Mode
D1629: Allow to generate a random sring
Attached
Detach File
Event Timeline
Log In to Comment