| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Uid\Factory;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Uid\Uuid;
|
|
|
15 |
use Symfony\Component\Uid\UuidV1;
|
|
|
16 |
use Symfony\Component\Uid\UuidV4;
|
|
|
17 |
use Symfony\Component\Uid\UuidV5;
|
|
|
18 |
use Symfony\Component\Uid\UuidV6;
|
|
|
19 |
|
|
|
20 |
class UuidFactory
|
|
|
21 |
{
|
|
|
22 |
private string $defaultClass;
|
|
|
23 |
private string $timeBasedClass;
|
|
|
24 |
private string $nameBasedClass;
|
|
|
25 |
private string $randomBasedClass;
|
|
|
26 |
private ?Uuid $timeBasedNode;
|
|
|
27 |
private ?Uuid $nameBasedNamespace;
|
|
|
28 |
|
|
|
29 |
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)
|
|
|
30 |
{
|
|
|
31 |
if (null !== $timeBasedNode && !$timeBasedNode instanceof Uuid) {
|
|
|
32 |
$timeBasedNode = Uuid::fromString($timeBasedNode);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
if (null !== $nameBasedNamespace) {
|
|
|
36 |
$nameBasedNamespace = $this->getNamespace($nameBasedNamespace);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
$this->defaultClass = is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass : $defaultClass;
|
|
|
40 |
$this->timeBasedClass = is_numeric($timeBasedClass) ? Uuid::class.'V'.$timeBasedClass : $timeBasedClass;
|
|
|
41 |
$this->nameBasedClass = is_numeric($nameBasedClass) ? Uuid::class.'V'.$nameBasedClass : $nameBasedClass;
|
|
|
42 |
$this->randomBasedClass = is_numeric($randomBasedClass) ? Uuid::class.'V'.$randomBasedClass : $randomBasedClass;
|
|
|
43 |
$this->timeBasedNode = $timeBasedNode;
|
|
|
44 |
$this->nameBasedNamespace = $nameBasedNamespace;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public function create(): Uuid
|
|
|
48 |
{
|
|
|
49 |
$class = $this->defaultClass;
|
|
|
50 |
|
|
|
51 |
return new $class();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function randomBased(): RandomBasedUuidFactory
|
|
|
55 |
{
|
|
|
56 |
return new RandomBasedUuidFactory($this->randomBasedClass);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function timeBased(Uuid|string $node = null): TimeBasedUuidFactory
|
|
|
60 |
{
|
|
|
61 |
$node ??= $this->timeBasedNode;
|
|
|
62 |
|
|
|
63 |
if (null !== $node && !$node instanceof Uuid) {
|
|
|
64 |
$node = Uuid::fromString($node);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
return new TimeBasedUuidFactory($this->timeBasedClass, $node);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function nameBased(Uuid|string $namespace = null): NameBasedUuidFactory
|
|
|
71 |
{
|
|
|
72 |
$namespace ??= $this->nameBasedNamespace;
|
|
|
73 |
|
|
|
74 |
if (null === $namespace) {
|
|
|
75 |
throw new \LogicException(sprintf('A namespace should be defined when using "%s()".', __METHOD__));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private function getNamespace(Uuid|string $namespace): Uuid
|
|
|
82 |
{
|
|
|
83 |
if ($namespace instanceof Uuid) {
|
|
|
84 |
return $namespace;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return match ($namespace) {
|
|
|
88 |
'dns' => new UuidV1(Uuid::NAMESPACE_DNS),
|
|
|
89 |
'url' => new UuidV1(Uuid::NAMESPACE_URL),
|
|
|
90 |
'oid' => new UuidV1(Uuid::NAMESPACE_OID),
|
|
|
91 |
'x500' => new UuidV1(Uuid::NAMESPACE_X500),
|
|
|
92 |
default => Uuid::fromString($namespace),
|
|
|
93 |
};
|
|
|
94 |
}
|
|
|
95 |
}
|