PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Rfc4122 / UuidBuilder.php

UuidBuilder.php in Media Cloud Sync 1.4.2, at includes/sdk/google/ramsey/uuid/src/Rfc4122/UuidBuilder.php

103 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122;
14
15 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Builder\UuidBuilderInterface;
16 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec\CodecInterface;
17 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface;
18 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\Time\UnixTimeConverter;
19 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\TimeConverterInterface;
20 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\UnableToBuildUuidException;
21 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\UnsupportedOperationException;
22 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Math\BrickMathCalculator;
23 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\UuidInterface as Rfc4122UuidInterface;
24 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid;
25 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidInterface;
26 use Throwable;
27 /**
28 * UuidBuilder builds instances of RFC 9562 (formerly 4122) UUIDs
29 *
30 * @immutable
31 */
32 class UuidBuilder implements UuidBuilderInterface
33 {
34 private TimeConverterInterface $unixTimeConverter;
35 /**
36 * Constructs the DefaultUuidBuilder
37 *
38 * @param NumberConverterInterface $numberConverter The number converter to use when constructing the Uuid
39 * @param TimeConverterInterface $timeConverter The time converter to use for converting Gregorian time extracted
40 * from version 1, 2, and 6 UUIDs to Unix timestamps
41 * @param TimeConverterInterface | null $unixTimeConverter The time converter to use for converter Unix Epoch time
42 * extracted from version 7 UUIDs to Unix timestamps
43 */
44 public function __construct(private NumberConverterInterface $numberConverter, private TimeConverterInterface $timeConverter, ?TimeConverterInterface $unixTimeConverter = null)
45 {
46 $this->unixTimeConverter = $unixTimeConverter ?? new UnixTimeConverter(new BrickMathCalculator());
47 }
48 /**
49 * Builds and returns a Uuid
50 *
51 * @param CodecInterface $codec The codec to use for building this Uuid instance
52 * @param string $bytes The byte string from which to construct a UUID
53 *
54 * @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface
55 *
56 * @pure
57 */
58 public function build(CodecInterface $codec, string $bytes) : UuidInterface
59 {
60 try {
61 /** @var Fields $fields */
62 $fields = $this->buildFields($bytes);
63 if ($fields->isNil()) {
64 /** @phpstan-ignore possiblyImpure.new */
65 return new NilUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
66 }
67 if ($fields->isMax()) {
68 /** @phpstan-ignore possiblyImpure.new */
69 return new MaxUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
70 }
71 return match ($fields->getVersion()) {
72 /** @phpstan-ignore possiblyImpure.new */
73 Uuid::UUID_TYPE_TIME => new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter),
74 Uuid::UUID_TYPE_DCE_SECURITY => new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter),
75 /** @phpstan-ignore possiblyImpure.new */
76 Uuid::UUID_TYPE_HASH_MD5 => new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter),
77 /** @phpstan-ignore possiblyImpure.new */
78 Uuid::UUID_TYPE_RANDOM => new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter),
79 /** @phpstan-ignore possiblyImpure.new */
80 Uuid::UUID_TYPE_HASH_SHA1 => new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter),
81 Uuid::UUID_TYPE_REORDERED_TIME => new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter),
82 Uuid::UUID_TYPE_UNIX_TIME => new UuidV7($fields, $this->numberConverter, $codec, $this->unixTimeConverter),
83 /** @phpstan-ignore possiblyImpure.new */
84 Uuid::UUID_TYPE_CUSTOM => new UuidV8($fields, $this->numberConverter, $codec, $this->timeConverter),
85 default => throw new UnsupportedOperationException('The UUID version in the given fields is not supported by this UUID builder'),
86 };
87 } catch (Throwable $e) {
88 /** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */
89 throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e);
90 }
91 }
92 /**
93 * Proxy method to allow injecting a mock for testing
94 *
95 * @pure
96 */
97 protected function buildFields(string $bytes) : FieldsInterface
98 {
99 /** @phpstan-ignore possiblyImpure.new */
100 return new Fields($bytes);
101 }
102 }
103