Skip to content

Commit 3879b79

Browse files
[Uid] Rework internal format conversion
1 parent e314213 commit 3879b79

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Symfony/Component/Uid/Uuid.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ public static function isValid(string $uuid /*, int $format = self::FORMAT_RFC_4
132132
return false;
133133
}
134134

135-
$uuid = self::transformToRfc4122($uuid, $format);
135+
if (!$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

Comments
 (0)