Skip to content

Commit 0ff54cc

Browse files
authored
More unit tests, use $matcheswith preg_match() (#30)
1 parent c52e222 commit 0ff54cc

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

src/UUID.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ class UUID
5858
public const TIME_OFFSET_INT = 0x01b21dd213814000;
5959

6060
/** @internal */
61-
private const REPLACE_ARR = array('urn:', 'uuid:', '-', '{', '}');
62-
63-
/** @internal */
64-
private const UUID_REGEX = '/^(?:urn:)?(?:uuid:)?(\{)?[0-9a-f]{8}\-?[0-9a-f]{4}'
65-
. '\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}(?(1)\}|)$/i';
61+
private const UUID_REGEX = '/^(?:urn:)?(?:uuid:)?(\{)?([0-9a-f]{8})\-?([0-9a-f]{4})'
62+
. '\-?([0-9a-f]{4})\-?([0-9a-f]{4})\-?([0-9a-f]{12})(?(1)\}|)$/i';
6663

6764
/** @internal */
6865
private static $unixts = 0;
@@ -94,11 +91,11 @@ private static function getUnixTime(): array
9491
/** @internal */
9592
private static function stripExtras(string $uuid): string
9693
{
97-
if (!self::isValid($uuid)) {
94+
if (preg_match(self::UUID_REGEX, $uuid, $m) !== 1) {
9895
throw new \InvalidArgumentException('Invalid UUID string: ' . $uuid);
9996
}
10097
// Get hexadecimal components of UUID
101-
return strtolower(str_replace(self::REPLACE_ARR, '', $uuid));
98+
return strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6]);
10299
}
103100

104101
/** @internal */

tests/UuidTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public function testCanValidate()
9999
$this->assertTrue(
100100
UUID::isValid('11a38b9a-b3da-360f-9353-a5a725514269')
101101
);
102+
$this->assertFalse(
103+
UUID::isValid('11a38b9a-b3da-360f-9353-a5a72551426')
104+
);
105+
$this->assertFalse(
106+
UUID::isValid('11a38b9a-b3da-360f-9353-a5a7255142690')
107+
);
102108
$this->assertTrue(
103109
UUID::isValid('urn:uuid:c4a760a8-dbcf-5254-a0d9-6a4474bd1b62')
104110
);
@@ -181,6 +187,14 @@ public function testCanUseAliases()
181187
);
182188
}
183189

190+
public function testKnownGetTime()
191+
{
192+
$uuid6_time = UUID::getTime('1ebacf4f-a4a8-68ee-b4ec-618c14d005d5');
193+
$this->assertSame($uuid6_time, '1620145373.6118510');
194+
$uuid7_time = UUID::getTime('061a3d43-61d0-7cf4-bfce-753dadab55e1');
195+
$this->assertSame($uuid7_time, '1638126646.1903860');
196+
}
197+
184198
public function testGetTimeValid()
185199
{
186200
$now = microtime(true);

0 commit comments

Comments
 (0)