/lib/Widget/Uuid.php
PHP | 43 lines | 15 code | 2 blank | 26 comment | 0 complexity | 78c77df605d569aa97b0e5e93dee3b62 MD5 | raw file
- <?php
- /**
- * Widget Framework
- *
- * @copyright Copyright (c) 2008-2013 Twin Huang
- * @license http://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Widget;
- /**
- * A util widget that generates a RANDOM UUID(universally unique identifier)
- *
- * @author Twin Huang <twinhuang@qq.com>
- */
- class Uuid extends AbstractWidget
- {
- /**
- * Generate a RANDOM UUID(universally unique identifier)
- *
- * @link http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
- * @link http://php.net/manual/en/function.uniqid.php
- * @return string
- */
- public function __invoke()
- {
- return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- // 32 bits for "time_low"
- mt_rand(0, 0xffff), mt_rand(0, 0xffff),
- // 16 bits for "time_mid"
- mt_rand(0, 0xffff),
- // 16 bits for "time_hi_and_version",
- // four most significant bits holds version number 4
- mt_rand(0, 0x0fff) | 0x4000,
- // 16 bits, 8 bits for "clk_seq_hi_res",
- // 8 bits for "clk_seq_low",
- // two most significant bits holds zero and one for variant DCE1.1
- mt_rand(0, 0x3fff) | 0x8000,
- // 48 bits for "node"
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
- );
- }
- }