Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Uid;
13
 
14
/**
15
 * @author Grégoire Pineau <lyrixx@lyrixx.info>
16
 *
17
 * @see https://tools.ietf.org/html/rfc4122#appendix-C for details about namespaces
18
 */
19
class Uuid extends AbstractUid
20
{
21
    public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
22
    public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
23
    public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
24
    public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
25
 
26
    protected const TYPE = 0;
27
    protected const NIL = '00000000-0000-0000-0000-000000000000';
28
    protected const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
29
 
30
    public function __construct(string $uuid, bool $checkVariant = false)
31
    {
32
        $type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
33
 
34
        if (false === $type || (static::TYPE ?: $type) !== $type) {
35
            throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
36
        }
37
 
38
        $this->uid = strtolower($uuid);
39
 
40
        if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
41
            throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
42
        }
43
    }
44
 
45
    public static function fromString(string $uuid): static
46
    {
47
        if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58[''])) {
48
            $uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
49
        }
50
 
51
        if (16 === \strlen($uuid)) {
52
            // don't use uuid_unparse(), it's slower
53
            $uuid = bin2hex($uuid);
54
            $uuid = substr_replace($uuid, '-', 8, 0);
55
            $uuid = substr_replace($uuid, '-', 13, 0);
56
            $uuid = substr_replace($uuid, '-', 18, 0);
57
            $uuid = substr_replace($uuid, '-', 23, 0);
58
        } elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
200 lars 59
            $ulid = new NilUlid();
148 lars 60
            $ulid->uid = strtoupper($uuid);
61
            $uuid = $ulid->toRfc4122();
62
        }
63
 
64
        if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
65
            return new static($uuid);
66
        }
67
 
68
        if (self::NIL === $uuid) {
69
            return new NilUuid();
70
        }
71
 
72
        if (self::MAX === $uuid = strtr($uuid, 'F', 'f')) {
73
            return new MaxUuid();
74
        }
75
 
76
        if (!\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) {
77
            return new self($uuid);
78
        }
79
 
80
        return match ((int) $uuid[14]) {
81
            UuidV1::TYPE => new UuidV1($uuid),
82
            UuidV3::TYPE => new UuidV3($uuid),
83
            UuidV4::TYPE => new UuidV4($uuid),
84
            UuidV5::TYPE => new UuidV5($uuid),
85
            UuidV6::TYPE => new UuidV6($uuid),
86
            UuidV7::TYPE => new UuidV7($uuid),
87
            UuidV8::TYPE => new UuidV8($uuid),
88
            default => new self($uuid),
89
        };
90
    }
91
 
92
    final public static function v1(): UuidV1
93
    {
94
        return new UuidV1();
95
    }
96
 
97
    final public static function v3(self $namespace, string $name): UuidV3
98
    {
99
        // don't use uuid_generate_md5(), some versions are buggy
100
        $uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
101
 
102
        return new UuidV3(self::format($uuid, '-3'));
103
    }
104
 
105
    final public static function v4(): UuidV4
106
    {
107
        return new UuidV4();
108
    }
109
 
110
    final public static function v5(self $namespace, string $name): UuidV5
111
    {
112
        // don't use uuid_generate_sha1(), some versions are buggy
113
        $uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
114
 
115
        return new UuidV5(self::format($uuid, '-5'));
116
    }
117
 
118
    final public static function v6(): UuidV6
119
    {
120
        return new UuidV6();
121
    }
122
 
123
    final public static function v7(): UuidV7
124
    {
125
        return new UuidV7();
126
    }
127
 
128
    final public static function v8(string $uuid): UuidV8
129
    {
130
        return new UuidV8($uuid);
131
    }
132
 
133
    public static function isValid(string $uuid): bool
134
    {
200 lars 135
        if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
136
            return true;
137
        }
138
 
139
        if (self::MAX === strtr($uuid, 'F', 'f') && \in_array(static::class, [__CLASS__, MaxUuid::class], true)) {
140
            return true;
141
        }
142
 
148 lars 143
        if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
144
            return false;
145
        }
146
 
147
        return __CLASS__ === static::class || static::TYPE === (int) $uuid[14];
148
    }
149
 
150
    public function toBinary(): string
151
    {
152
        return uuid_parse($this->uid);
153
    }
154
 
155
    public function toRfc4122(): string
156
    {
157
        return $this->uid;
158
    }
159
 
160
    public function compare(AbstractUid $other): int
161
    {
162
        if (false !== $cmp = uuid_compare($this->uid, $other->uid)) {
163
            return $cmp;
164
        }
165
 
166
        return parent::compare($other);
167
    }
168
 
169
    private static function format(string $uuid, string $version): string
170
    {
171
        $uuid[8] = $uuid[8] & "\x3F" | "\x80";
172
        $uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
173
        $uuid = substr_replace($uuid, $version, 13, 1);
174
        $uuid = substr_replace($uuid, '-', 18, 0);
175
 
176
        return substr_replace($uuid, '-', 23, 0);
177
    }
178
}