1: <?php
 2: /**
 3:  * Wei Framework
 4:  *
 5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
 6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
 7:  */
 8: 
 9: namespace Wei;
10: 
11: /**
12:  * A util wei that generates a RANDOM UUID(universally unique identifier)
13:  *
14:  * @author      Twin Huang <twinhuang@qq.com>
15:  */
16: class Uuid extends Base
17: {
18:     /**
19:      * Generate a RANDOM UUID(universally unique identifier)
20:      *
21:      * @link http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
22:      * @link http://php.net/manual/en/function.uniqid.php
23:      * @return string
24:      */
25:     public function __invoke()
26:     {
27:         return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
28:             // 32 bits for "time_low"
29:             mt_rand(0, 0xffff), mt_rand(0, 0xffff),
30:             // 16 bits for "time_mid"
31:             mt_rand(0, 0xffff),
32:             // 16 bits for "time_hi_and_version",
33:             // four most significant bits holds version number 4
34:             mt_rand(0, 0x0fff) | 0x4000,
35:             // 16 bits, 8 bits for "clk_seq_hi_res",
36:             // 8 bits for "clk_seq_low",
37:             // two most significant bits holds zero and one for variant DCE1.1
38:             mt_rand(0, 0x3fff) | 0x8000,
39:             // 48 bits for "node"
40:             mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
41:         );
42:     }
43: }
44: