Skip to content

Commit ac6c642

Browse files
committed
Updated the contents
1 parent c37a518 commit ac6c642

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

components/uid.rst

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ Installation
2222
2323
.. include:: /components/require_autoload.rst.inc
2424

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-
3125
UUIDs
3226
-----
3327

@@ -39,40 +33,69 @@ groups of hexadecimal characters: ``xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx``
3933
Generating UUIDs
4034
~~~~~~~~~~~~~~~~
4135

42-
Use any of the following named constructors depending on your needs::
36+
Use the named constructors of the ``Uuid`` class or any of the specific classes
37+
to create each type of UUID::
4338

4439
use Symfony\Component\Uid\Uuid;
40+
use Symfony\Component\Uid\UuidV1;
41+
use Symfony\Component\Uid\UuidV3;
42+
use Symfony\Component\Uid\UuidV4;
43+
use Symfony\Component\Uid\UuidV5;
44+
use Symfony\Component\Uid\UuidV6;
4545

4646
// UUID type 1 generates the UUID using the MAC address of your device and a timestamp.
4747
// Both are obtained automatically, so you don't have to pass any constructor argument.
4848
$uuid = Uuid::v1();
49+
$uuid = new UuidV1();
4950

5051
// UUID type 4 generates a random UUID, so you don't have to pass any constructor argument.
5152
$uuid = Uuid::v4();
53+
$uuid = new UuidV4();
5254

5355
// UUID type 3 and 5 generate a UUID hashing the given namespace and name. Type 3 uses
5456
// MD5 hashes and Type 5 uses SHA-1. The namespace is another UUID (e.g. a Type 4 UUID)
5557
// and the name is an arbitrary string (e.g. a product name; if it's unique).
56-
$namespace = Uuid::v4();
58+
$namespace = new UuidV4();
5759
$name = $product->getUniqueName();
60+
5861
$uuid = Uuid::v3($namespace, $name);
62+
$uuid = new UuidV3($namespace, $name);
63+
5964
$uuid = Uuid::v5($namespace, $name);
65+
$uuid = new UuidV5($namespace, $name);
66+
67+
// UUID type 6 is not part of the UUID standard. It's lexicographically sortable
68+
// (like ULIDs) and contains a 60-bit timestamp and 63 extra unique bits.
69+
// It's defined in http://gh.peabody.io/uuidv6/
70+
$uuid = Uuid::v6();
71+
$uuid = new UuidV6();
6072

6173
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::
74+
``Uuid`` class (or the static ``fromString()`` method) to create an object and
75+
make use of the utilities available for Symfony UUIDs::
6476

6577
// this value is generated somewhere else
6678
$uuidValue = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
6779
$uuid = new Uuid($uuidValue);
80+
$uuid = Uuid::fromString($uuidValue);
6881

6982
If your UUIDs are generated in binary format, use the ``fromBinary()`` method
7083
to create the objects for them::
7184

7285
$uuid = Uuid::fromBinary($uuidBinaryContents);
7386

74-
// the opposite method is also available
75-
$uuidAsBinary = $uuid->toBinary();
87+
Converting UUIDs
88+
~~~~~~~~~~~~~~~~
89+
90+
Use these methods to transform the UUID object into different bases::
91+
92+
$uuidValue = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
93+
$uuid = new Uuid($uuidValue);
94+
95+
$uuid->toBinary(); // string(16) "..." (binary contents can't be printed)
96+
$uuid->toBase32(); // string(26) "6SWYGR8QAV27NACAHMK5RG0RPG"
97+
$uuid->toBase58(); // string(22) "TuetYWNHhmuSQ3xPoVLv9M"
98+
$uuid->toRfc4122(); // string(36) "d9e7a184-5d5b-11ea-a62a-3499710062d0"
7699

77100
Working with UUIDs
78101
~~~~~~~~~~~~~~~~~~
@@ -81,19 +104,25 @@ UUID objects created with the ``Uuid`` class can use the following methods
81104
(which are equivalent to the ``uuid_*()`` method of the PHP extension)::
82105

