diff --git a/src/Assert.php b/src/Assert.php index 317cb258..089b0202 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -2442,16 +2442,10 @@ public static function uuid(mixed $value, string|callable $message = ''): string { static::string($value, $message); - $originalValue = $value; - $value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value); - - // The nil UUID is special form of UUID that is specified to have all - // 128 bits set to zero. - if ('00000000-0000-0000-0000-000000000000' === $value) { - return $originalValue; - } - - if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/D', $value)) { + // Accepts the plain form (including the nil UUID with all 128 bits + // set to zero), optionally preceded by "urn:" and/or "uuid:" and + // optionally wrapped in a matching pair of curly braces. + if (!\preg_match('/^(?:urn:)?(?:uuid:)?(\{)?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}(?(1)\})$/D', $value)) { $message = self::resolveMessage($message); static::reportInvalidArgument(\sprintf( $message ?: 'Value %s is not a valid UUID.', @@ -2459,7 +2453,7 @@ public static function uuid(mixed $value, string|callable $message = ''): string )); } - return $originalValue; + return $value; } /** diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 6c947c5b..e7cbcb5a 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -588,7 +588,9 @@ public static function getTests(): array ['isNonEmptyMap', [[1, 2, 3]], false], ['uuid', ['00000000-0000-0000-0000-000000000000'], true], ['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], + ['uuid', ['urn:uuid:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], ['uuid', ['uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true], + ['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true], ['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], ['uuid', ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], true], ['uuid', ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], true], @@ -603,6 +605,12 @@ public static function getTests(): array ['uuid', ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], false], ['uuid', ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], false], ['uuid', ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], false], + ['uuid', ['f{}f6f8cb0-c57d-21e1-9b21-0800200c9a66'], false], + ['uuid', ['ff6f8cb0-c57d-21e1-9b21-08002urn:00c9a66'], false], + ['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66uuid:'], false], + ['uuid', ['{}{}ff6f8cb0-c57d-21e1-9b21-0800200c9a66{}{}'], false], + ['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], false], + ['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], false], ['throws', [function () { throw new LogicException('test'); }, 'LogicException'], true], ['throws', [function () { throw new LogicException('test'); }, 'IllogicException'], false], ['throws', [function () { throw new Exception('test'); }], true],