Ejemplo n.º 1
0
 public static function generate()
 {
     $hex = \UIDGenerator::newTimestampedUID88(16);
     $hex = str_pad($hex, static::HEX_LEN, '0', STR_PAD_LEFT);
     return self::fromHex($hex);
 }
 /**
  * Returns a UUID objects based on given input. Will automatically try to
  * determine the input format of the given $input or fail with an exception.
  *
  * @param mixed $input
  * @return UUID|null
  * @throws InvalidInputException
  */
 public static function create($input = false)
 {
     // Most calls to UUID::create are binary strings, check string first
     if (is_string($input) || is_int($input) || $input === false) {
         if ($input === false) {
             // new uuid in base 16 and pad to HEX_LEN with 0's
             $hexValue = str_pad(\UIDGenerator::newTimestampedUID88(16), self::HEX_LEN, '0', STR_PAD_LEFT);
             return new static($hexValue, static::INPUT_HEX);
         } else {
             $len = strlen($input);
             if ($len === self::BIN_LEN) {
                 $value = $input;
                 $type = static::INPUT_BIN;
             } elseif ($len >= self::MIN_ALNUM_LEN && $len <= self::ALNUM_LEN && ctype_alnum($input)) {
                 $value = $input;
                 $type = static::INPUT_ALNUM;
             } elseif ($len === self::HEX_LEN && ctype_xdigit($input)) {
                 $value = $input;
                 $type = static::INPUT_HEX;
             } elseif ($len === self::OLD_BIN_LEN) {
                 $value = substr($input, 0, self::BIN_LEN);
                 $type = static::INPUT_BIN;
             } elseif ($len === self::OLD_HEX_LEN && ctype_xdigit($input)) {
                 $value = substr($input, 0, self::HEX_LEN);
                 $type = static::INPUT_HEX;
             } elseif (is_numeric($input)) {
                 // convert base 10 to base 16 and pad to HEX_LEN with 0's
                 $value = wfBaseConvert($input, 10, 16, self::HEX_LEN);
                 $type = static::INPUT_HEX;
             } else {
                 throw new InvalidInputException('Unknown input to UUID class', 'invalid-input');
             }
             if (isset(self::$instances[$type][$value])) {
                 return self::$instances[$type][$value];
             } else {
                 return new static($value, $type);
             }
         }
     } else {
         if (is_object($input)) {
             if ($input instanceof UUID) {
                 return $input;
             } elseif ($input instanceof Blob) {
                 return self::create($input->fetch());
             } else {
                 throw new InvalidInputException('Unknown input of type ' . get_class($input), 'invalid-input');
             }
         } elseif ($input === null) {
             return null;
         } else {
             throw new InvalidInputException('Unknown input type to UUID class: ' . gettype($input), 'invalid-input');
         }
     }
 }