From 621d2e51a8c36b5a76439d146901cd64639fa5a4 Mon Sep 17 00:00:00 2001 From: Cerys Date: Sun, 1 Dec 2024 16:06:23 +0000 Subject: [PATCH] more work on the UUID class --- src/DataStructures/UUID.php | 14 +++++++++----- tests/UUIDTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 tests/UUIDTest.php diff --git a/src/DataStructures/UUID.php b/src/DataStructures/UUID.php index 13453c3..5b92a96 100644 --- a/src/DataStructures/UUID.php +++ b/src/DataStructures/UUID.php @@ -8,17 +8,21 @@ use Exception; class UUID { private string $UUID; - public static string $ValidationRegex = "/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/"; + + public static string $UUIDValidationRegex = "/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/"; + public static string $DeegraphUUIDValidationRegex = "/^\{?[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\}?$/"; + public function __construct(string $uuid) { - if (preg_match(self::$ValidationRegex, $uuid)) { + if (preg_match(pattern: self::$UUIDValidationRegex, subject: $uuid) == 1) $this->UUID = $uuid; - } else { + elseif (preg_match(pattern: self::$DeegraphUUIDValidationRegex, subject: $uuid) == 1) + $this->UUID = substr(string: $uuid, offset: 1, length: -1); + else throw new InvalidUUIDFormatException(); - } } public function __toString(): string { - return "{{$this->UUID}}"; + return "{" . $this->UUID . "}"; } } diff --git a/tests/UUIDTest.php b/tests/UUIDTest.php new file mode 100644 index 0000000..8fb61f3 --- /dev/null +++ b/tests/UUIDTest.php @@ -0,0 +1,26 @@ +ExampleUUID . "}", + actual: strval(new UUID($this->ExampleUUID)), + ); + } + public function testValidUUID2() + { + self::assertEquals( + expected: "{" . $this->ExampleUUID . "}", + actual: strval(new UUID("{" . $this->ExampleUUID . "}")), + ); + } +}