@@ -22,12 +22,6 @@ Installation
22
22
23
23
.. include :: /components/require_autoload.rst.inc
24
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
25
UUIDs
32
26
-----
33
27
@@ -39,40 +33,69 @@ groups of hexadecimal characters: ``xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx``
39
33
Generating UUIDs
40
34
~~~~~~~~~~~~~~~~
41
35
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::
43
38
44
39
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;
45
45
46
46
// UUID type 1 generates the UUID using the MAC address of your device and a timestamp.
47
47
// Both are obtained automatically, so you don't have to pass any constructor argument.
48
48
$uuid = Uuid::v1();
49
+ $uuid = new UuidV1();
49
50
50
51
// UUID type 4 generates a random UUID, so you don't have to pass any constructor argument.
51
52
$uuid = Uuid::v4();
53
+ $uuid = new UuidV4();
52
54
53
55
// UUID type 3 and 5 generate a UUID hashing the given namespace and name. Type 3 uses
54
56
// MD5 hashes and Type 5 uses SHA-1. The namespace is another UUID (e.g. a Type 4 UUID)
55
57
// and the name is an arbitrary string (e.g. a product name; if it's unique).
56
- $namespace = Uuid::v4 ();
58
+ $namespace = new UuidV4 ();
57
59
$name = $product->getUniqueName();
60
+
58
61
$uuid = Uuid::v3($namespace, $name);
62
+ $uuid = new UuidV3($namespace, $name);
63
+
59
64
$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();
60
72
61
73
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::
64
76
65
77
// this value is generated somewhere else
66
78
$uuidValue = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
67
79
$uuid = new Uuid($uuidValue);
80
+ $uuid = Uuid::fromString($uuidValue);
68
81
69
82
If your UUIDs are generated in binary format, use the ``fromBinary() `` method
70
83
to create the objects for them::
71
84
72
85
$uuid = Uuid::fromBinary($uuidBinaryContents);
73
86
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"
76
99
77
100
Working with UUIDs
78
101
~~~~~~~~~~~~~~~~~~
@@ -81,19 +104,25 @@ UUID objects created with the ``Uuid`` class can use the following methods
81
104
(which are equivalent to the ``uuid_*() `` method of the PHP extension)::
82
105
83
106
use Symfony\Component\Uid\Uuid;
107
+ use Symfony\Component\Uid\NilUuid;
84
108
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
86
113
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
88
118
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)
91
122
92
- // the time and MAC address is only available in certain UUID types
123
+ // comparing UUIDs and checking for equality
93
124
$uuid1 = Uuid::v1();
94
- $uuid1->getTime(); // e.g. float(1584111384.2613)
95
- $uuid1->getMac(); // e.g. string(12) "27cb420bea01"
96
-
125
+ $uuid4 = Uuid::v4();
97
126
$uuid1->equals($uuid4); // false
98
127
99
128
// this method returns:
@@ -124,16 +153,26 @@ Instantiate the ``Ulid`` class to generate a random ULID value::
124
153
125
154
If your ULID is generated by another system, pass its value to the constructor::
126
155
127
- $ulidValue = 'X7NR9KZ0A4Y9BV1431S70A03M7 ';
156
+ $ulidValue = '01E439TP9XJZ9RPFH3T1PYBCR8 ';
128
157
$ulid = new Ulid($ulidValue);
129
158
130
159
If your ULIDs are generated in binary format, use the ``fromBinary() `` method
131
160
to create the objects for them::
132
161
133
162
$ulid = Ulid::fromBinary($ulidBinaryContents);
134
163
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"
137
176
138
177
Working with ULIDs
139
178
~~~~~~~~~~~~~~~~~~
@@ -145,8 +184,14 @@ ULID objects created with the ``Ulid`` class can use the following methods::
145
184
$ulid1 = new Ulid();
146
185
$ulid2 = new Ulid();
147
186
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
148
194
$ulid1->equals($ulid2); // false
149
- $ulid1->getTime(); // e.g. float(1584111384.2613)
150
195
// this method returns $ulid1 <=> $ulid2
151
196
$uuid1->compare($uuid4); // e.g. int(-1)
152
197
0 commit comments