/phpuuid.php
PHP | 141 lines | 77 code | 11 blank | 53 comment | 2 complexity | 1ed55fea60cb920ab0b3613745bdefb5 MD5 | raw file
Possible License(s): GPL-3.0
- <?php
- /*
- * phpUUID - pure PHP UUID implementation
- * Copyright (C) 2011 Daniel J. Lauk
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-
- /**
- * This file contains the UUID class.
- */
-
-
- /**
- * A simple UUID class.
- *
- * The class is based on the information in RFC 4122. There is one notable
- * difference though, which is the 'node' field:
- * According to the RFC the node field is 48 bits long (6 octets). We split
- * it up into a high (16 bits) and low (32 bits) part, so that 32-bit
- * machines can easily deal with it.
- *
- * @see http://tools.ietf.org/html/rfc4122
- */
- class UUID {
-
- protected $timeLow;
- protected $timeMid;
- protected $timeHighAndVersion;
- protected $clockSeqHighAndReserved;
- protected $clockSeqLow;
- protected $nodeHigh;
- protected $nodeLow;
-
- /**
- * Create a UUID object from a string.
- *
- * If the string is not in the correct format (e.g.
- * '6ba7b810-9dad-11d1-80b4-00c04fd430c8'), an `InvalidArgumentException`
- * will be thrown.
- *
- * @param string $str The UUID string
- * @return UUID The UUID object
- */
- public static function fromString($str) {
- $matches = null;
- $count = preg_match('/^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{4})([0-9a-f]{8})$/i', $str, $matches);
- if ($count === 0) throw new InvalidArgumentException(
- 'Not a valid UUID: ' . $str);
- $i = 1;
- $uuid = new UUID();
- $uuid->timeLow = UUID::hexToInt($matches[$i++]);
- $uuid->timeMid = UUID::hexToInt($matches[$i++]);
- $uuid->timeHighAndVersion = UUID::hexToInt($matches[$i++]);
- $uuid->clockSeqHighAndReserved = UUID::hexToInt($matches[$i++]);
- $uuid->clockSeqLow = UUID::hexToInt($matches[$i++]);
- $uuid->nodeHigh = UUID::hexToInt($matches[$i++]);
- $uuid->nodeLow = UUID::hexToInt($matches[$i++]);
-
- return $uuid;
- }
-
- private static function hexToInt($str) {
- $rc = 0;
- foreach (str_split($str) as $c) {
- switch ($c) {
- case '0': $x = 0; break;
- case '1': $x = 1; break;
- case '2': $x = 2; break;
- case '3': $x = 3; break;
- case '4': $x = 4; break;
- case '5': $x = 5; break;
- case '6': $x = 6; break;
- case '7': $x = 7; break;
- case '8': $x = 8; break;
- case '9': $x = 9; break;
- case 'a':
- case 'A': $x = 10; break;
- case 'b':
- case 'B': $x = 11; break;
- case 'c':
- case 'C': $x = 12; break;
- case 'd':
- case 'D': $x = 13; break;
- case 'e':
- case 'E': $x = 14; break;
- case 'f':
- case 'F': $x = 15; break;
- default: throw new IvalidArgumentException(
- "Invalid hex string: $str");
- }
- $rc = $rc << 4 | $x;
- }
- return $rc;
- }
-
- /**
- * Create a new UUID based on (pseudo) random numbers.
- *
- * This will set the version field on the UUID correctly.
- *
- * @return UUID A newly created (pseudo) random UUID.
- */
- public static function random() {
- $uuid = new UUID();
- $uuid->timeLow = mt_rand(0, 0xffff) << 16 | mt_rand(0, 0xffff);
- $uuid->timeMid = mt_rand(0, 0xffff);
- $uuid->timeHighAndVersion = mt_rand(0, 0xffff) & 0x0fff | 0x4000;
- $uuid->clockSeqHighAndReserved = mt_rand(0, 0xff) & 0x3f | 0xbf;
- $uuid->clockSeqLow = mt_rand(0, 0xff);
- $uuid->nodeHigh = mt_rand(0, 0xffff);
- $uuid->nodeLow = mt_rand(0, 0xffff) << 16 | mt_rand(0, 0xffff);
- return $uuid;
- }
-
- /**
- * Create a string representation of this UUID object.
- *
- * @return string The UUID string.
- */
- public function toString() {
- $str = sprintf('%08x-%04x-%04x-%02x%02x-%04x%08x',
- $this->timeLow, $this->timeMid,
- $this->timeHighAndVersion, $this->clockSeqHighAndReserved,
- $this->clockSeqLow, $this->nodeHigh, $this->nodeLow);
- return $str;
- }
-
- }