00001 <?php
00002 namespace Habari;
00012 class UUID
00013 {
00014 private $uuid = array();
00015
00024 public function __construct( $version = 4 )
00025 {
00026 $uuid = array();
00027 for ( $i = 0; $i < 16; $i++ ) {
00028 $uuid[] = mt_rand( 0, 255 );
00029 }
00030
00031 $uuid[8] = ( $uuid[8] & 0x3f ) | 0x80;
00032
00033
00034
00035
00036
00037 $uuid[6] = ( $uuid[6] & 0x0f ) | 0x40;
00038
00039 $this->uuid = $uuid;
00040 }
00041
00045 public function __toString()
00046 {
00047 return $this->get_hex();
00048 }
00049
00053 public function get_array()
00054 {
00055 return $this->uuid;
00056 }
00057
00061 public function get_raw()
00062 {
00063 return implode( '', array_map( 'chr', $this->uuid ) );
00064 }
00065
00069 public function get_hex()
00070 {
00071 $uuid_hex = '';
00072 for ( $i = 0; $i < 16; $i++ ) {
00073 if ( 4==$i || 6==$i || 8==$i || 10==$i ) {
00074 $uuid_hex.= '-';
00075 }
00076 $uuid_hex.= sprintf( '%02x', $this->uuid[$i] );
00077 }
00078
00079 return $uuid_hex;
00080 }
00081
00087 public static function get()
00088 {
00089 $uuid = new UUID();
00090 return $uuid->get_hex();
00091 }
00092
00093 }
00094
00095 ?>