mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
Initial commit; supports UUID version 1 generation.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
.DS_Store
|
||||
phpunit.xml
|
||||
composer.lock
|
||||
build
|
||||
vendor
|
||||
*.phar
|
||||
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2012 Ben Ramsey <http://benramsey.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,5 @@
|
||||
# Rhumsaa\Uuid
|
||||
|
||||
A PHP 5.3+ library for generating and working with
|
||||
[RFC 4122](http://tools.ietf.org/html/rfc4122) version 1, 3, 4, and 5
|
||||
universally unique identifiers (UUID).
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "Rhumsaa/Uuid",
|
||||
"description": "A PHP 5.3+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).",
|
||||
"type": "library",
|
||||
"keywords": ["uuid", "identifier", "guid"],
|
||||
"homepage": "https://github.com/ramsey/uuid",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ben Ramsey",
|
||||
"homepage": "http://benramsey.com"
|
||||
}
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/ramsey/uuid/issues",
|
||||
"source": "https://github.com/ramsey/uuid"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {"Rhumsaa\\Uuid": "library/"}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the Rhumsaa\Uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) 2012 Ben Ramsey <http://benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace Rhumsaa\Uuid;
|
||||
|
||||
/**
|
||||
* Thrown to indicate that the requested operation is not supported.
|
||||
*/
|
||||
class UnsupportedOperationException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,520 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the Rhumsaa\Uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) 2012 Ben Ramsey <http://benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace Rhumsaa\Uuid;
|
||||
|
||||
/**
|
||||
* Represents a universally unique identifier (UUID)
|
||||
*
|
||||
* @see http://tools.ietf.org/html/rfc4122
|
||||
* @see http://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||
*/
|
||||
final class Uuid
|
||||
{
|
||||
/**
|
||||
* When this namespace is specified, the name string is a fully-qualified domain name.
|
||||
* @see http://tools.ietf.org/html/rfc4122#appendix-C
|
||||
*/
|
||||
const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
||||
|
||||
/**
|
||||
* When this namespace is specified, the name string is a URL.
|
||||
* @see http://tools.ietf.org/html/rfc4122#appendix-C
|
||||
*/
|
||||
const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
||||
|
||||
/**
|
||||
* When this namespace is specified, the name string is an ISO OID.
|
||||
* @see http://tools.ietf.org/html/rfc4122#appendix-C
|
||||
*/
|
||||
const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
|
||||
|
||||
/**
|
||||
* When this namespace is specified, the name string is an X.500 DN in DER or a text output format.
|
||||
* @see http://tools.ietf.org/html/rfc4122#appendix-C
|
||||
*/
|
||||
const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
|
||||
|
||||
/**
|
||||
* The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.7
|
||||
*/
|
||||
const NIL = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
/**
|
||||
* Reserved for NCS compatibility.
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.1
|
||||
*/
|
||||
const RESERVED_NCS = 0;
|
||||
|
||||
/**
|
||||
* Specifies the UUID layout given in RFC 4122.
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.1
|
||||
*/
|
||||
const RFC_4122 = 2;
|
||||
|
||||
/**
|
||||
* Reserved for Microsoft compatibility.
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.1
|
||||
*/
|
||||
const RESERVED_MICROSOFT = 6;
|
||||
|
||||
/**
|
||||
* Reserved for future definition.
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.1
|
||||
*/
|
||||
const RESERVED_FUTURE = 7;
|
||||
|
||||
/**
|
||||
* The least significant 64 bits of this UUID's 128 bit value
|
||||
* @var int
|
||||
*/
|
||||
protected $leastSignificantBits = 0;
|
||||
|
||||
/**
|
||||
* The most significant 64 bits of this UUID's 128 bit value
|
||||
* @var int
|
||||
*/
|
||||
protected $mostSignificantBits = 0;
|
||||
|
||||
/**
|
||||
* Creates a universally unique identifier (UUID) from the most significant
|
||||
* bits and least significant bits.
|
||||
*
|
||||
* Protected to prevent direct instantiation. Use static methods to create
|
||||
* UUIDs.
|
||||
*
|
||||
* @param int $mostSignificantBits The most significant 64 bits of this UUID's 128 bit value
|
||||
* @param int $leastSignificantBits The least significant 64 bits of this UUID's 128 bit value
|
||||
*/
|
||||
protected function __construct($mostSignificantBits, $leastSignificantBits)
|
||||
{
|
||||
$this->mostSignificantBits = $mostSignificantBits;
|
||||
$this->leastSignificantBits = $leastSignificantBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this UUID object to a string when the object is used in any
|
||||
* string context
|
||||
*
|
||||
* @return string
|
||||
* @see http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
public function compareTo(Uuid $uuid)
|
||||
{
|
||||
}
|
||||
|
||||
public function equals(Uuid $uuid)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the high field of the clock sequence multiplexed with the variant
|
||||
* (bits 65-72 of the UUID).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getClockSeqHiAndReserved()
|
||||
{
|
||||
return ($this->getLeastSignificantBits() >> 56) & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getClockSeqLow()
|
||||
{
|
||||
return ($this->getLeastSignificantBits() >> 48) & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return int
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.5
|
||||
*/
|
||||
public function getClockSequence()
|
||||
{
|
||||
return (($this->getClockSeqHiAndReserved() & 0x3f) << 8)
|
||||
| $this->getClockSeqLow();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return \DateTime
|
||||
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
|
||||
*/
|
||||
public function getDateTime()
|
||||
{
|
||||
if ($this->getVersion() != 1) {
|
||||
throw new UnsupportedOperationException('Not a time-based UUID');
|
||||
}
|
||||
|
||||
$unixTime = ($this->getTimestamp() - 0x01b21dd213814000) / 1e7;
|
||||
return new \DateTime('@' . number_format($unixTime, 0, '', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the least significant 64 bits of this UUID's 128 bit value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLeastSignificantBits()
|
||||
{
|
||||
return $this->leastSignificantBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most significant 64 bits of this UUID's 128 bit value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getMostSignificantBits()
|
||||
{
|
||||
return $this->mostSignificantBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return int
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.6
|
||||
*/
|
||||
public function getNode()
|
||||
{
|
||||
return $this->getLeastSignificantBits() & 0x0000ffffffffffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the high field of the timestamp multiplexed with the version
|
||||
* number (bits 49-64 of the UUID).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeHiAndVersion()
|
||||
{
|
||||
return $this->getMostSignificantBits() & 0xffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the timestamp (the first 32 bits of the UUID).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeLow()
|
||||
{
|
||||
return ($this->getMostSignificantBits() >> 32) & 0xffffffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeMid()
|
||||
{
|
||||
return ($this->getMostSignificantBits() >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return int
|
||||
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.4
|
||||
*/
|
||||
public function getTimestamp()
|
||||
{
|
||||
if ($this->getVersion() != 1) {
|
||||
throw new UnsupportedOperationException('Not a time-based UUID');
|
||||
}
|
||||
|
||||
return ($this->getTimeHiAndVersion() & 0x0fff) << 48
|
||||
| ($this->getTimeMid() & 0xffff) << 32
|
||||
| $this->getTimeLow();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return int
|
||||
* @see http://tools.ietf.org/html/rfc4122#section-4.1.1
|
||||
*/
|
||||
public function getVariant()
|
||||
{
|
||||
if (0 === ($this->getLeastSignificantBits() & (0x8000 << 48))) {
|
||||
$variant = self::RESERVED_NCS;
|
||||
} elseif (0 === ($this->getLeastSignificantBits() & (0x4000 << 48))) {
|
||||
$variant = self::RFC_4122;
|
||||
} elseif (0 === ($this->getLeastSignificantBits() & (0x2000 << 48))) {
|
||||
$variant = self::RESERVED_MICROSOFT;
|
||||
} else {
|
||||
$variant = self::RESERVED_FUTURE;
|
||||
}
|
||||
|
||||
return $variant;
|
||||
}
|
||||
|
||||
/**
|
||||
* The version number associated with this UUID. The version
|
||||
* number describes how this UUID was generated.
|
||||
*
|
||||
* The version number 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
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return (($this->getMostSignificantBits() >> 12) & 0x0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this UUID into a string representation
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return ($this->digits($this->getMostSignificantBits() >> 32, 8) . '-' .
|
||||
$this->digits($this->getMostSignificantBits() >> 16, 4) . '-' .
|
||||
$this->digits($this->getMostSignificantBits(), 4) . '-' .
|
||||
$this->digits($this->getLeastSignificantBits() >> 48, 4) . '-' .
|
||||
$this->digits($this->getLeastSignificantBits(), 12));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a UUID from the string standard representation as described
|
||||
* in the toString() method.
|
||||
*
|
||||
* @param string $name A string that specifies a UUID
|
||||
* @return Uuid
|
||||
*/
|
||||
public static function fromString($name)
|
||||
{
|
||||
$components = explode('-', $name);
|
||||
|
||||
if (count($components) != 5) {
|
||||
throw new \InvalidArgumentException('Invalid UUID string: ' . $name);
|
||||
}
|
||||
|
||||
// Put together most significant bits
|
||||
$msb = hexdec($components[0]);
|
||||
$msb <<= 16;
|
||||
$msb |= hexdec($components[1]);
|
||||
$msb <<= 16;
|
||||
$msb |= hexdec($components[2]);
|
||||
|
||||
// Put together least significant bits
|
||||
$lsb = hexdec($components[3]);
|
||||
$lsb <<= 48;
|
||||
$lsb |= hexdec($components[4]);
|
||||
|
||||
return new self($msb, $lsb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a UUID from a host ID, sequence number, and the current time.
|
||||
* If $node is not given, getMacAddress() is used to obtain the hardware
|
||||
* address. If $clockSeq is given, it is used as the sequence number;
|
||||
* otherwise a random 14-bit sequence number is chosen.
|
||||
*
|
||||
* @param int $node A 48-bit number representing the hardware address.
|
||||
* @param int $clockSeq 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 Uuid
|
||||
*/
|
||||
public static function uuid1($node = null, $clockSeq = null)
|
||||
{
|
||||
if ($node === null) {
|
||||
$node = self::getMacAddress();
|
||||
}
|
||||
|
||||
if ($clockSeq === null) {
|
||||
// Not using "stable storage"; see RFC 4122, Section 4.2.1.1
|
||||
$clockSeq = mt_rand(0, 1 << 14);
|
||||
}
|
||||
|
||||
// Create a 60-bit time value as a count of 100-nanosecond intervals
|
||||
// since 00:00:00.00, 15 October 1582
|
||||
$timeOfDay = gettimeofday();
|
||||
$uuidTime = ($timeOfDay['sec'] * 10000000) + ($timeOfDay['usec'] * 10) + 0x01b21dd213814000;
|
||||
|
||||
// Get the time parts for putting together most significant bits
|
||||
$timeLow = $uuidTime & 0xffffffff;
|
||||
$timeMid = ($uuidTime >> 32) & 0xffff;
|
||||
$timeHi = ($uuidTime >> 48) & 0x0fff;
|
||||
|
||||
// Put together most significant bits
|
||||
$msb = ($timeLow << 32) | ($timeMid << 16) | $timeHi;
|
||||
|
||||
// Set the version number to 1
|
||||
$msb &= ~(0xf000);
|
||||
$msb |= 1 << 12;
|
||||
|
||||
// Put together least significant bits
|
||||
$lsb = ($clockSeq << 48) | $node;
|
||||
|
||||
// Set the variant to RFC 4122
|
||||
$lsb &= ~(0xc000 << 48);
|
||||
$lsb |= 0x8000 << 48;
|
||||
|
||||
return new self($msb, $lsb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a UUID based on the MD5 hash of a namespace identifier (which
|
||||
* is a UUID) and a name (which is a string).
|
||||
*
|
||||
* @return Uuid
|
||||
*/
|
||||
public static function uuid3($ns, $name)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random UUID.
|
||||
*
|
||||
* @return Uuid
|
||||
*/
|
||||
public static function uuid4()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a UUID based on the SHA-1 hash of a namespace identifier (which
|
||||
* is a UUID) and a name (which is a string).
|
||||
*
|
||||
* @return Uuid
|
||||
*/
|
||||
public static function uuid5($ns, $name)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns $value represented by the specified number of hex $digits.
|
||||
*
|
||||
* @param int $value The value to process (in integer form)
|
||||
* @param int $digits The number of hex digits to return
|
||||
* @return string
|
||||
*/
|
||||
protected function digits($value, $digits)
|
||||
{
|
||||
$hi = (1 << ($digits * 4));
|
||||
return substr(dechex($hi | ($value & ($hi - 1))), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hardware address as a 48-bit positive integer. If all attempts to
|
||||
* obtain the hardware address fail, we choose a random 48-bit number with
|
||||
* its eighth bit set to 1 as recommended in RFC 4122. "Hardware address"
|
||||
* means the MAC address of a network interface, and on a machine with
|
||||
* multiple network interfaces the MAC address of any one of them may be
|
||||
* returned.
|
||||
*
|
||||
* @return string
|
||||
* @todo Needs evaluation and possibly modification to ensure this works
|
||||
* well across multiple platforms.
|
||||
*/
|
||||
protected static function getMacAddress()
|
||||
{
|
||||
// If we're on Windows, use ipconfig; otherwise use ifconfig
|
||||
if (strtoupper(substr(php_uname('a'), 0, 3)) == 'WIN') {
|
||||
$ifconfig = `ipconfig /all`;
|
||||
} else {
|
||||
$ifconfig = `ifconfig`;
|
||||
}
|
||||
|
||||
$mac = null;
|
||||
$pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/';
|
||||
$matches = array();
|
||||
|
||||
// Search the ifconfig output for all MAC addresses and return
|
||||
// the first one found
|
||||
if (preg_match_all($pattern, $ifconfig, $matches, PREG_PATTERN_ORDER)) {
|
||||
$mac = $matches[1][0];
|
||||
$mac = str_replace(':', '', $mac);
|
||||
$mac = str_replace('-', '', $mac);
|
||||
$mac = hexdec($mac);
|
||||
}
|
||||
|
||||
// If we couldn't find a MAC address, generate a random 48-bit number
|
||||
// with its eighth bit set to 1.
|
||||
if ($mac === null) {
|
||||
}
|
||||
|
||||
return $mac;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite>
|
||||
<directory>./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">./library</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="coverage-html" target="build/coverage" title="Rhumsaa\Uuid"
|
||||
charset="UTF-8" yui="true" highlight="true"
|
||||
lowUpperBound="35" highLowerBound="70"/>
|
||||
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||
<log type="junit" target="build/logs/junit.xml"
|
||||
logIncompleteSkipped="false"/>
|
||||
</logging>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,322 @@
|
||||
<?php
|
||||
namespace Rhumsaa\Uuid;
|
||||
|
||||
class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::fromString
|
||||
* @covers Rhumsaa\Uuid\Uuid::__construct
|
||||
*/
|
||||
public function testFromString()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::fromString
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid UUID string:
|
||||
*/
|
||||
public function testFromStringWithInvalidUuidString()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReserved
|
||||
*/
|
||||
public function testGetClockSeqHiAndReserved()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(155, $uuid->getClockSeqHiAndReserved());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLow
|
||||
*/
|
||||
public function testGetClockSeqLow()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(33, $uuid->getClockSeqLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSequence
|
||||
*/
|
||||
public function testGetClockSequence()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(6945, $uuid->getClockSequence());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getDateTime
|
||||
*/
|
||||
public function testGetDateTime()
|
||||
{
|
||||
// Check a recent date
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
||||
$this->assertEquals('Wed, 04 Jul 2012 02:14:34 +0000', $uuid->getDateTime()->format('r'));
|
||||
|
||||
// Check an old date
|
||||
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
||||
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
||||
$this->assertEquals('Sun, 16 Oct 1582 16:34:04 +0000', $uuid->getDateTime()->format('r'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getDateTime
|
||||
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
||||
* @expectedExceptionMessage Not a time-based UUID
|
||||
*/
|
||||
public function testGetDateTimeFromNonVersion1Uuid()
|
||||
{
|
||||
// Using a version 4 UUID to test
|
||||
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
||||
$date = $uuid->getDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBits
|
||||
*/
|
||||
public function testGetLeastSignificantBits()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(-7268519526968550810, $uuid->getLeastSignificantBits());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBits
|
||||
*/
|
||||
public function testGetMostSignificantBits()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(-40659180767604255, $uuid->getMostSignificantBits());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getNode
|
||||
*/
|
||||
public function testGetNode()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(8796630719078, $uuid->getNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersion
|
||||
*/
|
||||
public function testGetTimeHiAndVersion()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(4577, $uuid->getTimeHiAndVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeLow
|
||||
*/
|
||||
public function testGetTimeLow()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(4285500592, $uuid->getTimeLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeMid
|
||||
*/
|
||||
public function testGetTimeMid()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(50557, $uuid->getTimeMid());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
||||
*/
|
||||
public function testGetTimestamp()
|
||||
{
|
||||
// Check for a recent date
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(135606608744910000, $uuid->getTimestamp());
|
||||
|
||||
// Check for an old date
|
||||
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
||||
$this->assertEquals(1460440000000, $uuid->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
||||
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
||||
* @expectedExceptionMessage Not a time-based UUID
|
||||
*/
|
||||
public function testGetTimestampFromNonVersion1Uuid()
|
||||
{
|
||||
// Using a version 4 UUID to test
|
||||
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
||||
$ts = $uuid->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
||||
*/
|
||||
public function testGetVariantForReservedNcs()
|
||||
{
|
||||
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-0b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid1->getVariant());
|
||||
|
||||
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-1b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid2->getVariant());
|
||||
|
||||
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-2b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid3->getVariant());
|
||||
|
||||
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-3b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid4->getVariant());
|
||||
|
||||
$uuid5 = Uuid::fromString('ff6f8cb0-c57d-11e1-4b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid5->getVariant());
|
||||
|
||||
$uuid6 = Uuid::fromString('ff6f8cb0-c57d-11e1-5b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid6->getVariant());
|
||||
|
||||
$uuid7 = Uuid::fromString('ff6f8cb0-c57d-11e1-6b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid7->getVariant());
|
||||
|
||||
$uuid8 = Uuid::fromString('ff6f8cb0-c57d-11e1-7b21-0800200c9a66');
|
||||
$this->assertEquals(0, $uuid8->getVariant());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
||||
*/
|
||||
public function testGetVariantForRfc4122()
|
||||
{
|
||||
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-8b21-0800200c9a66');
|
||||
$this->assertEquals(2, $uuid1->getVariant());
|
||||
|
||||
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(2, $uuid2->getVariant());
|
||||
|
||||
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-ab21-0800200c9a66');
|
||||
$this->assertEquals(2, $uuid3->getVariant());
|
||||
|
||||
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-bb21-0800200c9a66');
|
||||
$this->assertEquals(2, $uuid4->getVariant());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
||||
*/
|
||||
public function testGetVariantForReservedMicrosoft()
|
||||
{
|
||||
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-cb21-0800200c9a66');
|
||||
$this->assertEquals(6, $uuid1->getVariant());
|
||||
|
||||
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-db21-0800200c9a66');
|
||||
$this->assertEquals(6, $uuid2->getVariant());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVariant
|
||||
*/
|
||||
public function testGetVariantForReservedFuture()
|
||||
{
|
||||
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-eb21-0800200c9a66');
|
||||
$this->assertEquals(7, $uuid1->getVariant());
|
||||
|
||||
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-fb21-0800200c9a66');
|
||||
$this->assertEquals(7, $uuid2->getVariant());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
||||
*/
|
||||
public function testGetVersionForVersion1()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals(1, $uuid->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
||||
*/
|
||||
public function testGetVersionForVersion2()
|
||||
{
|
||||
$this->markTestIncomplete('This test is not implemented yet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
||||
*/
|
||||
public function testGetVersionForVersion3()
|
||||
{
|
||||
$uuid = Uuid::fromString('6fa459ea-ee8a-3ca4-894e-db77e160355e');
|
||||
$this->assertEquals(3, $uuid->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
||||
*/
|
||||
public function testGetVersionForVersion4()
|
||||
{
|
||||
$uuid = Uuid::fromString('6fabf0bc-603a-42f2-925b-d9f779bd0032');
|
||||
$this->assertEquals(4, $uuid->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getVersion
|
||||
*/
|
||||
public function testGetVersionForVersion5()
|
||||
{
|
||||
$uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d');
|
||||
$this->assertEquals(5, $uuid->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::toString
|
||||
* @covers Rhumsaa\Uuid\Uuid::__toString
|
||||
* @covers Rhumsaa\Uuid\Uuid::digits
|
||||
*/
|
||||
public function testToString()
|
||||
{
|
||||
// Check with a recent date
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
|
||||
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', sprintf('%s', $uuid));
|
||||
|
||||
// Check with an old date
|
||||
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
||||
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', $uuid->toString());
|
||||
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
|
||||
}
|
||||
|
||||
/**
|
||||
* covers Rhumsaa\Uuid\Uuid::uuid1
|
||||
*/
|
||||
public function testUuid1()
|
||||
{
|
||||
$uuid = Uuid::uuid1();
|
||||
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
||||
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
||||
$this->assertEquals(2, $uuid->getVariant());
|
||||
$this->assertEquals(1, $uuid->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* covers Rhumsaa\Uuid\Uuid::uuid1
|
||||
*/
|
||||
public function testUuid1WithNodeAndClockSequence()
|
||||
{
|
||||
$uuid = Uuid::uuid1(0x0800200c9a66, 0x1669);
|
||||
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
|
||||
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
|
||||
$this->assertEquals(2, $uuid->getVariant());
|
||||
$this->assertEquals(1, $uuid->getVersion());
|
||||
$this->assertEquals(5737, $uuid->getClockSequence());
|
||||
$this->assertEquals(8796630719078, $uuid->getNode());
|
||||
$this->assertEquals('9669-0800200c9a66', substr($uuid->toString(), 19));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
|
||||
// Ensure that composer has installed all dependencies
|
||||
if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.lock')) {
|
||||
die("Dependencies must be installed using composer:\n\nphp composer.phar install --dev\n\n"
|
||||
. "See http://getcomposer.org for help with installing composer\n");
|
||||
}
|
||||
|
||||
// Include the Composer autoloader
|
||||
include realpath(dirname(__FILE__) . '/../vendor/autoload.php');
|
||||
|
||||
Reference in New Issue
Block a user