Debian Codegraph @

vendor/ramsey/uuid/src/Rfc4122/UuidBuilder.php

raw
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Rfc4122;
16
17 use Ramsey\Uuid\Builder\UuidBuilderInterface;
18 use Ramsey\Uuid\Codec\CodecInterface;
19 use Ramsey\Uuid\Converter\NumberConverterInterface;
20 use Ramsey\Uuid\Converter\Time\UnixTimeConverter;
21 use Ramsey\Uuid\Converter\TimeConverterInterface;
22 use Ramsey\Uuid\Exception\UnableToBuildUuidException;
23 use Ramsey\Uuid\Exception\UnsupportedOperationException;
24 use Ramsey\Uuid\Math\BrickMathCalculator;
25 use Ramsey\Uuid\Rfc4122\UuidInterface as Rfc4122UuidInterface;
26 use Ramsey\Uuid\Uuid;
27 use Ramsey\Uuid\UuidInterface;
28 use Throwable;
29
30 /**
31 * UuidBuilder builds instances of RFC 9562 (formerly 4122) UUIDs
32 *
33 * @immutable
34 */
35 class UuidBuilder implements UuidBuilderInterface
36 {
37 private TimeConverterInterface $unixTimeConverter;
38
39 /**
40 * Constructs the DefaultUuidBuilder
41 *
42 * @param NumberConverterInterface $numberConverter The number converter to use when constructing the Uuid
43 * @param TimeConverterInterface $timeConverter The time converter to use for converting Gregorian time extracted
44 * from version 1, 2, and 6 UUIDs to Unix timestamps
45 * @param TimeConverterInterface | null $unixTimeConverter The time converter to use for converter Unix Epoch time
46 * extracted from version 7 UUIDs to Unix timestamps
47 */
48 public function __construct(
49 private NumberConverterInterface $numberConverter,
50 private TimeConverterInterface $timeConverter,
51 ?TimeConverterInterface $unixTimeConverter = null,
52 ) {
53 $this->unixTimeConverter = $unixTimeConverter ?? new UnixTimeConverter(new BrickMathCalculator());
54 }
55
56 /**
57 * Builds and returns a Uuid
58 *
59 * @param CodecInterface $codec The codec to use for building this Uuid instance
60 * @param string $bytes The byte string from which to construct a UUID
61 *
62 * @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface
63 *
64 * @pure
65 */
66 public function build(CodecInterface $codec, string $bytes): UuidInterface
67 {
68 try {
69 /** @var Fields $fields */
70 $fields = $this->buildFields($bytes);
71
72 if ($fields->isNil()) {
73 /** @phpstan-ignore possiblyImpure.new */
74 return new NilUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
75 }
76
77 if ($fields->isMax()) {
78 /** @phpstan-ignore possiblyImpure.new */
79 return new MaxUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
80 }
81
82 return match ($fields->getVersion()) {
83 /** @phpstan-ignore possiblyImpure.new */
84 Uuid::UUID_TYPE_TIME => new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter),
85 Uuid::UUID_TYPE_DCE_SECURITY
86 /** @phpstan-ignore possiblyImpure.new */
87 => new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter),
88 /** @phpstan-ignore possiblyImpure.new */
89 Uuid::UUID_TYPE_HASH_MD5 => new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter),
90 /** @phpstan-ignore possiblyImpure.new */
91 Uuid::UUID_TYPE_RANDOM => new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter),
92 /** @phpstan-ignore possiblyImpure.new */
93 Uuid::UUID_TYPE_HASH_SHA1 => new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter),
94 Uuid::UUID_TYPE_REORDERED_TIME
95 /** @phpstan-ignore possiblyImpure.new */
96 => new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter),
97 Uuid::UUID_TYPE_UNIX_TIME
98 /** @phpstan-ignore possiblyImpure.new */
99 => new UuidV7($fields, $this->numberConverter, $codec, $this->unixTimeConverter),
100 /** @phpstan-ignore possiblyImpure.new */
101 Uuid::UUID_TYPE_CUSTOM => new UuidV8($fields, $this->numberConverter, $codec, $this->timeConverter),
102 default => throw new UnsupportedOperationException(
103 'The UUID version in the given fields is not supported by this UUID builder',
104 ),
105 };
106 } catch (Throwable $e) {
107 /** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */
108 throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e);
109 }
110 }
111
112 /**
113 * Proxy method to allow injecting a mock for testing
114 *
115 * @pure
116 */
117 protected function buildFields(string $bytes): FieldsInterface
118 {
119 /** @phpstan-ignore possiblyImpure.new */
120 return new Fields($bytes);
121 }
122 }