Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] Added toHexString method to AbstractUid class #45945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public function toRfc4122(): string
return substr_replace($uuid, '-', 23, 0);
}

/**
* Returns the identifier as a prefixed hexadecimal case insensitive string.
*/
public function toHex(): string
{
return '0x'.bin2hex($this->toBinary());
}

/**
* Returns whether the argument is an AbstractUid and contains the same value as the current instance.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Uid/Command/InspectUlidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
['toBase32 (canonical)', (string) $ulid],
['toBase58', $ulid->toBase58()],
['toRfc4122', $ulid->toRfc4122()],
['toHex', $ulid->toHex()],
new TableSeparator(),
['Time', ($ulid->getDateTime())->format('Y-m-d H:i:s.v \U\T\C')],
]);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Uid/Command/InspectUuidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
['toRfc4122 (canonical)', (string) $uuid],
['toBase58', $uuid->toBase58()],
['toBase32', $uuid->toBase32()],
['toHex', $uuid->toHex()],
];

if ($uuid instanceof UuidV1 || $uuid instanceof UuidV6) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test()
toBase32 (canonical) 01E439TP9XJZ9RPFH3T1PYBCR8
toBase58 1BKocMc5BnrVcuq2ti4Eqm
toRfc4122 0171069d-593d-97d3-8b3e-23d06de5b308
toHex 0x0171069d593d97d38b3e23d06de5b308
---------------------- --------------------------------------
Time 2020-03-23 08:58:27.517 UTC
---------------------- --------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Uid/Tests/Command/InspectUuidCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testNil()
toRfc4122 (canonical) 00000000-0000-0000-0000-000000000000
toBase58 1111111111111111111111
toBase32 00000000000000000000000000
toHex 0x00000000000000000000000000000000
----------------------- --------------------------------------


Expand All @@ -58,6 +59,7 @@ public function testUnknown()
toRfc4122 (canonical) 461cc9b9-2397-0dba-91e9-33af4c63f7ec
toBase58 9f9nftX6dw4oVPm5uT17um
toBase32 263K4VJ8WQ1PX93T9KNX667XZC
toHex 0x461cc9b923970dba91e933af4c63f7ec
----------------------- --------------------------------------


Expand All @@ -73,6 +75,7 @@ public function testUnknown()
toRfc4122 (canonical) 461cc9b9-2397-2dba-91e9-33af4c63f7ec
toBase58 9f9nftX6fjLfNnvSAHMV7Z
toBase32 263K4VJ8WQ5PX93T9KNX667XZC
toHex 0x461cc9b923972dba91e933af4c63f7ec
----------------------- --------------------------------------


Expand All @@ -88,6 +91,7 @@ public function testUnknown()
toRfc4122 (canonical) 461cc9b9-2397-7dba-91e9-33af4c63f7ec
toBase58 9f9nftX6kE2K6HpooNEQ83
toBase32 263K4VJ8WQFPX93T9KNX667XZC
toHex 0x461cc9b923977dba91e933af4c63f7ec
----------------------- --------------------------------------


Expand All @@ -103,6 +107,7 @@ public function testUnknown()
toRfc4122 (canonical) 461cc9b9-2397-cdba-91e9-33af4c63f7ec
toBase58 9f9nftX6pihxonjBST7K8X
toBase32 263K4VJ8WQSPX93T9KNX667XZC
toHex 0x461cc9b92397cdba91e933af4c63f7ec
----------------------- --------------------------------------


Expand All @@ -123,6 +128,7 @@ public function testV1()
toRfc4122 (canonical) 4c8e3a2a-5993-11eb-a861-2bf05af69e52
toBase58 ATJGVdrgFqvc6thDFXv1Qu
toBase32 2CHRX2MPCK27NTGR9BY1DFD7JJ
toHex 0x4c8e3a2a599311eba8612bf05af69e52
----------------------- --------------------------------------
Time 2021-01-18 13:44:34.438609 UTC
----------------------- --------------------------------------
Expand All @@ -145,6 +151,7 @@ public function testV3()
toRfc4122 (canonical) d108a1a0-957e-3c77-b110-d3f912374439
toBase58 Sp7q16VVeC7zPsMPVEToq2
toBase32 6H12GT15BY7HVV246KZ493EH1S
toHex 0xd108a1a0957e3c77b110d3f912374439
----------------------- --------------------------------------


Expand All @@ -165,6 +172,7 @@ public function testV4()
toRfc4122 (canonical) 705c6eab-a535-4f49-bd51-436d0e81206a
toBase58 EsjuVs1nd42xt7jSB8hNQH
toBase32 3GBHQAQ99N9X4VTMA3DM78283A
toHex 0x705c6eaba5354f49bd51436d0e81206a
----------------------- --------------------------------------


Expand All @@ -185,6 +193,7 @@ public function testV5()
toRfc4122 (canonical) 4ec6c3ad-de94-5f75-b5f0-ad56661a30c4
toBase58 AjCoyQeK6TtFemqYWV5uKZ
toBase32 2ERV1TVQMMBXTVBW5DASK1MC64
toHex 0x4ec6c3adde945f75b5f0ad56661a30c4
----------------------- --------------------------------------


Expand All @@ -205,6 +214,7 @@ public function testV6()
toRfc4122 (canonical) 1eb59937-b0a7-6288-a861-db3dc2d8d4db
toBase58 4nwhs6vwvNU2AbcCSD1XP8
toBase32 0YPPCKFC57CA4AGREV7Q1DHN6V
toHex 0x1eb59937b0a76288a861db3dc2d8d4db
----------------------- --------------------------------------
Time 2021-01-18 13:45:52.427892 UTC
----------------------- --------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function testBinary()
$this->assertTrue($ulid->equals(Ulid::fromString(hex2bin('7fffffffffffffffffffffffffffffff'))));
}

public function toHex()
{
$ulid = Ulid::fromString('1BVXue8CnY8ogucrHX3TeF');
$this->assertSame('0x0177058f4dacd0b2a990a49af02bc008', $ulid->toHex());
}

public function testFromUuid()
{
$uuid = new UuidV4();
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public function testBinary()
$this->assertSame(self::A_UUID_V4, (string) $uuid);
}

public function testHex()
{
$uuid = new UuidV4(self::A_UUID_V4);

$this->assertSame('0xd6b3345b29054048a83cb5988e765d98', $uuid->toHex());
}

public function testFromUlid()
{
$ulid = new Ulid();
Expand Down