8000 minor #58089 [Uid] Rework internal format conversion (alexandre-daubois) · devloop42/symfony@f892c58 · GitHub
[go: up one dir, main page]

Skip to content

Commit f892c58

Browse files
minor symfony#58089 [Uid] Rework internal format conversion (alexandre-daubois)
This PR was merged into the 7.2 branch. Discussion ---------- [Uid] Rework internal format conversion | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT On a second look, the control flow is a bit confusing so I propose this refactor to clear things up. Commits ------- 736c5a6 [Uid] Rework internal format conversion
2 parents d43dd05 + 736c5a6 commit f892c58

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Symfony/Component/Uid/Uuid.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
8000 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)
0