Streamlining toString() method

This commit is contained in:
Ben Ramsey
2012-07-18 17:20:54 -05:00
parent 1b26a6da84
commit 302b5c519d
2 changed files with 14 additions and 19 deletions
+14 -18
View File
@@ -375,11 +375,20 @@ final class Uuid
*/
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));
$hex = sprintf(
'%016x%016x',
$this->getMostSignificantBits(),
$this->getLeastSignificantBits()
);
return sprintf(
'%s-%s-%s-%s-%s',
substr($hex, 0, 8),
substr($hex, 8, 4),
substr($hex, 12, 4),
substr($hex, 16, 4),
substr($hex, 20)
);
}
/**
@@ -522,19 +531,6 @@ final class Uuid
{
}
/**
* 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
-1
View File
@@ -321,7 +321,6 @@ class UuidTest extends \PHPUnit_Framework_TestCase
/**
* @covers Rhumsaa\Uuid\Uuid::toString
* @covers Rhumsaa\Uuid\Uuid::__toString
* @covers Rhumsaa\Uuid\Uuid::digits
*/
public function testToString()
{