@@ -124,15 +124,17 @@ final public static function v8(string $uuid): UuidV8
124124 /**
125125 * @param int-mask-of<Uuid::FORMAT_*> $format
126126 */
127- public static function isValid (string $ uuid /*, int $format = self::FORMAT_RFC_4122 */ ): bool
127+ public static function isValid (string $ uuid /* , int $format = self::FORMAT_RFC_4122 */ ): bool
128128 {
129129 $ format = 1 < \func_num_args () ? func_get_arg (1 ) : self ::FORMAT_RFC_4122 ;
130130
131131 if (36 === \strlen ($ uuid ) && !($ format & self ::FORMAT_RFC_4122 )) {
132132 return false ;
133133 }
134134
135- $ uuid = self ::transformToRfc4122 ($ uuid , $ format );
135+ if (false === $ uuid = self ::transformToRfc4122 ($ uuid , $ format )) {
136+ return false ;
137+ }
136138
137139 if (self ::NIL === $ uuid && \in_array (static ::class, [__CLASS__ , NilUuid::class], true )) {
138140 return true ;
@@ -190,10 +192,11 @@ private static function format(string $uuid, string $version): string
190192 *
191193 * @param int-mask-of<Uuid::FORMAT_*> $format
192194 *
193- * @return non-empty- string
195+ * @return string|false The RFC4122 string or false if the format doesn't match the input
194196 */
195- private static function transformToRfc4122 (string $ uuid , int $ format ): string
197+ private static function transformToRfc4122 (string $ uuid , int $ format ): string | false
196198 {
199+ $ inputUuid = $ uuid ;
197200 $ fromBase58 = false ;
198201 if (22 === \strlen ($ uuid ) && 22 === strspn ($ uuid , BinaryUtil::BASE58 ['' ]) && $ format & self ::FORMAT_BASE_58 ) {
199202 $ 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
214217 $ uuid = $ ulid ->toRfc4122 ();
215218 }
216219
220+ if ($ inputUuid === $ uuid && !($ format & self ::FORMAT_RFC_4122 )) {
221+ // input format doesn't match the input string
222+ return false ;
223+ }
224+
217225 return $ uuid ;
218226 }
219227}
0 commit comments