Skip to content

Commit

Permalink
Merge pull request #1 from freezy-sk/feature/static
Browse files Browse the repository at this point in the history
Changed instantiation from self to static
  • Loading branch information
webpatser committed Sep 29, 2014
2 parents 97586e7 + cdc1784 commit f28bc40
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions src/Webpatser/Uuid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Uuid

/**
* @param string $uuid
* @throws Exception
* @throws \Exception
*/
protected function __construct ($uuid)
{
Expand All @@ -126,29 +126,28 @@ protected function __construct ($uuid)
substr($uuid, 10, 6));
}


/**
* @param int $ver
* @param unknown_type $node
* @param unknown_type $ns
* @param string|null $node
* @param string|null $ns
* @return Uuid
* @throws Exception
* @throws \Exception
*/
public static function generate ($ver = 1, $node = NULL, $ns = NULL)
{
/* Create a new UUID based on provided data. */
switch ((int) $ver) {
case 1:
return new self( self::mintTime( $node ) );
return new static( self::mintTime( $node ) );
case 2:
// Version 2 is not supported
throw new \Exception( 'Version 2 is unsupported.' );
case 3:
return new self( self::mintName( self::MD5, $node, $ns ) );
return new static( self::mintName( self::MD5, $node, $ns ) );
case 4:
return new self( self::mintRand() );
return new static( self::mintRand() );
case 5:
return new self( self::mintName( self::SHA1, $node, $ns ) );
return new static( self::mintName( self::SHA1, $node, $ns ) );
default:
throw new \Exception( 'Selected version is invalid or unsupported.' );
}
Expand All @@ -162,7 +161,7 @@ public static function generate ($ver = 1, $node = NULL, $ns = NULL)
*/
public static function import ($uuid)
{
return new self(self::makeBin($uuid, 16));
return new static(self::makeBin($uuid, 16));
}

/**
Expand All @@ -172,7 +171,7 @@ public static function import ($uuid)
*
* @param string $a
* @param string $b
* @return string|string
* @return bool
*/
public static function compare ($a, $b)
{
Expand All @@ -186,16 +185,16 @@ public static function compare ($a, $b)

/**
* Echo the uuid
* @return string
*/
public function __toString ()
{
return $this->string;
}



/**
* @param string $var
* @return string|string|number|number|number|number|number|NULL|number|NULL|NULL
* @return int|string|null
*/
public function __get ($var)
{
Expand Down Expand Up @@ -247,8 +246,8 @@ public function __get ($var)
* Generates a Version 1 UUID.
* These are derived from the time at which they were generated.
*
* @param unknown_type $node
* @return unknown
* @param string|null $node
* @return string
*/
protected static function mintTime ($node = NULL)
{
Expand Down Expand Up @@ -297,12 +296,13 @@ protected static function mintTime ($node = NULL)
$uuid .= $node;
return $uuid;
}

/**
* Generate a Version 4 UUID.
* These are derived soly from random numbers.
* generate random fields
*
* @return Uuid
* @return string
*/
protected static function mintRand ()
{
Expand All @@ -313,15 +313,17 @@ protected static function mintRand ()
$uuid[6] = chr(ord($uuid[6]) & self::clearVer | self::version4);
return $uuid;
}

/**
* Generates a Version 3 or Version 5 UUID.
* These are derived from a hash of a name and its namespace, in binary form.
*
* @param unknown_type $ver
* @param unknown_type $node
* @param unknown_type $ns
* @return Uuid
* @throws Exception
* @param int $ver
* @param string $node
* @param string $ns
*
* @return string
* @throws \Exception
*/
protected static function mintName ($ver, $node, $ns)
{
Expand Down Expand Up @@ -353,13 +355,14 @@ protected static function mintName ($ver, $node, $ns)
$uuid[6] = chr( ord($uuid[6]) & self::clearVer | $version);
return ($uuid);
}

/**
* Insure that an input string is either binary or hexadecimal.
* Returns binary representation, or false on failure.
*
* @param unknown_type $str
* @param unknown_type $len
* @return Uuid|string
* @param string $str
* @param int $len
* @return bool|string
*/
protected static function makeBin ($str, $len)
{
Expand All @@ -379,10 +382,13 @@ protected static function makeBin ($str, $len)
return pack("H*", $str);
}
}

/**
* Look for a system-provided source of randomness, which is usually crytographically secure.
* /dev/urandom is tried first simply out of bias for Linux systems.
*
* @throws \Exception
* @return string
*/
public static function initRandom ()
{
Expand All @@ -401,16 +407,21 @@ public static function initRandom ()
}
return self::$randomFunc;
}


/**
* @param int $bytes
* @return string
*/
public static function randomBytes ($bytes)
{
return call_user_func(array('self', self::$randomFunc), $bytes);
}

/**
* Get the specified number of random bytes, using mt_rand().
* Randomness is returned as a string of bytes.
*
* @param unknown_type $bytes
* @param int $bytes
* @return string
*/
protected static function randomTwister ($bytes)
Expand All @@ -427,20 +438,23 @@ protected static function randomTwister ($bytes)
* previously opened with UUID::initRandom().
* Randomness is returned as a string of bytes.
*
* @param unknown_type $bytes
* @param int $bytes
* @return string
*/
protected static function randomFRead ($bytes)
{
return fread(self::$randomSource, $bytes);
}

/**
* Get the specified number of random bytes using Windows'
* randomness source via a COM object previously created by UUID::initRandom().
* Randomness is returned as a string of bytes.
*
* Straight binary mysteriously doesn't work, hence the base64
*
* @param unknown_type $bytes
* @param int $bytes
* @return string
*/
protected static function randomCOM ($bytes)
{
Expand Down

0 comments on commit f28bc40

Please sign in to comment.