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 . "}")), + ); + } +}