webCOMAND

\io_comand_util\uuid

Class for producing and working with Universally unique identifiers (UUIDs).

UUID Storage Formats

This class works with UUIDs in the following formats.

  • Binary - A string that contains a 128-bit (16-byte) number.  This is the most storage-efficient and often the most efficient for comparison, which makes it a good format for database storage.
  • Hex String - A string that contains a (32-byte) text representation of a UUID without dashes (ie. e30e7186bfc811e68f7c6cdf1cea4597).  Useful for including in URLs and other text-based machine readable transmissions.
  • String - A string that contains a (36-byte) text representation of a UUID with dashes (ie. e30e7186-bfc8-11e6-8f7c-6cdf1cea4597).  This format is easiest to read because it is visually broken up into chunks.

UUID Versions

UUID versions v1, v3, v4 and v5 are supported, but v1 is generally recommended and used throughout webCOMAND when possible because they are unique per network adapter and creation time reducing the likelihood of collisions.  When network adapter information is not available, v4 is used for simplicity and speed.

When generating a single UUID for a specific repository object, consider using v5, with the repository root object's UUID for the namespace and object OID for the name.
$uuid = uuid::v5($object->repo()->get_root()->UUID, $object->OID);
  • v1 - Date-time and MAC Address
  • v3 - Namespace and Name (MD5)
  • v4 - Random
  • v5 - Namespace and Name (SHA1)

Example

// generate v1 UUID based on MAC Address retrieved from database
$pdo = new PDO('mysql:host=localhost;dbname=db', $username, $password);
$uuid = \io_comand_util\uuid::v1($pdo);
echo("v1 UUID=" . \io_comand_util\uuid::any_to_string($uuid) . "\n");

// generate v3 UUID based on a namespace and name (MD5)
$repo = \comand::repo();
$root = $repo->get_root();
$namespace = $root->UUID;
$name = 'Unique name';
$uuid = \io_comand_util\uuid::v3($namespace, $name);
echo("v3 UUID=" . \io_comand_util\uuid::any_to_string($uuid) . "\n");

// generate a psuedo-random UUID
$uuid = \io_comand_util\uuid::v4();
echo("v4 UUID=" . \io_comand_util\uuid::any_to_string($uuid) . "\n");

// generate v5 UUID based on a namespace and name (SHA1)
$uuid = \io_comand_util\uuid::v5($namespace, $name);
echo("v5 UUID=" . \io_comand_util\uuid::any_to_string($uuid) . "\n");