83106
use Symfony\Component\Uid\Uuid;
107+
use Symfony\Component\Uid\NilUuid;
84108

85-
$uuid4 = Uuid::v4();
109+
// checking if the UUID is null (note that the class is called
110+
// NilUuid instead of NullUuid to follow the UUID standard notation)
111+
$uuid = Uuid::v4();
112+
$uuid instanceof NilUuid; // false
86113

87-
$uuid4->isNull(); // false
114+
// checking the type of UUID
115+
use Symfony\Component\Uid\UuidV4;
116+
$uuid = Uuid::v4();
117+
$uuid instanceof UuidV4; // true
88118

89-
// the Uuid class defines public constants for all versions (Uuid::TYPE_1, Uuid::TYPE_3, etc.)
90-
$uuid4->getType(); // int(4)
119+
// getting the UUID time (it's only available in certain UUID types)
120+
$uuid = Uuid::v1();
121+
$uuid->getTime(); // e.g. float(1584111384.2613)
91122

92-
// the time and MAC address is only available in certain UUID types
123+
// comparing UUIDs and checking for equality
93124
$uuid1 = Uuid::v1();
94-
$uuid1->getTime(); // e.g. float(1584111384.2613)
95-
$uuid1->getMac(); // e.g. string(12) "27cb420bea01"
96-
125+
$uuid4 = Uuid::v4();
97126
$uuid1->equals($uuid4); // false
98127

99128
// this method returns:
@@ -124,16 +153,26 @@ Instantiate the ``Ulid`` class to generate a random ULID value::
124153

125154
If your ULID is generated by another system, pass its value to the constructor::
126155

127-
$ulidValue = 'X7NR9KZ0A4Y9BV1431S70A03M7';
156+
$ulidValue = '01E439TP9XJZ9RPFH3T1PYBCR8';
128157
$ulid = new Ulid($ulidValue);
129158

130159
If your ULIDs are generated in binary format, use the ``fromBinary()`` method
131160
to create the objects for them::
132161

133162
$ulid = Ulid::fromBinary($ulidBinaryContents);
134163

135-
// the opposite method is also available
136-
$ulidAsBinary = $ulid->toBinary();
164+
Converting ULIDs
165+
~~~~~~~~~~~~~~~~
166+
167+
Use these methods to transform the ULID object into different bases::
168+
169+
$ulidValue = '01E439TP9XJZ9RPFH3T1PYBCR8';
170+
$ulid = new Ulid($ulidValue);
171+
172+
$ulid->toBinary(); // string(16) "..." (binary contents can't be printed)
173+
$ulid->toBase32(); // string(26) "01E439TP9XJZ9RPFH3T1PYBCR8"
174+
$ulid->toBase58(); // string(22) "1BKocMc5BnrVcuq2ti4Eqm"
175+
$ulid->toRfc4122(); // string(36) "0171069d-593d-97d3-8b3e-23d06de5b308"
137176

138177
Working with ULIDs
139178
~~~~~~~~~~~~~~~~~~
@@ -145,8 +184,14 @@ ULID objects created with the ``Ulid`` class can use the following methods::
145184
$ulid1 = new Ulid();
146185
$ulid2 = new Ulid();
147186

187+
// checking if a given value is valid as ULID
188+
$isValid = Ulid::isValid($ulidValue); // true or false
189+
190+
// getting the ULID time
191+
$ulid1->getTime(); // e.g. float(1584111384.2613)
192+
193+
// comparing ULIDs and checking for equality
148194
$ulid1->equals($ulid2); // false
149-
$ulid1->getTime(); // e.g. float(1584111384.2613)
150195
// this method returns $ulid1 <=> $ulid2
151196
$uuid1->compare($uuid4); // e.g. int(-1)
152197

0 commit comments

Comments
 (0)