|
| 1 | +.. index:: |
| 2 | + single: UID |
| 3 | + single: Components; UID |
| 4 | + |
| 5 | +The UID Component |
| 6 | +==================== |
| 7 | + |
| 8 | + The UID component provides utilities to work with `unique identifiers`_ (UIDs) |
| 9 | + such as UUIDs and ULIDs. |
| 10 | + |
| 11 | +.. versionadded:: 5.1 |
| 12 | + |
| 13 | + The UID component was introduced in Symfony 5.1 as an |
| 14 | + :doc:`experimental feature </contributing/code/experimental>`. |
| 15 | + |
| 16 | +Installation |
| 17 | +------------ |
| 18 | + |
| 19 | +.. code-block:: terminal |
| 20 | +
|
| 21 | + $ composer require symfony/uid |
| 22 | +
|
| 23 | +.. include:: /components/require_autoload.rst.inc |
| 24 | + |
| 25 | +Usage |
| 26 | +----- |
| 27 | + |
| 28 | +This component provides different classes to generate each type of UID. The |
| 29 | +following sections explain each of them. |
| 30 | + |
| 31 | +UUIDs |
| 32 | +----- |
| 33 | + |
| 34 | +`UUIDs`_ (*universally unique identifiers*) are one of the most popular UIDs in |
| 35 | +the software industry. UUIDs are 128-bit numbers usually represented as five |
| 36 | +groups of hexadecimal characters: ``xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx`` |
| 37 | +(the ``M`` digit is the UUID version and the ``N`` digit is the UUID variant). |
| 38 | + |
| 39 | +Generating UUIDs |
| 40 | +~~~~~~~~~~~~~~~~ |
| 41 | + |
| 42 | +Use any of the following named constructors depending on your needs:: |
| 43 | + |
| 44 | + use Symfony\Component\Uid\Uuid; |
| 45 | + |
| 46 | + // UUID type 1 generates the UUID using the MAC address of your device and a timestamp. |
| 47 | + // Both are obtained automatically, so you don't have to pass any constructor argument. |
| 48 | + $uuid = Uuid::v1(); |
| 49 | + |
| 50 | + // UUID type 4 generates a random UUID, so you don't have to pass any constructor argument. |
| 51 | + $uuid = Uuid::v4(); |
| 52 | + |
| 53 | + // UUID type 3 and 5 generate a UUID hashing the given namespace and name. Type 3 uses |
| 54 | + // MD5 hashes and Type 5 uses SHA-1. The namespace is another UUID (e.g. a Type 4 UUID) |
| 55 | + // and the name is an arbitrary string (e.g. a product name; if it's unique). |
| 56 | + $namespace = Uuid::v4(); |
| 57 | + $name = $product->getUniqueName(); |
| 58 | + $uuid = Uuid::v3($namespace, $name); |
| 59 | + $uuid = Uuid::v5($namespace, $name); |
| 60 | + |
| 61 | +If your UUID is generated by another system, use the regular constructor of the |
| 62 | +``Uuid`` class to create an object and make use of the utilities available for |
| 63 | +Symfony UUIDs:: |
| 64 | + |
| 65 | + // this value is generated somewhere else |
| 66 | + $uuidValue = 'd9e7a184-5d5b-11ea-a62a-3499710062d0'; |
| 67 | + $uuid = new Uuid($uuidValue); |
| 68 | + |
| 69 | +If your UUIDs are generated in binary format, use the ``fromBinary()`` method |
| 70 | +to create the objects for them:: |
| 71 | + |
| 72 | + $uuid = Uuid::fromBinary($uuidBinaryContents); |
| 73 | + |
| 74 | + // the opposite method is also available |
| 75 | + $uuidAsBinary = $uuid->toBinary(); |
| 76 | + |
| 77 | +Working with UUIDs |
| 78 | +~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +UUID objects created with the ``Uuid`` class can use the following methods |
| 81 | +(which are equivalent to the ``uuid_*()`` method of the PHP extension):: |
| 82 | + |
| 83 | + use Symfony\Component\Uid\Uuid; |
| 84 | + |
| 85 | + $uuid4 = Uuid::v4(); |
| 86 | + |
| 87 | + $uuid4->isNull(); // false |
| 88 | + |
| 89 | + // the Uuid class defines public constants for all versions and variants |
| 90 | + // (e.g. Uuid::TYPE_1, Uuid::TYPE_3, Uuid::VARIANT_NCS, etc.) |
| 91 | + $uuid4->getType(); // int(4) |
| 92 | + $uuid4->getVariant(); // int(1) |
| 93 | + |
| 94 | + // the time and MAC address is only available in certain UUID types |
| 95 | + $uuid1 = Uuid::v1(); |
| 96 | + $uuid1->getTime(); // e.g. float(1584111384.2613) |
| 97 | + $uuid1->getMac(); // e.g. string(12) "27cb420bea01" |
| 98 | + |
| 99 | + $uuid1->equals($uuid4); // false |
| 100 | + |
| 101 | + // this method returns: |
| 102 | + // * int(0) if $uuid1 and $uuid4 are equal |
| 103 | + // * int > 0 if $uuid1 is greater than $uuid4 |
| 104 | + // * int < 0 if $uuid1 is less than $uuid4 |
| 105 | + $uuid1->compare($uuid4); // e.g. int(4) |
| 106 | + |
| 107 | +ULIDs |
| 108 | +----- |
| 109 | + |
| 110 | +`ULIDs`_ (*Universally Unique Lexicographically Sortable Identifier*) are 128-bit |
| 111 | +numbers usually represented as a 26-character string: ``TTTTTTTTTTRRRRRRRRRRRRRRRR`` |
| 112 | +(where ``T`` represents a timestamp and ``R`` represents the random bits). |
| 113 | + |
| 114 | +ULIDs are an alternative to UUIDs when using those is impractical. They provide |
| 115 | +128-bit compatibility with UUID, they are lexicographically sortable and they |
| 116 | +are encoded as 26-character strings (vs 36-character UUIDs). |
| 117 | + |
| 118 | +Generating ULIDs |
| 119 | +~~~~~~~~~~~~~~~~ |
| 120 | + |
| 121 | +Instantiate the ``Ulid`` class to generate a random ULID value:: |
| 122 | + |
| 123 | + use Symfony\Component\Uid\Ulid; |
| 124 | + |
| 125 | + $ulid = new Ulid(); // e.g. 01AN4Z07BY79KA1307SR9X4MV3 |
| 126 | + |
| 127 | +If your ULID is generated by another system, pass its value to the constructor: |
| 128 | + |
| 129 | + $ulidValue = 'X7NR9KZ0A4Y9BV1431S70A03M7'; |
| 130 | + $ulid = new Ulid($ulidValue); |
| 131 | + |
| 132 | +If your ULIDs are generated in binary format, use the ``fromBinary()`` method |
| 133 | +to create the objects for them:: |
| 134 | + |
| 135 | + $ulid = Ulid::fromBinary($ulidBinaryContents); |
| 136 | + |
| 137 | + // the opposite method is also available |
| 138 | + $ulidAsBinary = $ulid->toBinary(); |
| 139 | + |
| 140 | +Working with ULIDs |
| 141 | +~~~~~~~~~~~~~~~~~~ |
| 142 | + |
| 143 | +ULID objects created with the ``Ulid`` class can use the following methods:: |
| 144 | + |
| 145 | + use Symfony\Component\Uid\Ulid; |
| 146 | + |
| 147 | + $ulid1 = new Ulid(); |
| 148 | + $ulid2 = new Ulid(); |
| 149 | + |
| 150 | + $ulid1->equals($ulid2); // false |
| 151 | + $ulid1->getTime(); // e.g. float(1584111384.2613) |
| 152 | + // this method returns $ulid1 <=> $ulid2 |
| 153 | + $uuid1->compare($uuid4); // e.g. int(-1) |
| 154 | + |
| 155 | +.. _`unique identifiers`: https://en.wikipedia.org/wiki/UID |
| 156 | +.. _`UUIDs`: https://en.wikipedia.org/wiki/Universally_unique_identifier |
| 157 | +.. _`ULIDs`: https://github.com/ulid/spec |
0 commit comments