Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | 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 Nicolas Grekas <p@tchwork.com>
16
 */
17
abstract class AbstractUid implements \JsonSerializable
18
{
19
    /**
20
     * The identifier in its canonic representation.
21
     */
22
    protected $uid;
23
 
24
    /**
25
     * Whether the passed value is valid for the constructor of the current class.
26
     */
27
    abstract public static function isValid(string $uid): bool;
28
 
29
    /**
30
     * Creates an AbstractUid from an identifier represented in any of the supported formats.
31
     *
32
     * @throws \InvalidArgumentException When the passed value is not valid
33
     */
34
    abstract public static function fromString(string $uid): static;
35
 
36
    /**
37
     * @throws \InvalidArgumentException When the passed value is not valid
38
     */
39
    public static function fromBinary(string $uid): static
40
    {
41
        if (16 !== \strlen($uid)) {
42
            throw new \InvalidArgumentException('Invalid binary uid provided.');
43
        }
44
 
45
        return static::fromString($uid);
46
    }
47
 
48
    /**
49
     * @throws \InvalidArgumentException When the passed value is not valid
50
     */
51
    public static function fromBase58(string $uid): static
52
    {
53
        if (22 !== \strlen($uid)) {
54
            throw new \InvalidArgumentException('Invalid base-58 uid provided.');
55
        }
56
 
57
        return static::fromString($uid);
58
    }
59
 
60
    /**
61
     * @throws \InvalidArgumentException When the passed value is not valid
62
     */
63
    public static function fromBase32(string $uid): static
64
    {
65
        if (26 !== \strlen($uid)) {
66
            throw new \InvalidArgumentException('Invalid base-32 uid provided.');
67
        }
68
 
69
        return static::fromString($uid);
70
    }
71
 
72
    /**
73
     * @throws \InvalidArgumentException When the passed value is not valid
74
     */
75
    public static function fromRfc4122(string $uid): static
76
    {
77
        if (36 !== \strlen($uid)) {
78
            throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
79
        }
80
 
81
        return static::fromString($uid);
82
    }
83
 
84
    /**
85
     * Returns the identifier as a raw binary string.
86
     */
87
    abstract public function toBinary(): string;
88
 
89
    /**
90
     * Returns the identifier as a base58 case sensitive string.
91
     */
92
    public function toBase58(): string
93
    {
94
        return strtr(sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
95
    }
96
 
97
    /**
98
     * Returns the identifier as a base32 case insensitive string.
99
     */
100
    public function toBase32(): string
101
    {
102
        $uid = bin2hex($this->toBinary());
103
        $uid = sprintf('%02s%04s%04s%04s%04s%04s%04s',
104
            base_convert(substr($uid, 0, 2), 16, 32),
105
            base_convert(substr($uid, 2, 5), 16, 32),
106
            base_convert(substr($uid, 7, 5), 16, 32),
107
            base_convert(substr($uid, 12, 5), 16, 32),
108
            base_convert(substr($uid, 17, 5), 16, 32),
109
            base_convert(substr($uid, 22, 5), 16, 32),
110
            base_convert(substr($uid, 27, 5), 16, 32)
111
        );
112
 
113
        return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
114
    }
115
 
116
    /**
117
     * Returns the identifier as a RFC4122 case insensitive string.
118
     */
119
    public function toRfc4122(): string
120
    {
121
        // don't use uuid_unparse(), it's slower
122
        $uuid = bin2hex($this->toBinary());
123
        $uuid = substr_replace($uuid, '-', 8, 0);
124
        $uuid = substr_replace($uuid, '-', 13, 0);
125
        $uuid = substr_replace($uuid, '-', 18, 0);
126
 
127
        return substr_replace($uuid, '-', 23, 0);
128
    }
129
 
130
    /**
131
     * Returns the identifier as a prefixed hexadecimal case insensitive string.
132
     */
133
    public function toHex(): string
134
    {
135
        return '0x'.bin2hex($this->toBinary());
136
    }
137
 
138
    /**
139
     * Returns whether the argument is an AbstractUid and contains the same value as the current instance.
140
     */
141
    public function equals(mixed $other): bool
142
    {
143
        if (!$other instanceof self) {
144
            return false;
145
        }
146
 
147
        return $this->uid === $other->uid;
148
    }
149
 
150
    public function compare(self $other): int
151
    {
152
        return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
153
    }
154
 
155
    public function __toString(): string
156
    {
157
        return $this->uid;
158
    }
159
 
160
    public function jsonSerialize(): string
161
    {
162
        return $this->uid;
163
    }
164
}