@@ -132,7 +132,9 @@ public static function isValid(string $uuid /*, int $format = self::FORMAT_RFC_4
132
132
return false ;
133
133
}
134
134
135
- $ uuid = self ::transformToRfc4122 ($ uuid , $ format );
135
+ if (!$ uuid = self ::transformToRfc4122 ($ uuid , $ format )) {
136
+ return false ;
137
+ }
136
138
137
139
if (self ::NIL === $ uuid && \in_array (static ::class, [__CLASS__ , NilUuid::class], true )) {
138
140
return true ;
@@ -190,10 +192,11 @@ private static function format(string $uuid, string $version): string
190
192
*
191
193
* @param int-mask-of<Uuid::FORMAT_*> $format
192
194
*
193
- * @return non-empty- string
195
+ * @return string|false The RFC4122 string or false if the format doesn't match the input
194
196
*/
195
- private static function transformToRfc4122 (string $ uuid , int $ format ): string
197
+ private static function transformToRfc4122 (string $ uuid , int $ format ): string | false
196
198
{
199
+ $ inputUuid = $ uuid ;
197
200
$ fromBase58 = false ;
198
201
if (22 === \strlen ($ uuid ) && 22 === strspn ($ uuid , BinaryUtil::BASE58 ['' ]) && $ format & self ::FORMAT_BASE_58 ) {
199
202
$ uuid = str_pad (BinaryUtil::fromBase ($ uuid , BinaryUtil::BASE58 ), 16 , "\0" , \STR_PAD_LEFT );
@@ -214,6 +217,11 @@ private static function transformToRfc4122(string $uuid, int $format): string
214
217
$ uuid = $ ulid ->toRfc4122 ();
215
218
}
216
219
220
+ if ($ inputUuid === $ uuid && !($ format & self ::FORMAT_RFC_4122 )) {
221
+ // input format doesn't match the input string
222
+ return false ;
223
+ }
224
+
217
225
return $ uuid ;
218
226
}
219
227
}
0 commit comments