| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/uuid library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
* phpcs:disable Squiz.Functions.GlobalFunction |
| 12 |
*/ |
| 13 |
declare (strict_types=1); |
| 14 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid; |
| 15 |
|
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 18 |
/** |
| 19 |
* Returns a version 1 (time-based) UUID from a host ID, sequence number, |
| 20 |
* and the current time |
| 21 |
* |
| 22 |
* @param Hexadecimal|int|string|null $node A 48-bit number representing the |
| 23 |
* hardware address; this number may be represented as an integer or a |
| 24 |
* hexadecimal string |
| 25 |
* @param int $clockSeq A 14-bit number used to help avoid duplicates that |
| 26 |
* could arise when the clock is set backwards in time or if the node ID |
| 27 |
* changes |
| 28 |
* |
| 29 |
* @return non-empty-string Version 1 UUID as a string |
| 30 |
*/ |
| 31 |
function v1($node = null, ?int $clockSeq = null): string |
| 32 |
{ |
| 33 |
return Uuid::uuid1($node, $clockSeq)->toString(); |
| 34 |
} |
| 35 |
/** |
| 36 |
* Returns a version 2 (DCE Security) UUID from a local domain, local |
| 37 |
* identifier, host ID, clock sequence, and the current time |
| 38 |
* |
| 39 |
* @param int $localDomain The local domain to use when generating bytes, |
| 40 |
* according to DCE Security |
| 41 |
* @param IntegerObject|null $localIdentifier The local identifier for the |
| 42 |
* given domain; this may be a UID or GID on POSIX systems, if the local |
| 43 |
* domain is person or group, or it may be a site-defined identifier |
| 44 |
* if the local domain is org |
| 45 |
* @param Hexadecimal|null $node A 48-bit number representing the hardware |
| 46 |
* address |
| 47 |
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates |
| 48 |
* that could arise when the clock is set backwards in time or if the |
| 49 |
* node ID changes |
| 50 |
* |
| 51 |
* @return non-empty-string Version 2 UUID as a string |
| 52 |
*/ |
| 53 |
function v2(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null): string |
| 54 |
{ |
| 55 |
return Uuid::uuid2($localDomain, $localIdentifier, $node, $clockSeq)->toString(); |
| 56 |
} |
| 57 |
/** |
| 58 |
* Returns a version 3 (name-based) UUID based on the MD5 hash of a |
| 59 |
* namespace ID and a name |
| 60 |
* |
| 61 |
* @param string|UuidInterface $ns The namespace (must be a valid UUID) |
| 62 |
* |
| 63 |
* @return non-empty-string Version 3 UUID as a string |
| 64 |
* |
| 65 |
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants, |
| 66 |
* but under constant factory setups, this method operates in functionally pure manners |
| 67 |
*/ |
| 68 |
function v3($ns, string $name): string |
| 69 |
{ |
| 70 |
return Uuid::uuid3($ns, $name)->toString(); |
| 71 |
} |
| 72 |
/** |
| 73 |
* Returns a version 4 (random) UUID |
| 74 |
* |
| 75 |
* @return non-empty-string Version 4 UUID as a string |
| 76 |
*/ |
| 77 |
function v4(): string |
| 78 |
{ |
| 79 |
return Uuid::uuid4()->toString(); |
| 80 |
} |
| 81 |
/** |
| 82 |
* Returns a version 5 (name-based) UUID based on the SHA-1 hash of a |
| 83 |
* namespace ID and a name |
| 84 |
* |
| 85 |
* @param string|UuidInterface $ns The namespace (must be a valid UUID) |
| 86 |
* |
| 87 |
* @return non-empty-string Version 5 UUID as a string |
| 88 |
* |
| 89 |
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants, |
| 90 |
* but under constant factory setups, this method operates in functionally pure manners |
| 91 |
*/ |
| 92 |
function v5($ns, string $name): string |
| 93 |
{ |
| 94 |
return Uuid::uuid5($ns, $name)->toString(); |
| 95 |
} |
| 96 |
/** |
| 97 |
* Returns a version 6 (ordered-time) UUID from a host ID, sequence number, |
| 98 |
* and the current time |
| 99 |
* |
| 100 |
* @param Hexadecimal|null $node A 48-bit number representing the hardware |
| 101 |
* address |
| 102 |
* @param int $clockSeq A 14-bit number used to help avoid duplicates that |
| 103 |
* could arise when the clock is set backwards in time or if the node ID |
| 104 |
* changes |
| 105 |
* |
| 106 |
* @return non-empty-string Version 6 UUID as a string |
| 107 |
*/ |
| 108 |
function v6(?Hexadecimal $node = null, ?int $clockSeq = null): string |
| 109 |
{ |
| 110 |
return Uuid::uuid6($node, $clockSeq)->toString(); |
| 111 |
} |
| 112 |
|