Skip to content

Commit 5df68ba

Browse files
authored
More typing and cleanup (#27)
1 parent 1d76242 commit 5df68ba

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/UUID.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ class UUID
6060
/** @internal */
6161
private const REPLACE_ARR = array('urn:', 'uuid:', '-', '{', '}');
6262

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';
66+
6367
/** @internal */
6468
private static $unixts = 0;
6569

6670
/** @internal */
6771
private static $subsec = 0;
6872

6973
/** @internal */
70-
private static function getUnixTime()
74+
private static function getUnixTime(): array
7175
{
7276
$timestamp = microtime(false);
7377
$unixts = intval(substr($timestamp, 11), 10);
@@ -88,7 +92,7 @@ private static function getUnixTime()
8892
}
8993

9094
/** @internal */
91-
private static function stripExtras($uuid)
95+
private static function stripExtras(string $uuid): string
9296
{
9397
if (!self::isValid($uuid)) {
9498
throw new \InvalidArgumentException('Invalid UUID string: ' . $uuid);
@@ -98,7 +102,7 @@ private static function stripExtras($uuid)
98102
}
99103

100104
/** @internal */
101-
private static function getBytes($uuid)
105+
private static function getBytes(string $uuid): string
102106
{
103107
$uhex = self::stripExtras($uuid);
104108

@@ -113,7 +117,7 @@ private static function getBytes($uuid)
113117
}
114118

115119
/** @internal */
116-
private static function uuidFromHex($uhex, $version)
120+
private static function uuidFromHex(string $uhex, int $version): string
117121
{
118122
return sprintf(
119123
'%08s-%04s-%04x-%04x-%12s',
@@ -223,8 +227,7 @@ public static function uuid7(): string
223227
*/
224228
public static function isValid(string $uuid): bool
225229
{
226-
return preg_match('/^(urn:)?(uuid:)?(\{)?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}'
227-
. '\-?[0-9a-f]{4}\-?[0-9a-f]{12}(?(3)\}|)$/i', $uuid) === 1;
230+
return preg_match(self::UUID_REGEX, $uuid) === 1;
228231
}
229232

230233
/**
@@ -247,8 +250,7 @@ public static function equals(string $uuid1, string $uuid2): bool
247250
*/
248251
public static function getVersion(string $uuid): int
249252
{
250-
$bytes = unpack('n*', self::getBytes($uuid));
251-
return (int) $bytes[4] >> 12;
253+
return intval(self::stripExtras($uuid)[12], 16);
252254
}
253255

254256
/**

0 commit comments

Comments
 (0)