Uuid
class Uuid implements UuidInterface (View source)
Represents a universally unique identifier (UUID), according to RFC 4122.
This class provides immutable UUID objects (the Uuid class) and the static
methods uuid1()
, uuid3()
, uuid4()
, and uuid5()
for generating version
1, 3, 4, and 5 UUIDs as specified in RFC 4122.
If all you want is a unique ID, you should probably call uuid1()
or uuid4()
.
Note that uuid1()
may compromise privacy since it creates a UUID containing
the computer’s network address. uuid4()
creates a random UUID.
Constants
NAMESPACE_DNS |
When this namespace is specified, the name string is a fully-qualified domain name. |
NAMESPACE_URL |
When this namespace is specified, the name string is a URL. |
NAMESPACE_OID |
When this namespace is specified, the name string is an ISO OID. |
NAMESPACE_X500 |
When this namespace is specified, the name string is an X.500 DN in DER or a text output format. |
NIL |
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero. |
RESERVED_NCS |
Reserved for NCS compatibility. |
RFC_4122 |
Specifies the UUID layout given in RFC 4122. |
RESERVED_MICROSOFT |
Reserved for Microsoft compatibility. |
RESERVED_FUTURE |
Reserved for future definition. |
VALID_PATTERN |
Regular expression pattern for matching a valid UUID of any variant. |
Properties
protected CodecInterface | $codec | The codec to use when encoding or decoding UUID strings. | |
protected array | $fields | The fields that make up this UUID. | |
protected NumberConverterInterface | $converter | The number converter to use for converting hex values to/from integers. |
Methods
Creates a universally unique identifier (UUID) from an array of fields.
Converts this UUID object to a string when the object is used in any string context.
Converts this UUID object to a string when the object is serialized
with json_encode()
Converts this UUID object to a string when the object is serialized
with serialize()
Re-constructs the object from its serialized form.
Compares this object to the specified object.
Returns the UUID as a 16-byte string (containing the six integer fields in big-endian byte order).
Returns the high field of the clock sequence multiplexed with the variant (bits 65-72 of the UUID).
Returns the high field of the clock sequence multiplexed with the variant (bits 65-72 of the UUID).
Returns the low field of the clock sequence (bits 73-80 of the UUID).
Returns the low field of the clock sequence (bits 73-80 of the UUID).
Returns the clock sequence value associated with this UUID.
Returns the clock sequence value associated with this UUID.
Returns the number converter to use for converting hex values to/from integers.
Returns a PHP DateTime
object representing the timestamp associated
with this UUID.
Returns an array of the fields of this UUID, with keys named according to the RFC 4122 names for the fields.
Returns an array of the fields of this UUID, with keys named according to the RFC 4122 names for the fields.
Returns the hexadecimal value of the UUID.
Returns the integer value of the UUID, converted to an appropriate number representation.
Returns the least significant 64 bits of this UUID's 128 bit value.
Returns the least significant 64 bits of this UUID's 128 bit value.
Returns the most significant 64 bits of this UUID's 128 bit value.
Returns the most significant 64 bits of this UUID's 128 bit value.
Returns the node value associated with this UUID
Returns the node value associated with this UUID
Returns the high field of the timestamp multiplexed with the version number (bits 49-64 of the UUID).
Returns the high field of the timestamp multiplexed with the version number (bits 49-64 of the UUID).
Returns the low field of the timestamp (the first 32 bits of the UUID).
Returns the low field of the timestamp (the first 32 bits of the UUID).
Returns the middle field of the timestamp (bits 33-48 of the UUID).
Returns the middle field of the timestamp (bits 33-48 of the UUID).
Returns the timestamp value associated with this UUID.
Returns the timestamp value associated with this UUID.
Returns the string representation of the UUID as a URN.
Returns the variant number associated with this UUID.
Returns the version number associated with this UUID.
Converts this UUID into a string representation.
Returns the currently set factory used to create UUIDs.
Creates a UUID from a byte string.
Creates a UUID from the string standard representation.
Creates a UUID from a 128-bit integer string.
Check if a string is a valid UUID.
Generate a version 1 UUID from a host ID, sequence number, and the current time.
Generate a version 3 UUID based on the MD5 hash of a namespace identifier (which is a UUID) and a name (which is a string).
Generate a version 4 (random) UUID.
Generate a version 5 UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).
Details
at line 156
__construct(array $fields, NumberConverterInterface $converter, CodecInterface $codec)
Creates a universally unique identifier (UUID) from an array of fields.
Unless you're making advanced use of this library to generate identifiers that deviate from RFC 4122, you probably do not want to instantiate a UUID directly. Use the static methods, instead:
use Ramsey\Uuid\Uuid;
$timeBasedUuid = Uuid::uuid1();
$namespaceMd5Uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://php.net/');
$randomUuid = Uuid::uuid4();
$namespaceSha1Uuid = Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://php.net/');
at line 173
string
__toString()
Converts this UUID object to a string when the object is used in any string context.
at line 185
string
jsonSerialize()
Converts this UUID object to a string when the object is serialized
with json_encode()
at line 197
string
serialize()
Converts this UUID object to a string when the object is serialized
with serialize()
at line 208
unserialize(string $serialized)
Re-constructs the object from its serialized form.
at line 216
int
compareTo(UuidInterface $other)
Compares this UUID to the specified UUID.
The first of two UUIDs is greater than the second if the most significant field in which the UUIDs differ is greater for the first UUID.
- Q. What's the value of being able to sort UUIDs?
- A. Use them as keys in a B-Tree or similar mapping.
at line 233
bool
equals(object $other)
Compares this object to the specified object.
The result is true if and only if the argument is not null, is a UUID object, has the same variant, and contains the same value, bit for bit, as this UUID.
at line 242
string
getBytes()
Returns the UUID as a 16-byte string (containing the six integer fields in big-endian byte order).
at line 253
int
getClockSeqHiAndReserved()
Returns the high field of the clock sequence multiplexed with the variant (bits 65-72 of the UUID).
at line 258
string
getClockSeqHiAndReservedHex()
Returns the high field of the clock sequence multiplexed with the variant (bits 65-72 of the UUID).
at line 268
int
getClockSeqLow()
Returns the low field of the clock sequence (bits 73-80 of the UUID).
at line 273
string
getClockSeqLowHex()
Returns the low field of the clock sequence (bits 73-80 of the UUID).
at line 294
int
getClockSequence()
Returns the clock sequence value associated with this UUID.
For UUID version 1, the clock sequence is used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes.
For UUID version 3 or 5, the clock sequence is a 14-bit value constructed from a name as described in RFC 4122, Section 4.3.
For UUID version 4, clock sequence is a randomly or pseudo-randomly generated 14-bit value as described in RFC 4122, Section 4.4.
at line 300
string
getClockSequenceHex()
Returns the clock sequence value associated with this UUID.
at line 305
NumberConverterInterface
getNumberConverter()
Returns the number converter to use for converting hex values to/from integers.
at line 310
DateTime
getDateTime()
Returns a PHP DateTime
object representing the timestamp associated
with this UUID.
The timestamp value is only meaningful in a time-based UUID, which
has version type 1. If this UUID is not a time-based UUID then
this method throws UnsupportedOperationException
.
at line 340
array
getFields()
Returns an array of the fields of this UUID, with keys named according to the RFC 4122 names for the fields.
- time_low: The low field of the timestamp, an unsigned 32-bit integer
- time_mid: The middle field of the timestamp, an unsigned 16-bit integer
- time_hi_and_version: The high field of the timestamp multiplexed with the version number, an unsigned 16-bit integer
- clock_seq_hi_and_reserved: The high field of the clock sequence multiplexed with the variant, an unsigned 8-bit integer
- clock_seq_low: The low field of the clock sequence, an unsigned 8-bit integer
- node: The spatially unique node identifier, an unsigned 48-bit integer
at line 352
array
getFieldsHex()
Returns an array of the fields of this UUID, with keys named according to the RFC 4122 names for the fields.
- time_low: The low field of the timestamp, an unsigned 32-bit integer
- time_mid: The middle field of the timestamp, an unsigned 16-bit integer
- time_hi_and_version: The high field of the timestamp multiplexed with the version number, an unsigned 16-bit integer
- clock_seq_hi_and_reserved: The high field of the clock sequence multiplexed with the variant, an unsigned 8-bit integer
- clock_seq_low: The low field of the clock sequence, an unsigned 8-bit integer
- node: The spatially unique node identifier, an unsigned 48-bit integer
at line 357
string
getHex()
Returns the hexadecimal value of the UUID.
at line 362
mixed
getInteger()
Returns the integer value of the UUID, converted to an appropriate number representation.
at line 372
mixed
getLeastSignificantBits()
Returns the least significant 64 bits of this UUID's 128 bit value.
at line 377
string
getLeastSignificantBitsHex()
Returns the least significant 64 bits of this UUID's 128 bit value.
at line 392
mixed
getMostSignificantBits()
Returns the most significant 64 bits of this UUID's 128 bit value.
at line 397
string
getMostSignificantBitsHex()
Returns the most significant 64 bits of this UUID's 128 bit value.
at line 431
int
getNode()
Returns the node value associated with this UUID
For UUID version 1, the node field consists of an IEEE 802 MAC address, usually the host address. For systems with multiple IEEE 802 addresses, any available one can be used. The lowest addressed octet (octet number 10) contains the global/local bit and the unicast/multicast bit, and is the first octet of the address transmitted on an 802.3 LAN.
For systems with no IEEE address, a randomly or pseudo-randomly generated value may be used; see RFC 4122, Section 4.5. The multicast bit must be set in such addresses, in order that they will never conflict with addresses obtained from network cards.
For UUID version 3 or 5, the node field is a 48-bit value constructed from a name as described in RFC 4122, Section 4.3.
For UUID version 4, the node field is a randomly or pseudo-randomly generated 48-bit value as described in RFC 4122, Section 4.4.
at line 436
string
getNodeHex()
Returns the node value associated with this UUID
For UUID version 1, the node field consists of an IEEE 802 MAC address, usually the host address. For systems with multiple IEEE 802 addresses, any available one can be used. The lowest addressed octet (octet number 10) contains the global/local bit and the unicast/multicast bit, and is the first octet of the address transmitted on an 802.3 LAN.
For systems with no IEEE address, a randomly or pseudo-randomly generated value may be used; see RFC 4122, Section 4.5. The multicast bit must be set in such addresses, in order that they will never conflict with addresses obtained from network cards.
For UUID version 3 or 5, the node field is a 48-bit value constructed from a name as described in RFC 4122, Section 4.3.
For UUID version 4, the node field is a randomly or pseudo-randomly generated 48-bit value as described in RFC 4122, Section 4.4.
at line 447
int
getTimeHiAndVersion()
Returns the high field of the timestamp multiplexed with the version number (bits 49-64 of the UUID).
at line 452
string
getTimeHiAndVersionHex()
Returns the high field of the timestamp multiplexed with the version number (bits 49-64 of the UUID).
at line 462
int
getTimeLow()
Returns the low field of the timestamp (the first 32 bits of the UUID).
at line 467
string
getTimeLowHex()
Returns the low field of the timestamp (the first 32 bits of the UUID).
at line 477
int
getTimeMid()
Returns the middle field of the timestamp (bits 33-48 of the UUID).
at line 482
string
getTimeMidHex()
Returns the middle field of the timestamp (bits 33-48 of the UUID).
at line 503
int
getTimestamp()
Returns the timestamp value associated with this UUID.
The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.
The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
at line 512
string
getTimestampHex()
Returns the timestamp value associated with this UUID.
The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.
The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
at line 526
string
getUrn()
Returns the string representation of the UUID as a URN.
at line 531
int
getVariant()
Returns the variant number associated with this UUID.
The variant number describes the layout of the UUID. The variant number has the following meaning:
- 0 - Reserved for NCS backward compatibility
- 2 - The RFC 4122 variant (used by this class)
- 6 - Reserved, Microsoft Corporation backward compatibility
- 7 - Reserved for future definition
at line 547
int|null
getVersion()
Returns the version number associated with this UUID.
The version number describes how this UUID was generated and has the following meaning:
- 1 - Time-based UUID
- 2 - DCE security UUID
- 3 - Name-based UUID hashed with MD5
- 4 - Randomly generated UUID
- 5 - Name-based UUID hashed with SHA-1
Returns null if this UUID is not an RFC 4122 variant, since version is only meaningful for this variant.
at line 556
string
toString()
Converts this UUID into a string representation.
at line 566
static UuidFactoryInterface
getFactory()
Returns the currently set factory used to create UUIDs.
at line 580
static
setFactory(UuidFactoryInterface $factory)
Sets the factory used to create UUIDs.
at line 591
static UuidInterface
fromBytes(string $bytes)
Creates a UUID from a byte string.
at line 602
static UuidInterface
fromString(string $name)
Creates a UUID from the string standard representation.
at line 613
static UuidInterface
fromInteger(string $integer)
Creates a UUID from a 128-bit integer string.
at line 624
static boolean
isValid(string $uuid)
Check if a string is a valid UUID.
at line 649
static UuidInterface
uuid1(int|string $node = null, int $clockSeq = null)
Generate a version 1 UUID from a host ID, sequence number, and the current time.
at line 662
static UuidInterface
uuid3(string $ns, string $name)
Generate a version 3 UUID based on the MD5 hash of a namespace identifier (which is a UUID) and a name (which is a string).
at line 672
static UuidInterface
uuid4()
Generate a version 4 (random) UUID.
at line 685
static UuidInterface
uuid5(string $ns, string $name)
Generate a version 5 UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).