aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/uuid/src/functions.php
blob: fa80f4e8548a11e03a60ef31d5195778420e1c1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

/**
 * This file is part of the ramsey/uuid library
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
 * @license http://opensource.org/licenses/MIT MIT
 * phpcs:disable Squiz.Functions.GlobalFunction
 */

declare(strict_types=1);

namespace Ramsey\Uuid;

use DateTimeInterface;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\Integer as IntegerObject;

/**
 * Returns a version 1 (Gregorian time) UUID from a host ID, sequence number,
 * and the current time
 *
 * @param Hexadecimal|int|string|null $node A 48-bit number representing the
 *     hardware address; this number may be represented as an integer or a
 *     hexadecimal string
 * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
 *     could arise when the clock is set backwards in time or if the node ID
 *     changes
 *
 * @return non-empty-string Version 1 UUID as a string
 */
function v1($node = null, ?int $clockSeq = null): string
{
    return Uuid::uuid1($node, $clockSeq)->toString();
}

/**
 * Returns a version 2 (DCE Security) UUID from a local domain, local
 * identifier, host ID, clock sequence, and the current time
 *
 * @param int $localDomain The local domain to use when generating bytes,
 *     according to DCE Security
 * @param IntegerObject|null $localIdentifier The local identifier for the
 *     given domain; this may be a UID or GID on POSIX systems, if the local
 *     domain is person or group, or it may be a site-defined identifier
 *     if the local domain is org
 * @param Hexadecimal|null $node A 48-bit number representing the hardware
 *     address
 * @param int|null $clockSeq A 14-bit number used to help avoid duplicates
 *     that could arise when the clock is set backwards in time or if the
 *     node ID changes
 *
 * @return non-empty-string Version 2 UUID as a string
 */
function v2(
    int $localDomain,
    ?IntegerObject $localIdentifier = null,
    ?Hexadecimal $node = null,
    ?int $clockSeq = null
): string {
    return Uuid::uuid2($localDomain, $localIdentifier, $node, $clockSeq)->toString();
}

/**
 * Returns a version 3 (name-based) UUID based on the MD5 hash of a
 * namespace ID and a name
 *
 * @param string|UuidInterface $ns The namespace (must be a valid UUID)
 *
 * @return non-empty-string Version 3 UUID as a string
 *
 * @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
 *             but under constant factory setups, this method operates in functionally pure manners
 */
function v3($ns, string $name): string
{
    return Uuid::uuid3($ns, $name)->toString();
}

/**
 * Returns a version 4 (random) UUID
 *
 * @return non-empty-string Version 4 UUID as a string
 */
function v4(): string
{
    return Uuid::uuid4()->toString();
}

/**
 * Returns a version 5 (name-based) UUID based on the SHA-1 hash of a
 * namespace ID and a name
 *
 * @param string|UuidInterface $ns The namespace (must be a valid UUID)
 *
 * @return non-empty-string Version 5 UUID as a string
 *
 * @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
 *             but under constant factory setups, this method operates in functionally pure manners
 */
function v5($ns, string $name): string
{
    return Uuid::uuid5($ns, $name)->toString();
}

/**
 * Returns a version 6 (reordered time) UUID from a host ID, sequence number,
 * and the current time
 *
 * @param Hexadecimal|null $node A 48-bit number representing the hardware
 *     address
 * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
 *     could arise when the clock is set backwards in time or if the node ID
 *     changes
 *
 * @return non-empty-string Version 6 UUID as a string
 */
function v6(?Hexadecimal $node = null, ?int $clockSeq = null): string
{
    return Uuid::uuid6($node, $clockSeq)->toString();
}

/**
 * Returns a version 7 (Unix Epoch time) UUID
 *
 * @param DateTimeInterface|null $dateTime An optional date/time from which
 *     to create the version 7 UUID. If not provided, the UUID is generated
 *     using the current date/time.
 *
 * @return non-empty-string Version 7 UUID as a string
 */
function v7(?DateTimeInterface $dateTime = null): string
{
    return Uuid::uuid7($dateTime)->toString();
}