| 1 |
<?php |
| 2 |
/** |
| 3 |
* UUIDs handling |
| 4 |
* |
| 5 |
* Handles all UUID operations and generation. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace OPcacheManager\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the UUID functionality. |
| 16 |
* |
| 17 |
* Handles all UUID operations and generation. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class UUID { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Generates a v4 UUID. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @return string A v4 UUID. |
| 38 |
*/ |
| 39 |
public static function generate_v4() { |
| 40 |
return sprintf( |
| 41 |
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
| 42 |
// phpcs:disable |
| 43 |
mt_rand( 0, 0xffff ), |
| 44 |
mt_rand( 0, 0xffff ), |
| 45 |
mt_rand( 0, 0xffff ), |
| 46 |
mt_rand( 0, 0x0fff ) | 0x4000, |
| 47 |
mt_rand( 0, 0x3fff ) | 0x8000, |
| 48 |
mt_rand( 0, 0xffff ), |
| 49 |
mt_rand( 0, 0xffff ), |
| 50 |
mt_rand( 0, 0xffff ) |
| 51 |
// phpcs:enabled |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Generates a (pseudo) unique ID. |
| 57 |
* This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. |
| 58 |
* |
| 59 |
* @param integer $length The length of the ID. |
| 60 |
* @return string The unique ID. |
| 61 |
* @since 1.0.0 |
| 62 |
*/ |
| 63 |
public static function generate_unique_id( $length = 10 ) { |
| 64 |
$result = ''; |
| 65 |
$date = new \DateTime(); |
| 66 |
do { |
| 67 |
$s = self::generate_v4(); |
| 68 |
$s = str_replace( '-', (string) ( $date->format( 'u' ) ), $s ); |
| 69 |
$result .= $s; |
| 70 |
$l = strlen( $result ); |
| 71 |
} while ( $l < $length ); |
| 72 |
return substr( str_shuffle( $result ), 0, $length ); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|