Browse Source

more work on the UUID class

dev
Cerys 3 weeks ago
parent
commit
621d2e51a8
  1. 14
      src/DataStructures/UUID.php
  2. 26
      tests/UUIDTest.php

14
src/DataStructures/UUID.php

@ -8,17 +8,21 @@ use Exception;
class UUID class UUID
{ {
private string $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) public function __construct(string $uuid)
{ {
if (preg_match(self::$ValidationRegex, $uuid)) { if (preg_match(pattern: self::$UUIDValidationRegex, subject: $uuid) == 1)
$this->UUID = $uuid; $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(); throw new InvalidUUIDFormatException();
}
} }
public function __toString(): string public function __toString(): string
{ {
return "{{$this->UUID}}"; return "{" . $this->UUID . "}";
} }
} }

26
tests/UUIDTest.php

@ -0,0 +1,26 @@
<?php
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait;
use Darksparrow\DeegraphInteractions\DataStructures\UUID;
use PHPUnit\Framework\TestCase;
class UUIDTest extends TestCase
{
private string $ExampleUUID = "12345678-ACDE-EFAB-CDEF-123456123456";
public function testValidUUID()
{
self::assertEquals(
expected: "{" . $this->ExampleUUID . "}",
actual: strval(new UUID($this->ExampleUUID)),
);
}
public function testValidUUID2()
{
self::assertEquals(
expected: "{" . $this->ExampleUUID . "}",
actual: strval(new UUID("{" . $this->ExampleUUID . "}")),
);
}
}
Loading…
Cancel
Save