Example #1
0
 /**
  * Uses the provided seconds and micro-seconds to calculate the time_low,
  * time_mid, and time_high fields used by RFC 4122 version 1 UUIDs
  *
  * @param string $seconds
  * @param string $microSeconds
  * @return string[] An array containing `low`, `mid`, and `high` keys
  * @link http://tools.ietf.org/html/rfc4122#section-4.2.2
  */
 public function calculateTime($seconds, $microSeconds)
 {
     $uuidTime = new BigNumber('0');
     $sec = new BigNumber($seconds);
     $sec->multiply('10000000');
     $usec = new BigNumber($microSeconds);
     $usec->multiply('10');
     $uuidTime->add($sec)->add($usec)->add('122192928000000000');
     $uuidTimeHex = sprintf('%016s', $uuidTime->convertToBase(16));
     return array('low' => substr($uuidTimeHex, 8), 'mid' => substr($uuidTimeHex, 4, 4), 'hi' => substr($uuidTimeHex, 0, 4));
 }
Example #2
0
 /**
  * Convert a number to a string, using the given alphabet.
  *
  * @param \Moontoast\Math\BigNumber $number
  * @param number $padToLength
  * @return string
  */
 private function numToString($number, $padToLength = null)
 {
     $output = '';
     while ($number->compareTo('0') > 0) {
         $ret = self::divmod($number, $this->alphabetLength);
         $number = $ret[0];
         $digit = $ret[1];
         $output .= $this->alphabet[(int) $digit->getValue()];
     }
     if (!is_null($padToLength)) {
         $reminder = max($padToLength - strlen($output), 0);
         $output = $output . str_repeat($this->alphabet[0], $reminder);
     }
     return $output;
 }
Example #3
0
 /**
  * Converts an integer or `Moontoast\Math\BigNumber` integer representation
  * into a hexadecimal string representation
  *
  * @param int|string|BigNumber $integer An integer or `Moontoast\Math\BigNumber`
  * @return string Hexadecimal string
  */
 public function toHex($integer)
 {
     if (!$integer instanceof BigNumber) {
         $integer = new BigNumber($integer);
     }
     return BigNumber::convertFromBase10($integer, 16);
 }
Example #4
0
 public function toHex($integer)
 {
     if (!$integer instanceof \Moontoast\Math\BigNumber) {
         $integer = new \Moontoast\Math\BigNumber($integer);
     }
     return \Moontoast\Math\BigNumber::baseConvert($integer, 10, 16);
 }
Example #5
0
 public function toHex($integer)
 {
     if (!$integer instanceof \Moontoast\Math\BigNumber) {
         $integer = new \Moontoast\Math\BigNumber($integer);
     }
     return \Moontoast\Math\BigNumber::convertFromBase10($integer, 16);
 }
Example #6
0
 /**
  * Creates a UUID from either the UUID as a 128-bit integer string or a Moontoast\Math\BigNumber object.
  *
  * @param string|\Moontoast\Math\BigNumber $integer String/BigNumber representation of UUID integer
  * @throws Exception\UnsatisfiedDependencyException If Moontoast\Math\BigNumber is not present
  * @return \Rhumsaa\Uuid\Uuid
  */
 public static function fromInteger($integer)
 {
     if (!self::hasBigNumber()) {
         throw new Exception\UnsatisfiedDependencyException('Cannot call ' . __METHOD__ . ' without support for large ' . 'integers, since integer is an unsigned ' . '128-bit integer; Moontoast\\Math\\BigNumber is required. ');
     }
     if (!$integer instanceof \Moontoast\Math\BigNumber) {
         $integer = new \Moontoast\Math\BigNumber($integer);
     }
     $hex = \Moontoast\Math\BigNumber::baseConvert($integer, 10, 16);
     $hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
     return self::fromString($hex);
 }
Example #7
0
 /**
  * Returns the most significant 64 bits of this UUID's 128 bit value
  *
  * @return \Moontoast\Math\BigNumber BigNumber representation of the unsigned 64-bit integer value
  * @throws Exception\UnsatisfiedDependencyException if Moontoast\Math\BigNumber is not present
  */
 public function getMostSignificantBits()
 {
     if (!self::hasBigNumber()) {
         throw new Exception\UnsatisfiedDependencyException('Cannot call ' . __METHOD__ . ' without support for large ' . 'integers, since most significant bits is an unsigned ' . '64-bit integer; Moontoast\\Math\\BigNumber is required' . '; consider calling getMostSignificantBitsHex instead');
     }
     $number = \Moontoast\Math\BigNumber::baseConvert($this->getMostSignificantBitsHex(), 16, 10);
     return new \Moontoast\Math\BigNumber($number);
 }
Example #8
0
 /**
  * Transforms a given string to a (big) number, based on the set alphabet.
  *
  * @param string $string
  *
  * @return BigNumber
  */
 private function stringToNum($string)
 {
     $number = new BigNumber(0);
     foreach (str_split(strrev($string)) as $char) {
         $number->multiply($this->alphabetLength)->add(array_search($char, $this->alphabet, false));
     }
     return $number;
 }
Example #9
0
 /**
  * BigNumber constructor.
  *
  * @param mixed $number
  * @param null $scale
  */
 public function __construct($number, $scale = null)
 {
     @trigger_error(sprintf('"%s" is deprecated.  Use "Gdbots\\Pbj\\WellKnown\\BigNumber" from "gdbots/pbj" 1.1.x or later instead.', __CLASS__), E_USER_DEPRECATED);
     parent::__construct($number, $scale);
 }