Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| UUID | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| default | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\RnG |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\RnG; |
| 16 | |
| 17 | /** |
| 18 | * UUID generator. |
| 19 | * |
| 20 | * @package phpOMS\Utils\RnG |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class UUID |
| 26 | { |
| 27 | /** |
| 28 | * Get default random UUID |
| 29 | * |
| 30 | * @param int<8, max> $length Result length in bytes |
| 31 | * |
| 32 | * @return string |
| 33 | * |
| 34 | * @throws \InvalidArgumentException |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public static function default(int $length = 16) : string |
| 39 | { |
| 40 | /** @phpstan-ignore-next-line */ |
| 41 | if ($length < 8) { |
| 42 | throw new \InvalidArgumentException(); |
| 43 | } |
| 44 | |
| 45 | return \pack('Q', \time()) . ($length > 8 ? \random_bytes($length - 8) : ''); |
| 46 | } |
| 47 | } |