55use Ramsey \Uuid \Uuid ;
66use RuntimeException ;
77
8+ /**
9+ * XUID (eXtended Unique IDentifier) utility class
10+ *
11+ * Provides functionality to generate and manipulate XUIDs, which are
12+ * URL-safe, base64-encoded representations of UUIDs.
13+ */
814class Xuid
915{
10- protected static $ map = [
16+ /**
17+ * @var array<string, string> Character mapping for base64 URL encoding
18+ */
19+ protected static array $ map = [
1120 '+ ' => '- ' ,
1221 '/ ' => '_ ' ,
1322 ];
1423
15- protected static $ alphaNumericOnly = false ;
24+ /**
25+ * @var bool Whether to force alphanumeric-only XUIDs
26+ */
27+ protected static bool $ alphaNumericOnly = false ;
1628
17- public static function forceAlphaNumeric ($ force = true )
29+ /**
30+ * Force generation of alphanumeric-only XUIDs
31+ *
32+ * @param bool $force Whether to force alphanumeric-only generation
33+ */
34+ public static function forceAlphaNumeric (bool $ force = true ): void
1835 {
1936 self ::$ alphaNumericOnly = $ force ;
2037 }
2138
22- public static function setMap ($ map )
39+ /**
40+ * Set custom character mapping for base64 URL encoding
41+ *
42+ * @param array<string, string> $map Character mapping array
43+ */
44+ public static function setMap (array $ map ): void
2345 {
2446 self ::$ map = $ map ;
2547 }
2648
27- public static function isValidUuid ($ uuid )
49+ /**
50+ * Validate if a string is a valid UUID
51+ *
52+ * @param string $uuid The UUID string to validate
53+ * @return bool True if valid UUID, false otherwise
54+ */
55+ public static function isValidUuid (string $ uuid ): bool
2856 {
2957 if (preg_match ("/^(\{)?[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}(?(1)\})$/i " , $ uuid )) {
3058 return true ;
3159 }
3260 return false ;
3361 }
3462
35- public static function isValidXuid ($ xuid )
63+ /**
64+ * Validate if a string is a valid XUID
65+ *
66+ * @param string $xuid The XUID string to validate
67+ * @return bool True if valid XUID, false otherwise
68+ */
69+ public static function isValidXuid (string $ xuid ): bool
3670 {
3771 $ uuid = self ::decode ($ xuid );
3872 return self ::isValidUuid ($ uuid );
3973 }
4074
4175
42- public static function getXuid ()
76+ /**
77+ * Generate a new XUID
78+ *
79+ * @return string A new XUID string
80+ */
81+ public static function getXuid (): string
4382 {
4483 do {
4584 $ uuid = self ::getUuid ();
@@ -48,13 +87,24 @@ public static function getXuid()
4887 return $ xuid ;
4988 }
5089
51- public static function getUuid ()
90+ /**
91+ * Generate a new UUID v4
92+ *
93+ * @return string A new UUID v4 string
94+ */
95+ public static function getUuid (): string
5296 {
5397 $ uuid = Uuid::uuid4 ();
54- return $ uuid ;
98+ return $ uuid-> toString () ;
5599 }
56100
57- public static function base64UrlEncode ($ data )
101+ /**
102+ * Encode data using base64 URL-safe encoding
103+ *
104+ * @param string $data The data to encode
105+ * @return string The base64 URL-safe encoded string
106+ */
107+ public static function base64UrlEncode (string $ data ): string
58108 {
59109 $ str = rtrim (base64_encode ($ data ), '= ' );
60110 foreach (self ::$ map as $ from => $ to ) {
@@ -63,7 +113,13 @@ public static function base64UrlEncode($data)
63113 return $ str ;
64114 }
65115
66- public static function base64UrlDecode ($ data )
116+ /**
117+ * Decode base64 URL-safe encoded data
118+ *
119+ * @param string $data The base64 URL-safe encoded string to decode
120+ * @return string The decoded data
121+ */
122+ public static function base64UrlDecode (string $ data ): string
67123 {
68124 $ str = strtr ($ data , '-_ ' , '+/ ' );
69125 foreach (self ::$ map as $ from => $ to ) {
@@ -74,7 +130,14 @@ public static function base64UrlDecode($data)
74130 return $ str ;
75131 }
76132
77- public static function encode ($ uuid )
133+ /**
134+ * Encode a UUID to XUID format
135+ *
136+ * @param string $uuid The UUID string to encode
137+ * @return string The encoded XUID string
138+ * @throws RuntimeException If the UUID is invalid
139+ */
140+ public static function encode (string $ uuid ): string
78141 {
79142 if (!self ::isValidUuid ($ uuid )) {
80143 throw new RuntimeException ("Invalid UUID " );
@@ -85,7 +148,14 @@ public static function encode($uuid)
85148 return $ xuid ;
86149 }
87150
88- public static function decode ($ xuid )
151+ /**
152+ * Decode a XUID to UUID format
153+ *
154+ * @param string $xuid The XUID string to decode
155+ * @return string The decoded UUID string
156+ * @throws RuntimeException If the XUID is invalid
157+ */
158+ public static function decode (string $ xuid ): string
89159 {
90160 $ bin = self ::base64UrlDecode ($ xuid );
91161 $ uuid = bin2hex ($ bin );
0 commit comments