1 | <?php |
||
2 | |||
3 | namespace Bmatovu\Uuid\Support; |
||
4 | |||
5 | use Ramsey\Uuid\Exception\InvalidUuidStringException; |
||
6 | use Ramsey\Uuid\Uuid; |
||
7 | use Ramsey\Uuid\UuidInterface; |
||
8 | |||
9 | class Util |
||
10 | { |
||
11 | /** |
||
12 | * Generate UUID by version. |
||
13 | * |
||
14 | * @param int $version UUID Version |
||
15 | * @param string|\Ramsey\Uuid\UuidInterface $ns The UUID namespace in which to create the named UUID |
||
16 | * @param string $name The name to create a UUID for |
||
17 | * |
||
18 | * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException |
||
19 | * |
||
20 | * @return \Ramsey\Uuid\UuidInterface UUID |
||
21 | */ |
||
22 | 5 | public static function generateUuid(int $version = 4, $ns = null, string $name = null): UuidInterface |
|
23 | { |
||
24 | 5 | $ns = $ns ? $ns : Uuid::NAMESPACE_DNS; |
|
25 | |||
26 | 5 | $name = $name ? $name : php_uname('n'); |
|
27 | |||
28 | switch ($version) { |
||
29 | 5 | case 1: |
|
30 | 1 | $uuid = Uuid::uuid1(); |
|
31 | 1 | break; |
|
32 | 5 | case 3: |
|
33 | 1 | $uuid = Uuid::uuid3($ns, $name); |
|
34 | 1 | break; |
|
35 | 5 | case 5: |
|
36 | 1 | $uuid = Uuid::uuid5($ns, $name); |
|
37 | 1 | break; |
|
38 | default: |
||
39 | 5 | $uuid = Uuid::uuid4(); |
|
40 | 5 | break; |
|
41 | } |
||
42 | |||
43 | 5 | return $uuid; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Validate UUID plus version. |
||
48 | * |
||
49 | * @param string $str |
||
50 | * @param int|null $version UUID Version |
||
51 | * |
||
52 | * @return bool True for valid. |
||
53 | */ |
||
54 | 1 | public static function validateUuid(string $str, int $version = null) |
|
55 | { |
||
56 | 1 | if ($version === null) { |
|
57 | 1 | return Uuid::isValid($str); |
|
58 | } |
||
59 | |||
60 | try { |
||
61 | 1 | return Uuid::fromString($str)->getVersion() === $version; |
|
0 ignored issues
–
show
|
|||
62 | 1 | } catch (InvalidUuidStringException $e) { |
|
63 | 1 | return false; |
|
64 | } |
||
65 | } |
||
66 | } |
||
67 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.