mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
Streamlining toString() method
This commit is contained in:
+14
-18
@@ -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
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user