18
18
*
19
19
* @author Nicolas Grekas <p@tchwork.com>
20
20
*/
21
- class Ulid implements \JsonSerializable
21
+ class Ulid extends AbstractUid
22
22
{
23
23
private static $ time = -1 ;
24
24
private static $ rand = [];
25
25
26
- private $ ulid ;
27
-
28
26
public function __construct (string $ ulid = null )
29
27
{
30
28
if (null === $ ulid ) {
31
- $ this ->ulid = self ::generate ();
29
+ $ this ->uid = self ::generate ();
32
30
33
31
return ;
34
32
}
@@ -37,7 +35,7 @@ public function __construct(string $ulid = null)
37
35
throw new \InvalidArgumentException (sprintf ('Invalid ULID: "%s". ' , $ ulid ));
38
36
}
39
37
40
- $ this ->ulid = strtr ($ ulid , 'abcdefghjkmnpqrstvwxyz ' , 'ABCDEFGHJKMNPQRSTVWXYZ ' );
38
+ $ this ->uid = strtr ($ ulid , 'abcdefghjkmnpqrstvwxyz ' , 'ABCDEFGHJKMNPQRSTVWXYZ ' );
41
39
}
42
40
43
41
public static function isValid (string $ ulid ): bool
@@ -53,8 +51,17 @@ public static function isValid(string $ulid): bool
53
51
return $ ulid [0 ] <= '7 ' ;
54
52
}
55
53
56
- public static function fromString (string $ ulid ): self
54
+ /**
55
+ * {@inheritdoc}
56
+ */
57
+ public static function fromString (string $ ulid ): parent
57
58
{
59
+ if (36 === \strlen ($ ulid ) && Uuid::isValid ($ ulid )) {
60
+ $ ulid = Uuid::fromString ($ ulid )->toBinary ();
61
+ } elseif (22 === \strlen ($ ulid ) && 22 === strspn ($ ulid , BinaryUtil::BASE58 ['' ])) {
62
+ $ ulid = BinaryUtil::fromBase ($ ulid , BinaryUtil::BASE58 );
63
+ }
64
+
58
65
if (16 !== \strlen ($ ulid )) {
59
66
return new static ($ ulid );
60
67
}
@@ -73,9 +80,9 @@ public static function fromString(string $ulid): self
73
80
return new self (strtr ($ ulid , 'abcdefghijklmnopqrstuv ' , 'ABCDEFGHJKMNPQRSTVWXYZ ' ));
74
81
}
75
82
76
- public function toBinary ()
83
+ public function toBinary (): string
77
84
{
78
- $ ulid = strtr ($ this ->ulid , 'ABCDEFGHJKMNPQRSTVWXYZ ' , 'abcdefghijklmnopqrstuv ' );
85
+ $ ulid = strtr ($ this ->uid , 'ABCDEFGHJKMNPQRSTVWXYZ ' , 'abcdefghijklmnopqrstuv ' );
79
86
80
87
$ ulid = sprintf ('%02s%05s%05s%05s%05s%05s%05s ' ,
81
88
base_convert (substr ($ ulid , 0 , 2 ), 32 , 16 ),
@@ -90,26 +97,14 @@ public function toBinary()
90
97
return hex2bin ($ ulid );
91
98
}
92
99
93
- /**
94
- * Returns whether the argument is of class Ulid and contains the same value as the current instance.
95
- */
96
- public function equals ($ other ): bool
100
+ public function toBase32 (): string
97
101
{
98
- if (!$ other instanceof self) {
99
- return false ;
100
- }
101
-
102
- return $ this ->ulid === $ other ->ulid ;
103
- }
104
-
105
- public function compare (self $ other ): int
106
- {
107
- return $ this ->ulid <=> $ other ->ulid ;
102
+ return $ this ->uid ;
108
103
}
109
104
110
105
public function getTime (): float
111
106
{
112
- $ time = strtr (substr ($ this ->ulid , 0 , 10 ), 'ABCDEFGHJKMNPQRSTVWXYZ ' , 'abcdefghijklmnopqrstuv ' );
107
+ $ time = strtr (substr ($ this ->uid , 0 , 10 ), 'ABCDEFGHJKMNPQRSTVWXYZ ' , 'abcdefghijklmnopqrstuv ' );
113
108
114
109
if (\PHP_INT_SIZE >= 8 ) {
115
110
return hexdec (base_convert ($ time , 32 , 16 )) / 1000 ;
@@ -124,16 +119,6 @@ public function getTime(): float
124
119
return BinaryUtil::toBase (hex2bin ($ time ), BinaryUtil::BASE10 ) / 1000 ;
125
120
}
126
121
127
- public function __toString (): string
128
- {
129
- return $ this ->ulid ;
130
- }
131
-
132
- public function jsonSerialize (): string
133
- {
134
- return $ this ->ulid ;
135
- }
136
-
137
122
private static function generate (): string
138
123
{
139
124
$ time = microtime (false );
0 commit comments