New in Symfony 6.2: New Uid Features

The Uid component provides an object-oriented API to generate and represent UIDs such as UUID and ULID. In Symfony 6.2 we're adding some features to it.

MaxUuid and MaxUlid

Contributed by
Nicolas Grekas
in #47515.

In Symfony 6.2 we're introducing two new classes called MaxUlid and MaxUuid which represent the highest possible value of both UUID and ULID. You can use those objects to get the max values string representation, to compare them to other UID objects, etc.

If you are curious, these are the maximum values possible:

Max UUID: 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'
Max ULID: '7ZZZZZZZZZZZZZZZZZZZZZZZZZ'

Time-based Uid Interface

Contributed by
Barney Hanlon
in #47507.

Some UIDs include a date/time as part of their value. In Symfony 6.2 we're introducing the following interface so you can get this date/time value more easily:

interface TimeBasedUidInterface
{
    public function getDateTime(): \DateTimeImmutable;
}

The interface is only implemented by the UID types/versions that include a date/time, which are: Ulid, UuidV1, and UuidV6.

Uid Conversion to Hexadecimal Values

Contributed by
Aurimas Niekis
in #45945.

UIDs include lots of methods to convert their value into different formats (toBase32(), toBase58(), toRfc4122(), etc.) In Symfony 6.2 we're introducing a new toHex() method which returns the binary value as an hexadecimal string:

use Symfony\Component\Uid\Uuid;

$uuid = Uuid::v4()
// e.g. (string) $uuid = 'd6b3345b-2905-4048-a83c-b5988e765d98'
$hex = $uuid->toHex();
// $hex = '0xd6b3345b29054048a83cb5988e765d98'

The hexadecimal string returned by toHex() is ready to use in other parts of your application, such as storing or querying UIDs in binary format in the database.

UuidV7 and UuidV8 Support

Contributed by
Nicolas Grekas
in #47525.

The UUID specification continues adding new formats to generate values suited for different scenarios. They recently added UUID Version 7 and UUID Version 8 formats and Symfony 6.2 adds support for them:

use Symfony\Component\Uid\Uuid;

$uuid = Uuid::v7();
// e.g. (string) $uuid = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f'
$uuid = Uuid::v8();
// e.g. (string) $uuid = '216fff40-98d9-81e3-a5e2-0800200c9a66'

Conceptually, UUID v7 is very similar to ULID: they're monotonic, sortable, unguessable, and embed a ms-unix-timestamp.

            <hr style="margin-bottom: 5px" />
            <div style="font-size: 90%">
                <a href="https://symfony.com/sponsor">Sponsor</a> the Symfony project.
            </div>

https://symfony.com/blog/new-in-symfony-6-2-new-uid-features?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

созданный 2y | 17 нояб. 2022 г., 17:20:14


Войдите, чтобы добавить комментарий

Другие сообщения в этой группе

SymfonyLive Paris 2025 : Tirez profit de Messenger pour améliorer votre architecture

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

28 февр. 2025 г., 10:31:15 | Symfony
SymfonyLive Berlin 2025: Building really fast applications

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here. 🚨 Enjoy the last few days bef

27 февр. 2025 г., 16:10:03 | Symfony
SymfonyLive Paris 2025 :  Async avec Messenger, AMQP et Mercure

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here. 🚨 Tod

26 февр. 2025 г., 14:31:31 | Symfony
Symfony 6.4.19 released

Symfony 6.4.19 has just been released. Here is the list of the most important changes since 6.4.18:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComm
26 февр. 2025 г., 12:20:03 | Symfony
Symfony 7.2.4 released

Symfony 7.2.4 has just been released. Here is the list of the most important changes since 7.2.3:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComman
26 февр. 2025 г., 12:20:03 | Symfony
Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25 февр. 2025 г., 15:20:33 | Symfony
New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24 февр. 2025 г., 16:20:03 | Symfony