BinaryUtil.php 0000644 00000013624 15025057123 0007345 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* @internal
*
* @author Nicolas Grekas
*/
class BinaryUtil
{
public const BASE10 = [
'' => '0123456789',
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,
];
// https://tools.ietf.org/html/rfc4122#section-4.1.4
// 0x01b21dd213814000 is the number of 100-ns intervals between the
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
private const TIME_OFFSET_INT = 0x01B21DD213814000;
private const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
private const TIME_OFFSET_COM1 = "\xfe\x4d\xe2\x2d\xec\x7e\xbf\xff";
private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
public static function toBase(string $bytes, array $map): string
{
$base = \strlen($alphabet = $map['']);
$bytes = array_values(unpack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', $bytes));
$digits = '';
while ($count = \count($bytes)) {
$quotient = [];
$remainder = 0;
for ($i = 0; $i !== $count; ++$i) {
$carry = $bytes[$i] + ($remainder << (\PHP_INT_SIZE >= 8 ? 16 : 8));
$digit = intdiv($carry, $base);
$remainder = $carry % $base;
if ($digit || $quotient) {
$quotient[] = $digit;
}
}
$digits = $alphabet[$remainder].$digits;
$bytes = $quotient;
}
return $digits;
}
public static function fromBase(string $digits, array $map): string
{
$base = \strlen($map['']);
$count = \strlen($digits);
$bytes = [];
while ($count) {
$quotient = [];
$remainder = 0;
for ($i = 0; $i !== $count; ++$i) {
$carry = ($bytes ? $digits[$i] : $map[$digits[$i]]) + $remainder * $base;
if (\PHP_INT_SIZE >= 8) {
$digit = $carry >> 16;
$remainder = $carry & 0xFFFF;
} else {
$digit = $carry >> 8;
$remainder = $carry & 0xFF;
}
if ($digit || $quotient) {
$quotient[] = $digit;
}
}
$bytes[] = $remainder;
$count = \count($digits = $quotient);
}
return pack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', ...array_reverse($bytes));
}
public static function add(string $a, string $b): string
{
$carry = 0;
for ($i = 7; 0 <= $i; --$i) {
$carry += \ord($a[$i]) + \ord($b[$i]);
$a[$i] = \chr($carry & 0xFF);
$carry >>= 8;
}
return $a;
}
/**
* @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
*/
public static function hexToDateTime(string $time): \DateTimeImmutable
{
if (\PHP_INT_SIZE >= 8) {
$time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
} else {
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
if (self::TIME_OFFSET_BIN <= $time) {
$time = self::add($time, self::TIME_OFFSET_COM2);
$time[0] = $time[0] & "\x7F";
$time = self::toBase($time, self::BASE10);
} else {
$time = self::add($time, self::TIME_OFFSET_COM1);
$time = '-'.self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
}
}
if (9 > \strlen($time)) {
$time = '-' === $time[0] ? '-'.str_pad(substr($time, 1), 8, '0', \STR_PAD_LEFT) : str_pad($time, 8, '0', \STR_PAD_LEFT);
}
return \DateTimeImmutable::createFromFormat('U.u?', substr_replace($time, '.', -7, 0));
}
/**
* @return string Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
*/
public static function dateTimeToHex(\DateTimeInterface $time): string
{
if (\PHP_INT_SIZE >= 8) {
if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
}
return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
}
$time = $time->format('Uu0');
$negative = '-' === $time[0];
if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
}
$time = self::fromBase($time, self::BASE10);
$time = str_pad($time, 8, "\0", \STR_PAD_LEFT);
if ($negative) {
$time = self::add($time, self::TIME_OFFSET_COM1) ^ "\xff\xff\xff\xff\xff\xff\xff\xff";
} else {
$time = self::add($time, self::TIME_OFFSET_BIN);
}
return bin2hex($time);
}
}
composer.json 0000644 00000001627 15025057123 0007274 0 ustar 00 {
"name": "symfony/uid",
"type": "library",
"description": "Provides an object-oriented API to generate and represent UIDs",
"keywords": ["uid", "uuid", "ulid"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Grégoire Pineau",
"email": "lyrixx@lyrixx.info"
},
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.0.2",
"symfony/polyfill-uuid": "^1.15"
},
"require-dev": {
"symfony/console": "^5.4|^6.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Uid\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}
UuidV6.php 0000644 00000003764 15025057123 0006411 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A v6 UUID is lexicographically sortable and contains a 60-bit timestamp and 62 extra unique bits.
*
* Unlike UUIDv1, this implementation of UUIDv6 doesn't leak the MAC address of the host.
*
* @author Nicolas Grekas
*/
class UuidV6 extends Uuid
{
protected const TYPE = 6;
private static string $node;
public function __construct(string $uuid = null)
{
if (null === $uuid) {
$this->uid = static::generate();
} else {
parent::__construct($uuid, true);
}
}
public function getDateTime(): \DateTimeImmutable
{
return BinaryUtil::hexToDateTime('0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3));
}
public function getNode(): string
{
return substr($this->uid, 24);
}
public static function generate(\DateTimeInterface $time = null, Uuid $node = null): string
{
$uuidV1 = UuidV1::generate($time, $node);
$uuid = substr($uuidV1, 15, 3).substr($uuidV1, 9, 4).$uuidV1[0].'-'.substr($uuidV1, 1, 4).'-6'.substr($uuidV1, 5, 3).substr($uuidV1, 18, 6);
if ($node) {
return $uuid.substr($uuidV1, 24);
}
// uuid_create() returns a stable "node" that can leak the MAC of the host, but
// UUIDv6 prefers a truly random number here, let's XOR both to preserve the entropy
if (!isset(self::$node)) {
$seed = [random_int(0, 0xFFFFFF), random_int(0, 0xFFFFFF)];
$node = unpack('N2', hex2bin('00'.substr($uuidV1, 24, 6)).hex2bin('00'.substr($uuidV1, 30)));
self::$node = sprintf('%06x%06x', ($seed[0] ^ $node[1]) | 0x010000, $seed[1] ^ $node[2]);
}
return $uuid.self::$node;
}
}
CHANGELOG.md 0000644 00000001463 15025057123 0006361 0 ustar 00 CHANGELOG
=========
5.4
---
* Add `NilUlid`
5.3
---
* The component is not marked as `@experimental` anymore
* Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()`
* [BC BREAK] Replace `UuidV1::getTime()`, `UuidV6::getTime()` and `Ulid::getTime()` by `UuidV1::getDateTime()`, `UuidV6::getDateTime()` and `Ulid::getDateTime()`
* Add `Uuid::NAMESPACE_*` constants from RFC4122
* Add `UlidFactory`, `UuidFactory`, `RandomBasedUuidFactory`, `TimeBasedUuidFactory` and `NameBasedUuidFactory`
* Add commands to generate and inspect UUIDs and ULIDs
5.2.0
-----
* made UUIDv6 always return truly random node fields to prevent leaking the MAC of the host
5.1.0
-----
* added support for UUID
* added support for ULID
* added the component
Ulid.php 0000644 00000014661 15025057123 0006162 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A ULID is lexicographically sortable and contains a 48-bit timestamp and 80-bit of crypto-random entropy.
*
* @see https://github.com/ulid/spec
*
* @author Nicolas Grekas
*/
class Ulid extends AbstractUid
{
protected const NIL = '00000000000000000000000000';
private static string $time = '';
private static array $rand = [];
public function __construct(string $ulid = null)
{
if (null === $ulid) {
$this->uid = static::generate();
return;
}
if (self::NIL === $ulid) {
$this->uid = $ulid;
return;
}
if (!self::isValid($ulid)) {
throw new \InvalidArgumentException(sprintf('Invalid ULID: "%s".', $ulid));
}
$this->uid = strtoupper($ulid);
}
public static function isValid(string $ulid): bool
{
if (26 !== \strlen($ulid)) {
return false;
}
if (26 !== strspn($ulid, '0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz')) {
return false;
}
return $ulid[0] <= '7';
}
/**
* {@inheritdoc}
*/
public static function fromString(string $ulid): static
{
if (36 === \strlen($ulid) && preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $ulid)) {
$ulid = uuid_parse($ulid);
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
$ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
}
if (16 !== \strlen($ulid)) {
if (self::NIL === $ulid) {
return new NilUlid();
}
return new static($ulid);
}
$ulid = bin2hex($ulid);
$ulid = sprintf('%02s%04s%04s%04s%04s%04s%04s',
base_convert(substr($ulid, 0, 2), 16, 32),
base_convert(substr($ulid, 2, 5), 16, 32),
base_convert(substr($ulid, 7, 5), 16, 32),
base_convert(substr($ulid, 12, 5), 16, 32),
base_convert(substr($ulid, 17, 5), 16, 32),
base_convert(substr($ulid, 22, 5), 16, 32),
base_convert(substr($ulid, 27, 5), 16, 32)
);
if (self::NIL === $ulid) {
return new NilUlid();
}
$u = new static(self::NIL);
$u->uid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
return $u;
}
public function toBinary(): string
{
$ulid = strtr($this->uid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
$ulid = sprintf('%02s%05s%05s%05s%05s%05s%05s',
base_convert(substr($ulid, 0, 2), 32, 16),
base_convert(substr($ulid, 2, 4), 32, 16),
base_convert(substr($ulid, 6, 4), 32, 16),
base_convert(substr($ulid, 10, 4), 32, 16),
base_convert(substr($ulid, 14, 4), 32, 16),
base_convert(substr($ulid, 18, 4), 32, 16),
base_convert(substr($ulid, 22, 4), 32, 16)
);
return hex2bin($ulid);
}
public function toBase32(): string
{
return $this->uid;
}
public function getDateTime(): \DateTimeImmutable
{
$time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
if (\PHP_INT_SIZE >= 8) {
$time = (string) hexdec(base_convert($time, 32, 16));
} else {
$time = sprintf('%02s%05s%05s',
base_convert(substr($time, 0, 2), 32, 16),
base_convert(substr($time, 2, 4), 32, 16),
base_convert(substr($time, 6, 4), 32, 16)
);
$time = BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
}
if (4 > \strlen($time)) {
$time = '000'.$time;
}
return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -3, 0));
}
public static function generate(\DateTimeInterface $time = null): string
{
if (null === $mtime = $time) {
$time = microtime(false);
$time = substr($time, 11).substr($time, 2, 3);
} elseif (0 > $time = $time->format('Uv')) {
throw new \InvalidArgumentException('The timestamp must be positive.');
}
if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {
randomize:
$r = unpack('nr1/nr2/nr3/nr4/nr', random_bytes(10));
$r['r1'] |= ($r['r'] <<= 4) & 0xF0000;
$r['r2'] |= ($r['r'] <<= 4) & 0xF0000;
$r['r3'] |= ($r['r'] <<= 4) & 0xF0000;
$r['r4'] |= ($r['r'] <<= 4) & 0xF0000;
unset($r['r']);
self::$rand = array_values($r);
self::$time = $time;
} elseif ([0xFFFFF, 0xFFFFF, 0xFFFFF, 0xFFFFF] === self::$rand) {
if (\PHP_INT_SIZE >= 8 || 10 > \strlen($time = self::$time)) {
$time = (string) (1 + $time);
} elseif ('999999999' === $mtime = substr($time, -9)) {
$time = (1 + substr($time, 0, -9)).'000000000';
} else {
$time = substr_replace($time, str_pad(++$mtime, 9, '0', \STR_PAD_LEFT), -9);
}
goto randomize;
} else {
for ($i = 3; $i >= 0 && 0xFFFFF === self::$rand[$i]; --$i) {
self::$rand[$i] = 0;
}
++self::$rand[$i];
$time = self::$time;
}
if (\PHP_INT_SIZE >= 8) {
$time = base_convert($time, 10, 32);
} else {
$time = str_pad(bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)), 12, '0', \STR_PAD_LEFT);
$time = sprintf('%s%04s%04s',
base_convert(substr($time, 0, 2), 16, 32),
base_convert(substr($time, 2, 5), 16, 32),
base_convert(substr($time, 7, 5), 16, 32)
);
}
return strtr(sprintf('%010s%04s%04s%04s%04s',
$time,
base_convert(self::$rand[0], 10, 32),
base_convert(self::$rand[1], 10, 32),
base_convert(self::$rand[2], 10, 32),
base_convert(self::$rand[3], 10, 32)
), 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
}
}
UuidV1.php 0000644 00000003764 15025057123 0006404 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A v1 UUID contains a 60-bit timestamp and 62 extra unique bits.
*
* @author Grégoire Pineau
*/
class UuidV1 extends Uuid
{
protected const TYPE = 1;
private static ?string $clockSeq = null;
public function __construct(string $uuid = null)
{
if (null === $uuid) {
$this->uid = uuid_create(static::TYPE);
} else {
parent::__construct($uuid, true);
}
}
public function getDateTime(): \DateTimeImmutable
{
return BinaryUtil::hexToDateTime('0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8));
}
public function getNode(): string
{
return uuid_mac($this->uid);
}
public static function generate(\DateTimeInterface $time = null, Uuid $node = null): string
{
$uuid = !$time || !$node ? uuid_create(static::TYPE) : parent::NIL;
if ($time) {
if ($node) {
// use clock_seq from the node
$seq = substr($node->uid, 19, 4);
} else {
// generate a static random clock_seq to prevent any collisions with the real one
$seq = substr($uuid, 19, 4);
while (null === self::$clockSeq || $seq === self::$clockSeq) {
self::$clockSeq = sprintf('%04x', random_int(0, 0x3FFF) | 0x8000);
}
$seq = self::$clockSeq;
}
$time = BinaryUtil::dateTimeToHex($time);
$uuid = substr($time, 8).'-'.substr($time, 4, 4).'-1'.substr($time, 1, 3).'-'.$seq.substr($uuid, 23);
}
if ($node) {
$uuid = substr($uuid, 0, 24).substr($node->uid, 24);
}
return $uuid;
}
}
UuidV3.php 0000644 00000001120 15025057123 0006366 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A v3 UUID contains an MD5 hash of another UUID and a name.
*
* Use Uuid::v3() to compute one.
*
* @author Grégoire Pineau
*/
class UuidV3 extends Uuid
{
protected const TYPE = 3;
public function __construct(string $uuid)
{
parent::__construct($uuid, true);
}
}
Command/InspectUlidCommand.php 0000644 00000004242 15025057123 0012357 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Ulid;
#[AsCommand(name: 'ulid:inspect', description: 'Inspect a ULID')]
class InspectUlidCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDefinition([
new InputArgument('ulid', InputArgument::REQUIRED, 'The ULID to inspect'),
])
->setHelp(<<<'EOF'
The %command.name% displays information about a ULID.
php %command.full_name% 01EWAKBCMWQ2C94EXNN60ZBS0Q
php %command.full_name% 1BVdfLn3ERmbjYBLCdaaLW
php %command.full_name% 01771535-b29c-b898-923b-b5a981f5e417
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
try {
$ulid = Ulid::fromString($input->getArgument('ulid'));
} catch (\InvalidArgumentException $e) {
$io->error($e->getMessage());
return 1;
}
$io->table(['Label', 'Value'], [
['toBase32 (canonical)', (string) $ulid],
['toBase58', $ulid->toBase58()],
['toRfc4122', $ulid->toRfc4122()],
new TableSeparator(),
['Time', $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C')],
]);
return 0;
}
}
Command/GenerateUuidCommand.php 0000644 00000016543 15025057123 0012524 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Factory\UuidFactory;
use Symfony\Component\Uid\Uuid;
#[AsCommand(name: 'uuid:generate', description: 'Generate a UUID')]
class GenerateUuidCommand extends Command
{
private $factory;
public function __construct(UuidFactory $factory = null)
{
$this->factory = $factory ?? new UuidFactory();
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDefinition([
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'),
new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'),
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'),
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs, predefined namespaces keywords "dns", "url", "oid" and "x500" are accepted'),
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: rfc4122, base58 or base32', 'rfc4122'),
])
->setHelp(<<<'EOF'
The %command.name% generates a UUID.
php %command.full_name%
To generate a time-based UUID:
php %command.full_name% --time-based=now
To specify a time-based UUID's node:
php %command.full_name% --time-based=@1613480254 --node=fb3502dc-137e-4849-8886-ac90d07f64a7
To generate a name-based UUID:
php %command.full_name% --name-based=foo
To specify a name-based UUID's namespace:
php %command.full_name% --name-based=bar --namespace=fb3502dc-137e-4849-8886-ac90d07f64a7
To generate a random-based UUID:
php %command.full_name% --random-based
To generate several UUIDs:
php %command.full_name% --count=10
To output a specific format:
php %command.full_name% --format=base58
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
$time = $input->getOption('time-based');
$node = $input->getOption('node');
$name = $input->getOption('name-based');
$namespace = $input->getOption('namespace');
$random = $input->getOption('random-based');
if (false !== ($time ?? $name ?? $random) && 1 < ((null !== $time) + (null !== $name) + $random)) {
$io->error('Only one of "--time-based", "--name-based" or "--random-based" can be provided at a time.');
return 1;
}
if (null === $time && null !== $node) {
$io->error('Option "--node" can only be used with "--time-based".');
return 1;
}
if (null === $name && null !== $namespace) {
$io->error('Option "--namespace" can only be used with "--name-based".');
return 1;
}
switch (true) {
case null !== $time:
if (null !== $node) {
try {
$node = Uuid::fromString($node);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('Invalid node "%s": %s', $node, $e->getMessage()));
return 1;
}
}
try {
new \DateTimeImmutable($time);
} catch (\Exception $e) {
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage())));
return 1;
}
$create = function () use ($node, $time): Uuid {
return $this->factory->timeBased($node)->create(new \DateTimeImmutable($time));
};
break;
case null !== $name:
if ($namespace && !\in_array($namespace, ['dns', 'url', 'oid', 'x500'], true)) {
try {
$namespace = Uuid::fromString($namespace);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('Invalid namespace "%s": %s', $namespace, $e->getMessage()));
return 1;
}
}
$create = function () use ($namespace, $name): Uuid {
try {
$factory = $this->factory->nameBased($namespace);
} catch (\LogicException $e) {
throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.');
}
return $factory->create($name);
};
break;
case $random:
$create = [$this->factory->randomBased(), 'create'];
break;
default:
$create = [$this->factory, 'create'];
break;
}
$formatOption = $input->getOption('format');
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $formatOption));
return 1;
}
$count = (int) $input->getOption('count');
try {
for ($i = 0; $i < $count; ++$i) {
$output->writeln($create()->$format());
}
} catch (\Exception $e) {
$io->error($e->getMessage());
return 1;
}
return 0;
}
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues($this->getAvailableFormatOptions());
}
}
private function getAvailableFormatOptions(): array
{
return [
'base32',
'base58',
'rfc4122',
];
}
}
Command/InspectUuidCommand.php 0000644 00000005147 15025057123 0012375 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV6;
#[AsCommand(name: 'uuid:inspect', description: 'Inspect a UUID')]
class InspectUuidCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDefinition([
new InputArgument('uuid', InputArgument::REQUIRED, 'The UUID to inspect'),
])
->setHelp(<<<'EOF'
The %command.name% displays information about a UUID.
php %command.full_name% a7613e0a-5986-11eb-a861-2bf05af69e52
php %command.full_name% MfnmaUvvQ1h8B14vTwt6dX
php %command.full_name% 57C4Z0MPC627NTGR9BY1DFD7JJ
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
try {
/** @var Uuid $uuid */
$uuid = Uuid::fromString($input->getArgument('uuid'));
} catch (\InvalidArgumentException $e) {
$io->error($e->getMessage());
return 1;
}
if (-1 === $version = uuid_type($uuid)) {
$version = 'nil';
} elseif (0 === $version || 2 === $version || 6 < $version) {
$version = 'unknown';
}
$rows = [
['Version', $version],
['toRfc4122 (canonical)', (string) $uuid],
['toBase58', $uuid->toBase58()],
['toBase32', $uuid->toBase32()],
];
if ($uuid instanceof UuidV1 || $uuid instanceof UuidV6) {
$rows[] = new TableSeparator();
$rows[] = ['Time', $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C')];
}
$io->table(['Label', 'Value'], $rows);
return 0;
}
}
Command/GenerateUlidCommand.php 0000644 00000007201 15025057123 0012502 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Factory\UlidFactory;
#[AsCommand(name: 'ulid:generate', description: 'Generate a ULID')]
class GenerateUlidCommand extends Command
{
private const FORMAT_OPTIONS = [
'base32',
'base58',
'rfc4122',
];
private $factory;
public function __construct(UlidFactory $factory = null)
{
$this->factory = $factory ?? new UlidFactory();
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDefinition([
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'),
])
->setHelp(<<<'EOF'
The %command.name% command generates a ULID.
php %command.full_name%
To specify the timestamp:
php %command.full_name% --time="2021-02-16 14:09:08"
To generate several ULIDs:
php %command.full_name% --count=10
To output a specific format:
php %command.full_name% --format=rfc4122
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
if (null !== $time = $input->getOption('time')) {
try {
$time = new \DateTimeImmutable($time);
} catch (\Exception $e) {
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage())));
return 1;
}
}
$formatOption = $input->getOption('format');
if (\in_array($formatOption, self::FORMAT_OPTIONS)) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
return 1;
}
$count = (int) $input->getOption('count');
try {
for ($i = 0; $i < $count; ++$i) {
$output->writeln($this->factory->create($time)->$format());
}
} catch (\Exception $e) {
$io->error($e->getMessage());
return 1;
}
return 0;
}
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(self::FORMAT_OPTIONS);
}
}
}
UuidV5.php 0000644 00000001120 15025057123 0006370 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A v5 UUID contains a SHA1 hash of another UUID and a name.
*
* Use Uuid::v5() to compute one.
*
* @author Grégoire Pineau
*/
class UuidV5 extends Uuid
{
protected const TYPE = 5;
public function __construct(string $uuid)
{
parent::__construct($uuid, true);
}
}
README.md 0000644 00000000744 15025057123 0006030 0 ustar 00 Uid Component
=============
The UID component provides an object-oriented API to generate and represent UIDs.
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/uid.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
LICENSE 0000644 00000002051 15025057123 0005547 0 ustar 00 Copyright (c) 2020-2023 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
AbstractUid.php 0000644 00000010402 15025057123 0007457 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* @author Nicolas Grekas
*/
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.
*
* @throws \InvalidArgumentException When the passed value is not valid
*/
abstract public static function fromString(string $uid): static;
/**
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBinary(string $uid): static
{
if (16 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid binary uid provided.');
}
return static::fromString($uid);
}
/**
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBase58(string $uid): static
{
if (22 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid base-58 uid provided.');
}
return static::fromString($uid);
}
/**
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromBase32(string $uid): static
{
if (26 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid base-32 uid provided.');
}
return static::fromString($uid);
}
/**
* @throws \InvalidArgumentException When the passed value is not valid
*/
public static function fromRfc4122(string $uid): static
{
if (36 !== \strlen($uid)) {
throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
}
return static::fromString($uid);
}
/**
* Returns the identifier as a raw binary string.
*/
abstract public function toBinary(): string;
/**
* Returns the identifier as a base58 case sensitive string.
*/
public function toBase58(): string
{
return strtr(sprintf('%022s', BinaryUtil::toBase($this->toBinary(), BinaryUtil::BASE58)), '0', '1');
}
/**
* Returns the identifier as a base32 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
{
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($this->toBinary());
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
/**
* Returns whether the argument is an AbstractUid and contains the same value as the current instance.
*/
public function equals(mixed $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;
}
}
Uuid.php 0000644 00000012053 15025057123 0006164 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* @author Grégoire Pineau
*
* @see https://tools.ietf.org/html/rfc4122#appendix-C for details about namespaces
*/
class Uuid extends AbstractUid
{
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
protected const TYPE = 0;
protected const NIL = '00000000-0000-0000-0000-000000000000';
public function __construct(string $uuid, bool $checkVariant = false)
{
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
if (false === $type || (static::TYPE ?: $type) !== $type) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}
$this->uid = strtolower($uuid);
if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}
}
public static function fromString(string $uuid): static
{
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58[''])) {
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
}
if (16 === \strlen($uuid)) {
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($uuid);
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
$uuid = substr_replace($uuid, '-', 23, 0);
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
$ulid = new NilUlid();
$ulid->uid = strtoupper($uuid);
$uuid = $ulid->toRfc4122();
}
if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
return new static($uuid);
}
if (self::NIL === $uuid) {
return new NilUuid();
}
if (\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) {
switch ($uuid[14]) {
case UuidV1::TYPE: return new UuidV1($uuid);
case UuidV3::TYPE: return new UuidV3($uuid);
case UuidV4::TYPE: return new UuidV4($uuid);
case UuidV5::TYPE: return new UuidV5($uuid);
case UuidV6::TYPE: return new UuidV6($uuid);
}
}
return new self($uuid);
}
final public static function v1(): UuidV1
{
return new UuidV1();
}
final public static function v3(self $namespace, string $name): UuidV3
{
// don't use uuid_generate_md5(), some versions are buggy
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
return new UuidV3(self::format($uuid, '-3'));
}
final public static function v4(): UuidV4
{
return new UuidV4();
}
final public static function v5(self $namespace, string $name): UuidV5
{
// don't use uuid_generate_sha1(), some versions are buggy
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
return new UuidV5(self::format($uuid, '-5'));
}
final public static function v6(): UuidV6
{
return new UuidV6();
}
public static function isValid(string $uuid): bool
{
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
return true;
}
if (__CLASS__ === static::class && 'ffffffff-ffff-ffff-ffff-ffffffffffff' === strtr($uuid, 'F', 'f')) {
return true;
}
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
return false;
}
return __CLASS__ === static::class || static::TYPE === (int) $uuid[14];
}
public function toBinary(): string
{
return uuid_parse($this->uid);
}
public function toRfc4122(): string
{
return $this->uid;
}
public function compare(AbstractUid $other): int
{
if (false !== $cmp = uuid_compare($this->uid, $other->uid)) {
return $cmp;
}
return parent::compare($other);
}
private static function format(string $uuid, string $version): string
{
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
$uuid = substr_replace($uuid, $version, 13, 1);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
}
UuidV4.php 0000644 00000001643 15025057123 0006401 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* A v4 UUID contains a 122-bit random number.
*
* @author Grégoire Pineau
*/
class UuidV4 extends Uuid
{
protected const TYPE = 4;
public function __construct(string $uuid = null)
{
if (null === $uuid) {
$uuid = random_bytes(16);
$uuid[6] = $uuid[6] & "\x0F" | "\x40";
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = bin2hex($uuid);
$this->uid = substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 12, 4).'-'.substr($uuid, 16, 4).'-'.substr($uuid, 20, 12);
} else {
parent::__construct($uuid, true);
}
}
}
NilUuid.php 0000644 00000000730 15025057123 0006626 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
/**
* @author Grégoire Pineau
*/
class NilUuid extends Uuid
{
protected const TYPE = -1;
public function __construct()
{
$this->uid = parent::NIL;
}
}
NilUlid.php 0000644 00000000577 15025057123 0006626 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid;
class NilUlid extends Ulid
{
public function __construct()
{
$this->uid = parent::NIL;
}
}
Factory/NameBasedUuidFactory.php 0000644 00000002120 15025057123 0012655 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Factory;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV3;
use Symfony\Component\Uid\UuidV5;
class NameBasedUuidFactory
{
private string $class;
private $namespace;
public function __construct(string $class, Uuid $namespace)
{
$this->class = $class;
$this->namespace = $namespace;
}
public function create(string $name): UuidV5|UuidV3
{
switch ($class = $this->class) {
case UuidV5::class: return Uuid::v5($this->namespace, $name);
case UuidV3::class: return Uuid::v3($this->namespace, $name);
}
if (is_subclass_of($class, UuidV5::class)) {
$uuid = Uuid::v5($this->namespace, $name);
} else {
$uuid = Uuid::v3($this->namespace, $name);
}
return new $class($uuid);
}
}
Factory/UuidFactory.php 0000644 00000006403 15025057123 0011125 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Factory;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV5;
use Symfony\Component\Uid\UuidV6;
class UuidFactory
{
private string $defaultClass;
private string $timeBasedClass;
private string $nameBasedClass;
private string $randomBasedClass;
private $timeBasedNode;
private $nameBasedNamespace;
public function __construct(string|int $defaultClass = UuidV6::class, string|int $timeBasedClass = UuidV6::class, string|int $nameBasedClass = UuidV5::class, string|int $randomBasedClass = UuidV4::class, Uuid|string $timeBasedNode = null, Uuid|string $nameBasedNamespace = null)
{
if (null !== $timeBasedNode && !$timeBasedNode instanceof Uuid) {
$timeBasedNode = Uuid::fromString($timeBasedNode);
}
if (null !== $nameBasedNamespace) {
$nameBasedNamespace = $this->getNamespace($nameBasedNamespace);
}
$this->defaultClass = is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass : $defaultClass;
$this->timeBasedClass = is_numeric($timeBasedClass) ? Uuid::class.'V'.$timeBasedClass : $timeBasedClass;
$this->nameBasedClass = is_numeric($nameBasedClass) ? Uuid::class.'V'.$nameBasedClass : $nameBasedClass;
$this->randomBasedClass = is_numeric($randomBasedClass) ? Uuid::class.'V'.$randomBasedClass : $randomBasedClass;
$this->timeBasedNode = $timeBasedNode;
$this->nameBasedNamespace = $nameBasedNamespace;
}
public function create(): UuidV6|UuidV4|UuidV1
{
$class = $this->defaultClass;
return new $class();
}
public function randomBased(): RandomBasedUuidFactory
{
return new RandomBasedUuidFactory($this->randomBasedClass);
}
public function timeBased(Uuid|string $node = null): TimeBasedUuidFactory
{
$node ?? $node = $this->timeBasedNode;
if (null !== $node && !$node instanceof Uuid) {
$node = Uuid::fromString($node);
}
return new TimeBasedUuidFactory($this->timeBasedClass, $node);
}
public function nameBased(Uuid|string $namespace = null): NameBasedUuidFactory
{
$namespace ?? $namespace = $this->nameBasedNamespace;
if (null === $namespace) {
throw new \LogicException(sprintf('A namespace should be defined when using "%s()".', __METHOD__));
}
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
}
private function getNamespace(Uuid|string $namespace): Uuid
{
if ($namespace instanceof Uuid) {
return $namespace;
}
switch ($namespace) {
case 'dns': return new UuidV1(Uuid::NAMESPACE_DNS);
case 'url': return new UuidV1(Uuid::NAMESPACE_URL);
case 'oid': return new UuidV1(Uuid::NAMESPACE_OID);
case 'x500': return new UuidV1(Uuid::NAMESPACE_X500);
default: return Uuid::fromString($namespace);
}
}
}
Factory/RandomBasedUuidFactory.php 0000644 00000001101 15025057123 0013213 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Factory;
use Symfony\Component\Uid\UuidV4;
class RandomBasedUuidFactory
{
private string $class;
public function __construct(string $class)
{
$this->class = $class;
}
public function create(): UuidV4
{
$class = $this->class;
return new $class();
}
}
Factory/TimeBasedUuidFactory.php 0000644 00000001560 15025057123 0012702 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Factory;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV6;
class TimeBasedUuidFactory
{
private string $class;
private $node;
public function __construct(string $class, Uuid $node = null)
{
$this->class = $class;
$this->node = $node;
}
public function create(\DateTimeInterface $time = null): UuidV6|UuidV1
{
$class = $this->class;
if (null === $time && null === $this->node) {
return new $class();
}
return new $class($class::generate($time, $this->node));
}
}
Factory/UlidFactory.php 0000644 00000000745 15025057123 0011117 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Factory;
use Symfony\Component\Uid\Ulid;
class UlidFactory
{
public function create(\DateTimeInterface $time = null): Ulid
{
return new Ulid(null === $time ? null : Ulid::generate($time));
}
}