Skip to content

[Serializer][Uid] Add the Uuid::FORMAT_RFC_9562 and UidNormalizer::NORMALIZATION_FORMAT_RFC9562 constants #58246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate `CsvEncoderContextBuilder::withEscapeChar()` method
* Add `SnakeCaseToCamelCaseNameConverter`
* Support subclasses of `\DateTime` and `\DateTimeImmutable` for denormalization
* Add the `UidNormalizer::NORMALIZATION_FORMAT_RFC9562` constant

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface
public const NORMALIZATION_FORMAT_BASE58 = 'base58';
public const NORMALIZATION_FORMAT_BASE32 = 'base32';
public const NORMALIZATION_FORMAT_RFC4122 = 'rfc4122';
public const NORMALIZATION_FORMAT_RFC9562 = self::NORMALIZATION_FORMAT_RFC4122;
public const NORMALIZATION_FORMATS = [
self::NORMALIZATION_FORMAT_CANONICAL,
self::NORMALIZATION_FORMAT_BASE58,
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Make `AbstractUid` implement `Ds\Hashable` if available
* Add support for binary, base-32 and base-58 representations in `Uuid::isValid()`
* Add the `Uuid::FORMAT_RFC_9562` constant to validate UUIDs in the RFC 9562 format

7.1
---
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public function testIsValidWithVariousFormat()
$this->assertFalse(Uuid::isValid($uuid->toBinary(), Uuid::FORMAT_RFC_4122));
$this->assertTrue(Uuid::isValid($uuid->toRfc4122(), Uuid::FORMAT_RFC_4122));

$this->assertFalse(Uuid::isValid($uuid->toBase32(), Uuid::FORMAT_RFC_9562));
$this->assertFalse(Uuid::isValid($uuid->toBase58(), Uuid::FORMAT_RFC_9562));
$this->assertFalse(Uuid::isValid($uuid->toBinary(), Uuid::FORMAT_RFC_9562));
$this->assertTrue(Uuid::isValid($uuid->toRfc4122(), Uuid::FORMAT_RFC_9562));

$this->assertTrue(Uuid::isValid($uuid->toBase32(), Uuid::FORMAT_ALL));
$this->assertTrue(Uuid::isValid($uuid->toBase58(), Uuid::FORMAT_ALL));
$this->assertTrue(Uuid::isValid($uuid->toBinary(), Uuid::FORMAT_ALL));
Expand Down
19 changes: 10 additions & 9 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public const FORMAT_BASE_32 = 1 << 1;
public const FORMAT_BASE_58 = 1 << 2;
public const FORMAT_RFC_4122 = 1 << 3;
public const FORMAT_RFC_9562 = self::FORMAT_RFC_4122;
public const FORMAT_ALL = -1;

protected const TYPE = 0;
Expand All @@ -50,7 +51,7 @@

public static function fromString(string $uuid): static
{
$uuid = self::transformToRfc4122($uuid, self::FORMAT_ALL);
$uuid = self::transformToRfc9562($uuid, self::FORMAT_ALL);

if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
return new static($uuid);
Expand Down Expand Up @@ -124,15 +125,15 @@
/**
* @param int-mask-of<Uuid::FORMAT_*> $format
*/
public static function isValid(string $uuid /* , int $format = self::FORMAT_RFC_4122 */): bool
public static function isValid(string $uuid /* , int $format = self::FORMAT_RFC_9562 */): bool

Check failure on line 128 in src/Symfony/Component/Uid/Uuid.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Uid/Uuid.php:128:43: ParamNameMismatch: Argument 1 of Symfony\Component\Uid\Uuid::isValid has wrong name $uuid, expecting $uid as defined by Symfony\Component\Uid\AbstractUid::isValid (see https://psalm.dev/230)

Check failure on line 128 in src/Symfony/Component/Uid/Uuid.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Uid/Uuid.php:128:43: ParamNameMismatch: Argument 1 of Symfony\Component\Uid\Uuid::isValid has wrong name $uuid, expecting $uid as defined by Symfony\Component\Uid\AbstractUid::isValid (see https://psalm.dev/230)
{
$format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_4122;
$format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_9562;

if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_4122)) {
if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_9562)) {
return false;
}

if (false === $uuid = self::transformToRfc4122($uuid, $format)) {
if (false === $uuid = self::transformToRfc9562($uuid, $format)) {
return false;
}

Expand Down Expand Up @@ -188,13 +189,13 @@
}

/**
* Transforms a binary string, a base-32 string or a base-58 string to a RFC4122 string.
* Transforms a binary string, a base-32 string or a base-58 string to a RFC9562 string.
*
* @param int-mask-of<Uuid::FORMAT_*> $format
*
* @return string|false The RFC4122 string or false if the format doesn't match the input
* @return string|false The RFC9562 string or false if the format doesn't match the input
*/
private static function transformToRfc4122(string $uuid, int $format): string|false
private static function transformToRfc9562(string $uuid, int $format): string|false
{
$inputUuid = $uuid;
$fromBase58 = false;
Expand All @@ -217,7 +218,7 @@
$uuid = $ulid->toRfc4122();
}

if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_4122)) {
if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_9562)) {
// input format doesn't match the input string
return false;
}
Expand Down
Loading