File tree 4 files changed +116
-14
lines changed
4 files changed +116
-14
lines changed Original file line number Diff line number Diff line change 18
18
use Ramsey \Uuid \UuidInterface ;
19
19
20
20
use function bin2hex ;
21
+ use function sprintf ;
21
22
use function substr ;
22
23
23
24
/**
29
30
*/
30
31
class GuidStringCodec extends StringCodec
31
32
{
33
+ public function encode (UuidInterface $ uuid ): string
34
+ {
35
+ $ hex = bin2hex ($ uuid ->getFields ()->getBytes ());
36
+
37
+ /** @var non-empty-string */
38
+ return sprintf (
39
+ '%02s%02s%02s%02s-%02s%02s-%02s%02s-%04s-%012s ' ,
40
+ substr ($ hex , 6 , 2 ),
41
+ substr ($ hex , 4 , 2 ),
42
+ substr ($ hex , 2 , 2 ),
43
+ substr ($ hex , 0 , 2 ),
44
+ substr ($ hex , 10 , 2 ),
45
+ substr ($ hex , 8 , 2 ),
46
+ substr ($ hex , 14 , 2 ),
47
+ substr ($ hex , 12 , 2 ),
48
+ substr ($ hex , 16 , 4 ),
49
+ substr ($ hex , 20 ),
50
+ );
51
+ }
52
+
32
53
public function decode (string $ encodedUuid ): UuidInterface
33
54
{
34
55
$ bytes = $ this ->getBytes ($ encodedUuid );
Original file line number Diff line number Diff line change 17
17
use Ramsey \Uuid \Builder \UuidBuilderInterface ;
18
18
use Ramsey \Uuid \Exception \InvalidArgumentException ;
19
19
use Ramsey \Uuid \Exception \InvalidUuidStringException ;
20
- use Ramsey \Uuid \Rfc4122 \FieldsInterface ;
21
20
use Ramsey \Uuid \Uuid ;
22
21
use Ramsey \Uuid \UuidInterface ;
23
22
23
+ use function bin2hex ;
24
24
use function hex2bin ;
25
25
use function implode ;
26
+ use function sprintf ;
26
27
use function str_replace ;
27
28
use function strlen ;
28
29
use function substr ;
@@ -47,19 +48,17 @@ public function __construct(private UuidBuilderInterface $builder)
47
48
48
49
public function encode (UuidInterface $ uuid ): string
49
50
{
50
- /** @var FieldsInterface $fields */
51
- $ fields = $ uuid ->getFields ();
52
-
53
- return $ fields ->getTimeLow ()->toString ()
54
- . '- '
55
- . $ fields ->getTimeMid ()->toString ()
56
- . '- '
57
- . $ fields ->getTimeHiAndVersion ()->toString ()
58
- . '- '
59
- . $ fields ->getClockSeqHiAndReserved ()->toString ()
60
- . $ fields ->getClockSeqLow ()->toString ()
61
- . '- '
62
- . $ fields ->getNode ()->toString ();
51
+ $ hex = bin2hex ($ uuid ->getFields ()->getBytes ());
52
+
53
+ /** @var non-empty-string */
54
+ return sprintf (
55
+ '%08s-%04s-%04s-%04s-%012s ' ,
56
+ substr ($ hex , 0 , 8 ),
57
+ substr ($ hex , 8 , 4 ),
58
+ substr ($ hex , 12 , 4 ),
59
+ substr ($ hex , 16 , 4 ),
60
+ substr ($ hex , 20 ),
61
+ );
63
62
}
64
63
65
64
/**
Original file line number Diff line number Diff line change
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
+ */
12
+
13
+ declare (strict_types=1 );
14
+
15
+ namespace Ramsey \Uuid \Benchmark ;
16
+
17
+ use Ramsey \Uuid \FeatureSet ;
18
+ use Ramsey \Uuid \Guid \Guid ;
19
+ use Ramsey \Uuid \UuidFactory ;
20
+ use Ramsey \Uuid \UuidInterface ;
21
+
22
+ final class GuidConversionBench
23
+ {
24
+ private const UUID_BYTES = [
25
+ "\x1e\x94\x42\x33\x98\x10\x41\x38\x96\x22\x56\xe1\xf9\x0c\x56\xed" ,
26
+ ];
27
+
28
+ private UuidInterface $ uuid ;
29
+
30
+ public function __construct ()
31
+ {
32
+ $ factory = new UuidFactory (new FeatureSet (useGuids: true ));
33
+
34
+ $ this ->uuid = $ factory ->fromBytes (self ::UUID_BYTES [0 ]);
35
+
36
+ assert ($ this ->uuid instanceof Guid);
37
+ }
38
+
39
+ public function benchStringConversionOfGuid (): void
40
+ {
41
+ $ this ->uuid ->toString ();
42
+ }
43
+ }
Original file line number Diff line number Diff line change
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
+ */
12
+
13
+ declare (strict_types=1 );
14
+
15
+ namespace Ramsey \Uuid \Benchmark ;
16
+
17
+ use Ramsey \Uuid \UuidFactory ;
18
+ use Ramsey \Uuid \UuidInterface ;
19
+
20
+ final class NonLazyUuidConversionBench
21
+ {
22
+ private const UUID_BYTES = [
23
+ "\x1e\x94\x42\x33\x98\x10\x41\x38\x96\x22\x56\xe1\xf9\x0c\x56\xed" ,
24
+ ];
25
+
26
+ private UuidInterface $ uuid ;
27
+
28
+ public function __construct ()
29
+ {
30
+ $ factory = new UuidFactory ();
31
+
32
+ $ this ->uuid = $ factory ->fromBytes (self ::UUID_BYTES [0 ]);
33
+ }
34
+
35
+ public function benchStringConversionOfUuid (): void
36
+ {
37
+ $ this ->uuid ->toString ();
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments