Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Uid] add AbstractUid and interop with base-58/32/RFC4122 encodings #36074

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
Mar 15, 2020
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
106 changes: 106 additions & 0 deletions 106 src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Uid;

/**
* @experimental in 5.1
*
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractUid implements \JsonSerializable
{
/**
* The identifier in its canonic representation.
*/
protected $uid;

/**
* Whether the passed value is valid for the constructor of the current class.
*/
abstract public static function isValid(string $uid): bool;

/**
* Creates an AbstractUid from an identifier represented in any of the supported formats.
*
* @return static
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
abstract public static function fromString(string $uid): self;

/**
* Returns the identifier as a raw binary string.
*/
abstract public function toBinary(): string;

/**
* Returns the identifier as a base-58 case sensitive string.
*/
public function toBase58(): string
{
return strtr(sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
}

/**
* Returns the identifier as a base-32 case insensitive string.
*/
public function toBase32(): string
{
$uid = bin2hex($this->toBinary());
$uid = sprintf('%02s%04s%04s%04s%04s%04s%04s',
base_convert(substr($uid, 0, 2), 16, 32),
base_convert(substr($uid, 2, 5), 16, 32),
base_convert(substr($uid, 7, 5), 16, 32),
base_convert(substr($uid, 12, 5), 16, 32),
base_convert(substr($uid, 17, 5), 16, 32),
base_convert(substr($uid, 22, 5), 16, 32),
base_convert(substr($uid, 27, 5), 16, 32)
);

return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
}

/**
* Returns the identifier as a RFC4122 case insensitive string.
*/
public function toRfc4122(): string
{
return uuid_unparse($this->toBinary());
}

/**
* Returns whether the argument is an AbstractUid and contains the same value as the current instance.
*/
public function equals($other): bool
{
if (!$other instanceof self) {
return false;
}

return $this->uid === $other->uid;
}

public function compare(self $other): int
{
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
}

public function __toString(): string
{
return $this->uid;
}

public function jsonSerialize(): string
{
return $this->uid;
}
}
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/Uid/BinaryUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ class BinaryUtil
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
];

public const BASE58 = [
'' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
1 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 'A' => 9,
'B' => 10, 'C' => 11, 'D' => 12, 'E' => 13, 'F' => 14, 'G' => 15,
'H' => 16, 'J' => 17, 'K' => 18, 'L' => 19, 'M' => 20, 'N' => 21,
'P' => 22, 'Q' => 23, 'R' => 24, 'S' => 25, 'T' => 26, 'U' => 27,
'V' => 28, 'W' => 29, 'X' => 30, 'Y' => 31, 'Z' => 32, 'a' => 33,
'b' => 34, 'c' => 35, 'd' => 36, 'e' => 37, 'f' => 38, 'g' => 39,
'h' => 40, 'i' => 41, 'j' => 42, 'k' => 43, 'm' => 44, 'n' => 45,
'o' => 46, 'p' => 47, 'q' => 48, 'r' => 49, 's' => 50, 't' => 51,
'u' => 52, 'v' => 53, 'w' => 54, 'x' => 55, 'y' => 56, 'z' => 57,
];

public static function toBase(string $bytes, array $map): string
{
$base = \strlen($alphabet = $map['']);
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Uid/NilUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class NilUuid extends Uuid

public function __construct()
{
$this->uuid = '00000000-0000-0000-0000-000000000000';
$this->uid = '00000000-0000-0000-0000-000000000000';
}
}
23 changes: 23 additions & 0 deletions 23 src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;

class UlidTest extends TestCase
{
Expand Down Expand Up @@ -49,6 +50,28 @@ public function testBinary()
$this->assertTrue($ulid->equals(Ulid::fromString(hex2bin('7fffffffffffffffffffffffffffffff'))));
}

public function testFromUuid()
{
$uuid = new UuidV4();

$ulid = Ulid::fromString($uuid);

$this->assertSame($uuid->toBase32(), (string) $ulid);
$this->assertSame($ulid->toBase32(), (string) $ulid);
$this->assertSame((string) $uuid, $ulid->toRfc4122());
$this->assertTrue($ulid->equals(Ulid::fromString($uuid)));
}

public function testBase58()
{
$ulid = new Ulid('00000000000000000000000000');
$this->assertSame('1111111111111111111111', $ulid->toBase58());

$ulid = Ulid::fromString("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
$this->assertSame('YcVfxkQb6JRzqk5kF2tNLv', $ulid->toBase58());
$this->assertTrue($ulid->equals(Ulid::fromString('YcVfxkQb6JRzqk5kF2tNLv')));
}

/**
* @group time-sensitive
*/
Expand Down
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\NilUuid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV3;
Expand Down Expand Up @@ -95,6 +96,26 @@ public function testBinary()
$this->assertSame(self::A_UUID_V4, (string) $uuid);
}

public function testFromUlid()
{
$ulid = new Ulid();
$uuid = Uuid::fromString($ulid);

$this->assertSame((string) $ulid, $uuid->toBase32());
$this->assertSame((string) $uuid, $uuid->toRfc4122());
$this->assertTrue($uuid->equals(Uuid::fromString($ulid)));
}

public function testBase58()
{
$uuid = new NilUuid();
$this->assertSame('1111111111111111111111', $uuid->toBase58());

$uuid = Uuid::fromString("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
$this->assertSame('YcVfxkQb6JRzqk5kF2tNLv', $uuid->toBase58());
$this->assertTrue($uuid->equals(Uuid::fromString('YcVfxkQb6JRzqk5kF2tNLv')));
}

public function testIsValid()
{
$this->assertFalse(Uuid::isValid('not a uuid'));
Expand Down
51 changes: 18 additions & 33 deletions 51 src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class Ulid implements \JsonSerializable
class Ulid extends AbstractUid
{
private static $time = -1;
private static $rand = [];

private $ulid;

public function __construct(string $ulid = null)
{
if (null === $ulid) {
$this->ulid = self::generate();
$this->uid = self::generate();

return;
}
Expand All @@ -39,7 +37,7 @@ public function __construct(string $ulid = null)
throw new \InvalidArgumentException(sprintf('Invalid ULID: "%s".', $ulid));
}

$this->ulid = strtr($ulid, 'abcdefghjkmnpqrstvwxyz', 'ABCDEFGHJKMNPQRSTVWXYZ');
$this->uid = strtr($ulid, 'abcdefghjkmnpqrstvwxyz', 'ABCDEFGHJKMNPQRSTVWXYZ');
}

public static function isValid(string $ulid): bool
Expand All @@ -55,8 +53,17 @@ public static function isValid(string $ulid): bool
return $ulid[0] <= '7';
}

public static function fromString(string $ulid): self
/**
* {@inheritdoc}
*/
public static function fromString(string $ulid): parent
{
if (36 === \strlen($ulid) && Uuid::isValid($ulid)) {
$ulid = Uuid::fromString($ulid)->toBinary();
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
$ulid = BinaryUtil::fromBase($ulid, BinaryUtil::BASE58);
}

if (16 !== \strlen($ulid)) {
return new static($ulid);
}
Expand All @@ -75,9 +82,9 @@ public static function fromString(string $ulid): self
return new self(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
}

public function toBinary()
public function toBinary(): string
{
$ulid = strtr($this->ulid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
$ulid = strtr($this->uid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');

$ulid = sprintf('%02s%05s%05s%05s%05s%05s%05s',
base_convert(substr($ulid, 0, 2), 32, 16),
Expand All @@ -92,26 +99,14 @@ public function toBinary()
return hex2bin($ulid);
}

/**
* Returns whether the argument is of class Ulid and contains the same value as the current instance.
*/
public function equals($other): bool
public function toBase32(): string
{
if (!$other instanceof self) {
return false;
}

return $this->ulid === $other->ulid;
}

public function compare(self $other): int
{
return $this->ulid <=> $other->ulid;
return $this->uid;
}

public function getTime(): float
{
$time = strtr(substr($this->ulid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
$time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');

if (\PHP_INT_SIZE >= 8) {
return hexdec(base_convert($time, 32, 16)) / 1000;
Expand All @@ -126,16 +121,6 @@ public function getTime(): float
return BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10) / 1000;
}

public function __toString(): string
{
return $this->ulid;
}

public function jsonSerialize(): string
{
return $this->ulid;
}

private static function generate(): string
{
$time = microtime(false);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.