UuidV2
DCE Security version, or version 2, UUIDs include local domain identifier, local ID for the specified domain, and node values that are combined into a 128-bit unsigned integer
It is important to note that a version 2 UUID suffers from some loss of fidelity of the timestamp, due to replacing the time_low field with the local identifier. When constructing the timestamp value for date purposes, we replace the local identifier bits with zeros. As a result, the timestamp can be off by a range of 0 to 429.4967295 seconds (or 7 minutes, 9 seconds, and 496730 microseconds).
Astute observers might note this value directly corresponds to 2^32 - 1, or 0xffffffff. The local identifier is 32-bits, and we have set each of these bits to 0, so the maximum range of timestamp drift is 0x00000000 to 0xffffffff (counted in 100-nanosecond intervals).
- Full name:
\Ramsey\Uuid\Rfc4122\UuidV2
- Parent class:
\Ramsey\Uuid\Uuid
- This class is marked as final and can't be subclassed
- This class implements:
\Ramsey\Uuid\Rfc4122\UuidInterface
- This class is a Final class
See Also:
- https://publications.opengroup.org/c311 - DCE 1.1: Authentication and Security Services
- https://publications.opengroup.org/c706 - DCE 1.1: Remote Procedure Call
- https://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01 - DCE 1.1: Auth & Sec, §5.2.1.1
- https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 - DCE 1.1: Auth & Sec, §11.5.1.1
- https://pubs.opengroup.org/onlinepubs/9629399/apdxa.htm - DCE 1.1: RPC, Appendix A
- https://github.com/google/uuid - Go package for UUIDs (includes DCE implementation)
Methods
__construct
Creates a version 2 (DCE Security) UUID
public __construct(\Ramsey\Uuid\Rfc4122\FieldsInterface $fields, \Ramsey\Uuid\Converter\NumberConverterInterface $numberConverter, \Ramsey\Uuid\Codec\CodecInterface $codec, \Ramsey\Uuid\Converter\TimeConverterInterface $timeConverter): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$fields |
\Ramsey\Uuid\Rfc4122\FieldsInterface | The fields from which to construct a UUID |
$numberConverter |
\Ramsey\Uuid\Converter\NumberConverterInterface | The number converter to use for converting hex values to/from integers |
$codec |
\Ramsey\Uuid\Codec\CodecInterface | The codec to use when encoding or decoding UUID strings |
$timeConverter |
\Ramsey\Uuid\Converter\TimeConverterInterface | The time converter to use for converting timestamps extracted from a UUID to unix timestamps |
getLocalDomain
Returns the local domain used to create this version 2 UUID
getLocalDomainName
Returns the string name of the local domain
getLocalIdentifier
Returns the local identifier for the domain used to create this version 2 UUID
Inherited methods
getClockSeqHiAndReserved
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getClockSeqHiAndReservedHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getClockSeqLow
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getClockSeqLowHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getClockSequence
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getClockSequenceHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getNumberConverter
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getDateTime
Returns a DateTimeInterface object representing the timestamp associated with the UUID
Return Value:
A PHP DateTimeImmutable instance representing the timestamp of a time-based UUID
getFieldsHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getLeastSignificantBits
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getLeastSignificantBitsHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getMostSignificantBits
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getMostSignificantBitsHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getNode
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getNodeHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeHiAndVersion
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeHiAndVersionHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeLow
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeLowHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeMid
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimeMidHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimestamp
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getTimestampHex
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getVariant
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
getVersion
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
__construct
Creates a universally unique identifier (UUID) from an array of fields
public __construct(\Ramsey\Uuid\Rfc4122\FieldsInterface $fields, \Ramsey\Uuid\Converter\NumberConverterInterface $numberConverter, \Ramsey\Uuid\Codec\CodecInterface $codec, \Ramsey\Uuid\Converter\TimeConverterInterface $timeConverter): mixed
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/');
Parameters:
Parameter | Type | Description |
---|---|---|
$fields |
\Ramsey\Uuid\Rfc4122\FieldsInterface | The fields from which to construct a UUID |
$numberConverter |
\Ramsey\Uuid\Converter\NumberConverterInterface | The number converter to use for converting hex values to/from integers |
$codec |
\Ramsey\Uuid\Codec\CodecInterface | The codec to use when encoding or decoding UUID strings |
$timeConverter |
\Ramsey\Uuid\Converter\TimeConverterInterface | The time converter to use for converting timestamps extracted from a UUID to unix timestamps |
__toString
Casts the UUID to the string standard representation
jsonSerialize
Converts the UUID to a string for JSON serialization
serialize
Converts the UUID to a string for PHP serialization
__serialize
unserialize
Re-constructs the object from its serialized form
Parameters:
Parameter | Type | Description |
---|---|---|
$data |
string | The serialized PHP string to unserialize into a UuidInterface instance |
__unserialize
Parameters:
Parameter | Type | Description |
---|---|---|
$data |
array{bytes?: string} |
compareTo
Returns -1, 0, or 1 if the UUID is less than, equal to, or greater than the other 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.
Parameters:
Parameter | Type | Description |
---|---|---|
$other |
\Ramsey\Uuid\UuidInterface | The UUID to compare |
Return Value:
-1, 0, or 1 if the UUID is less than, equal to, or greater than $other
equals
Returns true if the UUID is equal to the provided 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 the UUID.
Parameters:
Parameter | Type | Description |
---|---|---|
$other |
?object | An object to test for equality with this UUID |
Return Value:
True if the other object is equal to this UUID
getBytes
Returns the binary string representation of the UUID
getFields
Returns the fields that comprise this UUID
getHex
Returns the hexadecimal representation of the UUID
getInteger
Returns the integer representation of the UUID
getUrn
Returns the string standard representation of the UUID as a URN
toString
Returns the string standard representation of the UUID
getFactory
Returns the factory used to create UUIDs
- This method is static.
setFactory
Sets the factory used to create UUIDs
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$factory |
\Ramsey\Uuid\UuidFactoryInterface | A factory that will be used by this class to create UUIDs |
fromBytes
Creates a UUID from a byte string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$bytes |
string | A binary string |
Return Value:
A UuidInterface instance created from a binary string representation
fromString
Creates a UUID from the string standard representation
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$uuid |
string | A hexadecimal string |
Return Value:
A UuidInterface instance created from a hexadecimal string representation
fromDateTime
Creates a UUID from a DateTimeInterface instance
public static fromDateTime(\DateTimeInterface $dateTime, \Ramsey\Uuid\Type\Hexadecimal|null $node = null, int|null $clockSeq = null): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$dateTime |
\DateTimeInterface | The date and time |
$node |
\Ramsey\Uuid\Type\Hexadecimal|null | A 48-bit number representing the hardware address |
$clockSeq |
int|null | A 14-bit number used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes |
Return Value:
A UuidInterface instance that represents a version 1 UUID created from a DateTimeInterface instance
fromHexadecimal
Creates a UUID from the Hexadecimal object
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$hex |
\Ramsey\Uuid\Type\Hexadecimal | Hexadecimal object representing a hexadecimal number |
Return Value:
A UuidInterface instance created from the Hexadecimal object representing a hexadecimal number
fromInteger
Creates a UUID from a 128-bit integer string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$integer |
string | String representation of 128-bit integer |
Return Value:
A UuidInterface instance created from the string representation of a 128-bit integer
isValid
Returns true if the provided string is a valid UUID
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$uuid |
string | A string to validate as a UUID |
Return Value:
True if the string is a valid UUID, false otherwise
uuid1
Returns a version 1 (Gregorian time) UUID from a host ID, sequence number, and the current time
public static uuid1(\Ramsey\Uuid\Type\Hexadecimal|int|string|null $node = null, int|null $clockSeq = null): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$node |
\Ramsey\Uuid\Type\Hexadecimal|int|string|null | A 48-bit number representing the hardware address; this number may be represented as an integer or a hexadecimal string |
$clockSeq |
int|null | A 14-bit number used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes |
Return Value:
A UuidInterface instance that represents a version 1 UUID
uuid2
Returns a version 2 (DCE Security) UUID from a local domain, local identifier, host ID, clock sequence, and the current time
public static uuid2(int $localDomain, \Ramsey\Uuid\Type\Integer|null $localIdentifier = null, \Ramsey\Uuid\Type\Hexadecimal|null $node = null, int|null $clockSeq = null): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$localDomain |
int | The local domain to use when generating bytes, according to DCE Security |
$localIdentifier |
\Ramsey\Uuid\Type\Integer|null | The local identifier for the given domain; this may be a UID or GID on POSIX systems, if the local domain is person or group, or it may be a site-defined identifier if the local domain is org |
$node |
\Ramsey\Uuid\Type\Hexadecimal|null | A 48-bit number representing the hardware address |
$clockSeq |
int|null | A 14-bit number used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes (in a version 2 UUID, the lower 8 bits of this number are replaced with the domain). |
Return Value:
A UuidInterface instance that represents a version 2 UUID
uuid3
Returns a version 3 (name-based) UUID based on the MD5 hash of a namespace ID and a name
public static uuid3(string|\Ramsey\Uuid\UuidInterface $ns, string $name): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$ns |
string|\Ramsey\Uuid\UuidInterface | The namespace (must be a valid UUID) |
$name |
string | The name to use for creating a UUID |
Return Value:
A UuidInterface instance that represents a version 3 UUID
uuid4
Returns a version 4 (random) UUID
- This method is static.
Return Value:
A UuidInterface instance that represents a version 4 UUID
uuid5
Returns a version 5 (name-based) UUID based on the SHA-1 hash of a namespace ID and a name
public static uuid5(string|\Ramsey\Uuid\UuidInterface $ns, string $name): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$ns |
string|\Ramsey\Uuid\UuidInterface | The namespace (must be a valid UUID) |
$name |
string | The name to use for creating a UUID |
Return Value:
A UuidInterface instance that represents a version 5 UUID
uuid6
Returns a version 6 (reordered time) UUID from a host ID, sequence number, and the current time
public static uuid6(\Ramsey\Uuid\Type\Hexadecimal|null $node = null, int|null $clockSeq = null): \Ramsey\Uuid\UuidInterface
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$node |
\Ramsey\Uuid\Type\Hexadecimal|null | A 48-bit number representing the hardware address |
$clockSeq |
int|null | A 14-bit number used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes |
Return Value:
A UuidInterface instance that represents a version 6 UUID
uuid7
Returns a version 7 (Unix Epoch time) UUID
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$dateTime |
\DateTimeInterface|null | An optional date/time from which to create the version 7 UUID. If not provided, the UUID is generated using the current date/time. |
Return Value:
A UuidInterface instance that represents a version 7 UUID
uuid8
Returns a version 8 (custom) UUID
The bytes provided may contain any value according to your application's needs. Be aware, however, that other applications may not understand the semantics of the value.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$bytes |
string | A 16-byte octet string. This is an open blob of data that you may fill with 128 bits of information. Be aware, however, bits 48 through 51 will be replaced with the UUID version field, and bits 64 and 65 will be replaced with the UUID variant. You MUST NOT rely on these bits for your application needs. |
Return Value:
A UuidInterface instance that represents a version 8 UUID
Automatically generated on 2025-03-18