From 6c6b33caccb9cecb99bc56045d0139c60f851a33 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 2 Dec 2024 15:54:35 +0900 Subject: [PATCH] add uuidv4 verify method --- src/Convert/Color/Coordinates/HSB.php | 2 +- src/Convert/Color/Coordinates/HSL.php | 2 +- src/Create/Uids.php | 34 ++++++++----------- .../phpunit/Create/CoreLibsCreateUidsTest.php | 18 ++++++---- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Convert/Color/Coordinates/HSB.php b/src/Convert/Color/Coordinates/HSB.php index 1b15bd9..09bd57e 100644 --- a/src/Convert/Color/Coordinates/HSB.php +++ b/src/Convert/Color/Coordinates/HSB.php @@ -26,7 +26,7 @@ class HSB implements Interface\CoordinatesInterface private float $B = 0.0; /** @var string color space: either ok or cie */ - private string $colorspace = ''; + private string $colorspace = ''; /** @phpstan-ignore-line */ /** * HSB (HSV) color coordinates diff --git a/src/Convert/Color/Coordinates/HSL.php b/src/Convert/Color/Coordinates/HSL.php index 1adbe5c..5fbcf29 100644 --- a/src/Convert/Color/Coordinates/HSL.php +++ b/src/Convert/Color/Coordinates/HSL.php @@ -25,7 +25,7 @@ class HSL implements Interface\CoordinatesInterface /** @var float lightness (luminance) */ private float $L = 0.0; - /** @var string color space: either ok or cie */ + /** @var string color space: either sRGB */ private string $colorspace = ''; /** diff --git a/src/Create/Uids.php b/src/Create/Uids.php index 4233979..09bdc86 100644 --- a/src/Create/Uids.php +++ b/src/Create/Uids.php @@ -56,26 +56,6 @@ class Uids */ public static function uuidv4(): string { - /* return sprintf( - '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - // 32 bits for "time_low" - mt_rand(0, 0xffff), - mt_rand(0, 0xffff), - // 16 bits for "time_mid" - mt_rand(0, 0xffff), - // 16 bits for "time_hi_and_version", - // four most significant bits holds version number 4 - mt_rand(0, 0x0fff) | 0x4000, - // 16 bits, 8 bits for "clk_seq_hi_res", - // 8 bits for "clk_seq_low", - // two most significant bits holds zero and one for variant DCE1.1 - mt_rand(0, 0x3fff) | 0x8000, - // 48 bits for "node" - mt_rand(0, 0xffff), - mt_rand(0, 0xffff), - mt_rand(0, 0xffff) - ); */ - $data = random_bytes(16); assert(strlen($data) == 16); @@ -93,6 +73,20 @@ class Uids return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } + /** + * regex validate uuid v4 + * + * @param string $uuidv4 + * @return bool + */ + public static function validateUuuidv4(string $uuidv4): bool + { + if (!preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/", $uuidv4)) { + return false; + } + return true; + } + /** * creates a uniq id based on lengths * diff --git a/test/phpunit/Create/CoreLibsCreateUidsTest.php b/test/phpunit/Create/CoreLibsCreateUidsTest.php index 3612ee8..308d964 100644 --- a/test/phpunit/Create/CoreLibsCreateUidsTest.php +++ b/test/phpunit/Create/CoreLibsCreateUidsTest.php @@ -121,6 +121,7 @@ final class CoreLibsCreateUidsTest extends TestCase * must match 7e78fe0d-59b8-4637-af7f-e88d221a7d1e * * @covers ::uuidv4 + * @covers ::validateUuidv4 * @testdox uuidv4 check that return is matching regex [$_dataName] * * @return void @@ -129,13 +130,18 @@ final class CoreLibsCreateUidsTest extends TestCase { $uuid = \CoreLibs\Create\Uids::uuidv4(); $this->assertMatchesRegularExpression( - '/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/', - $uuid + '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', + $uuid, + 'Failed regex check' + ); + $this->assertTrue( + \CoreLibs\Create\Uids::validateUuuidv4($uuid), + 'Failed validate regex method' + ); + $this->assertFalse( + \CoreLibs\Create\Uids::validateUuuidv4('not-a-uuidv4'), + 'Failed wrong uuid validated as true' ); - // $this->assertStringMatchesFormat( - // '%4s%4s-%4s-%4s-%4s-%4s%4s%4s', - // $uuid - // ); } /**