public function testTimestamp() { $key = Base36TimeKey::generate(123456789); $this->assertEquals(123456789, floor(Base36TimeKey::getMsTime($key))); for ($x = 0; $x < 10; $x++) { $limit = 7000; for ($i = 0; $i < $limit; $i++) { $microtime = microtime(true); $key = Base36TimeKey::generate($microtime); $this->assertEquals(floor((string) $microtime), floor((string) Base36TimeKey::getMsTime($key))); } } }
public function __construct($fid) { $fid = strtoupper($fid); $this->timeKey = substr($fid, 9, 9); $this->indicator = substr($fid, 0, 1); $this->vendor = substr($fid, 1, 3); $this->app = substr($fid, 4, 2); $this->type = substr($fid, 6, 2); $this->location = substr($fid, 19, 5); $this->random = substr($fid, 25, 7); $this->verify = substr($fid, 31, 1); $ts = Base36TimeKey::getMsTime($this->timeKey); $this->timestampMs = $ts * 1000; $this->timestamp = floor($ts); }
/** * Generate a new FID * * @param string $vendor Vendor Key * @param string $app App key * @param string $type * @param string $indicator * @param null $secret * @param string $location * * @throws FidGenerateException * * @return string FID */ public static function generate($vendor, $app, $type, $indicator = SystemIndicator::ENTITY, $secret = null, $location = Location::UNKNOWN_REGION) { if (strlen($vendor) !== 3) { throw new FidGenerateException("Vendor Key must be 3 characters"); } if (strlen($app) !== 2) { throw new FidGenerateException("App Key must be 2 characters"); } if (strlen($type) !== 2) { throw new FidGenerateException("Entity Type must be 2 characters"); } if (!SystemIndicator::validate($indicator)) { throw new FidGenerateException("Invalid System Indicator"); } if (!Location::validate($location)) { throw new FidGenerateException("Invalid Data Location"); } $fid = sprintf("%s%s%s%s-%s-%s-%s", $indicator, $vendor, $app, $type, Base36TimeKey::generate(), $location, static::randomString($secret === null ? 7 : 6)); if ($secret !== null) { $fid .= strtoupper(substr(md5($secret . $fid), 0, 1)); } return $fid; }