Debian Codegraph @

lib/3rdParty/composer/symfony/uid/Factory/UuidFactory.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\Exception\LogicException;
15 use Symfony\Component\Uid\Uuid;
16 use Symfony\Component\Uid\UuidV1;
17 use Symfony\Component\Uid\UuidV4;
18 use Symfony\Component\Uid\UuidV5;
19 use Symfony\Component\Uid\UuidV7;
20
21 class UuidFactory
22 {
23 private string $defaultClass;
24 private string $timeBasedClass;
25 private string $nameBasedClass;
26 private string $randomBasedClass;
27 private ?Uuid $timeBasedNode;
28 private ?Uuid $nameBasedNamespace;
29
30 public function __construct(string|int $defaultClass = UuidV7::class, string|int $timeBasedClass = UuidV7::class, string|int $nameBasedClass = UuidV5::class, string|int $randomBasedClass = UuidV4::class, Uuid|string|null $timeBasedNode = null, Uuid|string|null $nameBasedNamespace = null)
31 {
32 if (null !== $timeBasedNode && !$timeBasedNode instanceof Uuid) {
33 $timeBasedNode = Uuid::fromString($timeBasedNode);
34 }
35
36 if (null !== $nameBasedNamespace) {
37 $nameBasedNamespace = $this->getNamespace($nameBasedNamespace);
38 }
39
40 $this->defaultClass = is_numeric($defaultClass) ? Uuid::class.'V'.$defaultClass : $defaultClass;
41 $this->timeBasedClass = is_numeric($timeBasedClass) ? Uuid::class.'V'.$timeBasedClass : $timeBasedClass;
42 $this->nameBasedClass = is_numeric($nameBasedClass) ? Uuid::class.'V'.$nameBasedClass : $nameBasedClass;
43 $this->randomBasedClass = is_numeric($randomBasedClass) ? Uuid::class.'V'.$randomBasedClass : $randomBasedClass;
44 $this->timeBasedNode = $timeBasedNode;
45 $this->nameBasedNamespace = $nameBasedNamespace;
46 }
47
48 public function create(): Uuid
49 {
50 $class = $this->defaultClass;
51
52 return new $class();
53 }
54
55 public function randomBased(): RandomBasedUuidFactory
56 {
57 return new RandomBasedUuidFactory($this->randomBasedClass);
58 }
59
60 public function timeBased(Uuid|string|null $node = null): TimeBasedUuidFactory
61 {
62 $node ??= $this->timeBasedNode;
63
64 if (null !== $node && !$node instanceof Uuid) {
65 $node = Uuid::fromString($node);
66 }
67
68 return new TimeBasedUuidFactory($this->timeBasedClass, $node);
69 }
70
71 /**
72 * @throws LogicException When no namespace is defined
73 */
74 public function nameBased(Uuid|string|null $namespace = null): NameBasedUuidFactory
75 {
76 $namespace ??= $this->nameBasedNamespace;
77
78 if (null === $namespace) {
79 throw new LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
80 }
81
82 return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
83 }
84
85 private function getNamespace(Uuid|string $namespace): Uuid
86 {
87 if ($namespace instanceof Uuid) {
88 return $namespace;
89 }
90
91 return match ($namespace) {
92 'dns' => new UuidV1(Uuid::NAMESPACE_DNS),
93 'url' => new UuidV1(Uuid::NAMESPACE_URL),
94 'oid' => new UuidV1(Uuid::NAMESPACE_OID),
95 'x500' => new UuidV1(Uuid::NAMESPACE_X500),
96 default => Uuid::fromString($namespace),
97 };
98 }
99 }