mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-19 16:47:23 +03:00
Providing separate get*Hex() methods for each UUID field
This commit is contained in:
@@ -199,6 +199,17 @@ class Uuid
|
||||
return ($this->getLeastSignificantBits() >> 56) & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the high field of the clock sequence multiplexed with the variant
|
||||
* (bits 65-72 of the UUID).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClockSeqHiAndReservedHex()
|
||||
{
|
||||
return sprintf('%02x', $this->getClockSeqHiAndReserved());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
|
||||
*
|
||||
@@ -209,6 +220,16 @@ class Uuid
|
||||
return ($this->getLeastSignificantBits() >> 48) & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClockSeqLowHex()
|
||||
{
|
||||
return sprintf('%02x', $this->getClockSeqLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the clock sequence value associated with this UUID.
|
||||
*
|
||||
@@ -231,6 +252,16 @@ class Uuid
|
||||
| $this->getClockSeqLow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the clock sequence value associated with this UUID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClockSequenceHex()
|
||||
{
|
||||
return sprintf('%04x', $this->getClockSequence());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a PHP DateTime object representing the timestamp associated
|
||||
* with this UUID.
|
||||
@@ -270,6 +301,24 @@ class Uuid
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the fields of this UUID, with keys named according
|
||||
* to the RFC 4122 names for the fields.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldsHex()
|
||||
{
|
||||
return array(
|
||||
'time_low' => $this->getTimeLowHex(),
|
||||
'time_mid' => $this->getTimeMidHex(),
|
||||
'time_hi_and_version' => $this->getTimeHiAndVersionHex(),
|
||||
'clock_seq_hi_and_reserved' => $this->getClockSeqHiAndReservedHex(),
|
||||
'clock_seq_low' => $this->getClockSeqLowHex(),
|
||||
'node' => $this->getNodeHex(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the least significant 64 bits of this UUID's 128 bit value
|
||||
*
|
||||
@@ -319,6 +368,16 @@ class Uuid
|
||||
return $this->getLeastSignificantBits() & 0x0000ffffffffffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the node value associated with this UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNodeHex()
|
||||
{
|
||||
return sprintf('%012x', $this->getNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the high field of the timestamp multiplexed with the version
|
||||
* number (bits 49-64 of the UUID).
|
||||
@@ -330,6 +389,17 @@ class Uuid
|
||||
return $this->getMostSignificantBits() & 0xffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the high field of the timestamp multiplexed with the version
|
||||
* number (bits 49-64 of the UUID).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTimeHiAndVersionHex()
|
||||
{
|
||||
return sprintf('%04x', $this->getTimeHiAndVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the timestamp (the first 32 bits of the UUID).
|
||||
*
|
||||
@@ -340,6 +410,16 @@ class Uuid
|
||||
return ($this->getMostSignificantBits() >> 32) & 0xffffffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the low field of the timestamp (the first 32 bits of the UUID).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTimeLowHex()
|
||||
{
|
||||
return sprintf('%08x', $this->getTimeLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
|
||||
*
|
||||
@@ -350,6 +430,16 @@ class Uuid
|
||||
return ($this->getMostSignificantBits() >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTimeMidHex()
|
||||
{
|
||||
return sprintf('%04x', $this->getTimeMid());
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp value associated with this UUID
|
||||
*
|
||||
@@ -377,6 +467,21 @@ class Uuid
|
||||
| $this->getTimeLow();
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp value associated with this UUID
|
||||
*
|
||||
* @return string
|
||||
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
|
||||
*/
|
||||
public function getTimestampHex()
|
||||
{
|
||||
if ($this->getVersion() != 1) {
|
||||
throw new UnsupportedOperationException('Not a time-based UUID');
|
||||
}
|
||||
|
||||
return sprintf('%015x', $this->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the UUID as a URN.
|
||||
*
|
||||
|
||||
@@ -72,6 +72,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(155, $uuid->getClockSeqHiAndReserved());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReservedHex
|
||||
*/
|
||||
public function testGetClockSeqHiAndReservedHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('9b', $uuid->getClockSeqHiAndReservedHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLow
|
||||
*/
|
||||
@@ -81,6 +90,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(33, $uuid->getClockSeqLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLowHex
|
||||
*/
|
||||
public function testGetClockSeqLowHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('21', $uuid->getClockSeqLowHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSequence
|
||||
*/
|
||||
@@ -90,6 +108,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(6945, $uuid->getClockSequence());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getClockSequenceHex
|
||||
*/
|
||||
public function testGetClockSequenceHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('1b21', $uuid->getClockSequenceHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getDateTime
|
||||
*/
|
||||
@@ -124,13 +151,37 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGetFields()
|
||||
{
|
||||
$fields = array(
|
||||
'time_low' => 4285500592,
|
||||
'time_mid' => 50557,
|
||||
'time_hi_and_version' => 4577,
|
||||
'clock_seq_hi_and_reserved' => 155,
|
||||
'clock_seq_low' => 33,
|
||||
'node' => 8796630719078,
|
||||
);
|
||||
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertArrayHasKey('time_low', $uuid->getFields());
|
||||
$this->assertArrayHasKey('time_mid', $uuid->getFields());
|
||||
$this->assertArrayHasKey('time_hi_and_version', $uuid->getFields());
|
||||
$this->assertArrayHasKey('clock_seq_hi_and_reserved', $uuid->getFields());
|
||||
$this->assertArrayHasKey('clock_seq_low', $uuid->getFields());
|
||||
$this->assertArrayHasKey('node', $uuid->getFields());
|
||||
|
||||
$this->assertEquals($fields, $uuid->getFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getFieldsHex
|
||||
*/
|
||||
public function testGetFieldsHex()
|
||||
{
|
||||
$fields = array(
|
||||
'time_low' => 'ff6f8cb0',
|
||||
'time_mid' => 'c57d',
|
||||
'time_hi_and_version' => '11e1',
|
||||
'clock_seq_hi_and_reserved' => '9b',
|
||||
'clock_seq_low' => '21',
|
||||
'node' => '0800200c9a66',
|
||||
);
|
||||
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
|
||||
$this->assertEquals($fields, $uuid->getFieldsHex());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,6 +211,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(8796630719078, $uuid->getNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getNodeHex
|
||||
*/
|
||||
public function testGetNodeHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('0800200c9a66', $uuid->getNodeHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersion
|
||||
*/
|
||||
@@ -169,6 +229,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(4577, $uuid->getTimeHiAndVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersionHex
|
||||
*/
|
||||
public function testGetTimeHiAndVersionHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('11e1', $uuid->getTimeHiAndVersionHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeLow
|
||||
*/
|
||||
@@ -178,6 +247,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(4285500592, $uuid->getTimeLow());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeLowHex
|
||||
*/
|
||||
public function testGetTimeLowHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('ff6f8cb0', $uuid->getTimeLowHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeMid
|
||||
*/
|
||||
@@ -187,6 +265,15 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(50557, $uuid->getTimeMid());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimeMidHex
|
||||
*/
|
||||
public function testGetTimeMidHex()
|
||||
{
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('c57d', $uuid->getTimeMidHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
||||
*/
|
||||
@@ -201,6 +288,20 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(1460440000000, $uuid->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
|
||||
*/
|
||||
public function testGetTimestampHex()
|
||||
{
|
||||
// Check for a recent date
|
||||
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
|
||||
$this->assertEquals('1e1c57dff6f8cb0', $uuid->getTimestampHex());
|
||||
|
||||
// Check for an old date
|
||||
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
|
||||
$this->assertEquals('00001540901e600', $uuid->getTimestampHex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
|
||||
* @covers Rhumsaa\Uuid\UnsupportedOperationException
|
||||
@@ -214,6 +315,19 @@ class UuidTest extends \PHPUnit_Framework_TestCase
|
||||
$ts = $uuid->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
|
||||
* @covers Rhumsaa\Uuid\UnsupportedOperationException
|
||||
* @expectedException Rhumsaa\Uuid\UnsupportedOperationException
|
||||
* @expectedExceptionMessage Not a time-based UUID
|
||||
*/
|
||||
public function testGetTimestampHexFromNonVersion1Uuid()
|
||||
{
|
||||
// Using a version 4 UUID to test
|
||||
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
|
||||
$ts = $uuid->getTimestampHex();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Rhumsaa\Uuid\Uuid::getUrn
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user