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 / UuidFactory.php

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

399 lines 15.2 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;
14
15 use DateTimeInterface;
16 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Builder\UuidBuilderInterface;
17 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec\CodecInterface;
18 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface;
19 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\TimeConverterInterface;
20 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\DceSecurityGeneratorInterface;
21 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\DefaultTimeGenerator;
22 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\NameGeneratorInterface;
23 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\RandomGeneratorInterface;
24 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\TimeGeneratorInterface;
25 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\UnixTimeGenerator;
26 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Lazy\LazyUuidFromString;
27 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\NodeProviderInterface;
28 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\Time\FixedTimeProvider;
29 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal;
30 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject;
31 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Time;
32 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Validator\ValidatorInterface;
33 use function bin2hex;
34 use function hex2bin;
35 use function pack;
36 use function str_pad;
37 use function strtolower;
38 use function substr;
39 use function substr_replace;
40 use function unpack;
41 use const STR_PAD_LEFT;
42 class UuidFactory implements UuidFactoryInterface
43 {
44 private CodecInterface $codec;
45 private DceSecurityGeneratorInterface $dceSecurityGenerator;
46 private NameGeneratorInterface $nameGenerator;
47 private NodeProviderInterface $nodeProvider;
48 private NumberConverterInterface $numberConverter;
49 private RandomGeneratorInterface $randomGenerator;
50 private TimeConverterInterface $timeConverter;
51 private TimeGeneratorInterface $timeGenerator;
52 private TimeGeneratorInterface $unixTimeGenerator;
53 private UuidBuilderInterface $uuidBuilder;
54 private ValidatorInterface $validator;
55 /**
56 * @var bool whether the feature set was provided from outside, or we can operate under "default" assumptions
57 */
58 private bool $isDefaultFeatureSet;
59 /**
60 * @param FeatureSet | null $features A set of available features in the current environment
61 */
62 public function __construct(?FeatureSet $features = null)
63 {
64 $this->isDefaultFeatureSet = $features === null;
65 $features = $features ?: new FeatureSet();
66 $this->codec = $features->getCodec();
67 $this->dceSecurityGenerator = $features->getDceSecurityGenerator();
68 $this->nameGenerator = $features->getNameGenerator();
69 $this->nodeProvider = $features->getNodeProvider();
70 $this->numberConverter = $features->getNumberConverter();
71 $this->randomGenerator = $features->getRandomGenerator();
72 $this->timeConverter = $features->getTimeConverter();
73 $this->timeGenerator = $features->getTimeGenerator();
74 $this->uuidBuilder = $features->getBuilder();
75 $this->validator = $features->getValidator();
76 $this->unixTimeGenerator = $features->getUnixTimeGenerator();
77 }
78 /**
79 * Returns the codec used by this factory
80 */
81 public function getCodec() : CodecInterface
82 {
83 return $this->codec;
84 }
85 /**
86 * Sets the codec to use for this factory
87 *
88 * @param CodecInterface $codec A UUID encoder-decoder
89 */
90 public function setCodec(CodecInterface $codec) : void
91 {
92 $this->isDefaultFeatureSet = \false;
93 $this->codec = $codec;
94 }
95 /**
96 * Returns the name generator used by this factory
97 */
98 public function getNameGenerator() : NameGeneratorInterface
99 {
100 return $this->nameGenerator;
101 }
102 /**
103 * Sets the name generator to use for this factory
104 *
105 * @param NameGeneratorInterface $nameGenerator A generator to generate binary data, based on a namespace and name
106 */
107 public function setNameGenerator(NameGeneratorInterface $nameGenerator) : void
108 {
109 $this->isDefaultFeatureSet = \false;
110 $this->nameGenerator = $nameGenerator;
111 }
112 /**
113 * Returns the node provider used by this factory
114 */
115 public function getNodeProvider() : NodeProviderInterface
116 {
117 return $this->nodeProvider;
118 }
119 /**
120 * Returns the random generator used by this factory
121 */
122 public function getRandomGenerator() : RandomGeneratorInterface
123 {
124 return $this->randomGenerator;
125 }
126 /**
127 * Returns the time generator used by this factory
128 */
129 public function getTimeGenerator() : TimeGeneratorInterface
130 {
131 return $this->timeGenerator;
132 }
133 /**
134 * Sets the time generator to use for this factory
135 *
136 * @param TimeGeneratorInterface $generator A generator to generate binary data, based on the time
137 */
138 public function setTimeGenerator(TimeGeneratorInterface $generator) : void
139 {
140 $this->isDefaultFeatureSet = \false;
141 $this->timeGenerator = $generator;
142 }
143 /**
144 * Returns the DCE Security generator used by this factory
145 */
146 public function getDceSecurityGenerator() : DceSecurityGeneratorInterface
147 {
148 return $this->dceSecurityGenerator;
149 }
150 /**
151 * Sets the DCE Security generator to use for this factory
152 *
153 * @param DceSecurityGeneratorInterface $generator A generator to generate binary data, based on a local domain and
154 * local identifier
155 */
156 public function setDceSecurityGenerator(DceSecurityGeneratorInterface $generator) : void
157 {
158 $this->isDefaultFeatureSet = \false;
159 $this->dceSecurityGenerator = $generator;
160 }
161 /**
162 * Returns the number converter used by this factory
163 */
164 public function getNumberConverter() : NumberConverterInterface
165 {
166 return $this->numberConverter;
167 }
168 /**
169 * Sets the random generator to use for this factory
170 *
171 * @param RandomGeneratorInterface $generator A generator to generate binary data, based on some random input
172 */
173 public function setRandomGenerator(RandomGeneratorInterface $generator) : void
174 {
175 $this->isDefaultFeatureSet = \false;
176 $this->randomGenerator = $generator;
177 }
178 /**
179 * Sets the number converter to use for this factory
180 *
181 * @param NumberConverterInterface $converter A converter to use for working with large integers (i.e., integers
182 * greater than PHP_INT_MAX)
183 */
184 public function setNumberConverter(NumberConverterInterface $converter) : void
185 {
186 $this->isDefaultFeatureSet = \false;
187 $this->numberConverter = $converter;
188 }
189 /**
190 * Returns the UUID builder used by this factory
191 */
192 public function getUuidBuilder() : UuidBuilderInterface
193 {
194 return $this->uuidBuilder;
195 }
196 /**
197 * Sets the UUID builder to use for this factory
198 *
199 * @param UuidBuilderInterface $builder A builder for constructing instances of UuidInterface
200 */
201 public function setUuidBuilder(UuidBuilderInterface $builder) : void
202 {
203 $this->isDefaultFeatureSet = \false;
204 $this->uuidBuilder = $builder;
205 }
206 public function getValidator() : ValidatorInterface
207 {
208 return $this->validator;
209 }
210 /**
211 * Sets the validator to use for this factory
212 *
213 * @param ValidatorInterface $validator A validator to use for validating whether a string is a valid UUID
214 */
215 public function setValidator(ValidatorInterface $validator) : void
216 {
217 $this->isDefaultFeatureSet = \false;
218 $this->validator = $validator;
219 }
220 /**
221 * @pure
222 */
223 public function fromBytes(string $bytes) : UuidInterface
224 {
225 return $this->codec->decodeBytes($bytes);
226 }
227 /**
228 * @pure
229 */
230 public function fromString(string $uuid) : UuidInterface
231 {
232 $uuid = strtolower($uuid);
233 return $this->codec->decode($uuid);
234 }
235 /**
236 * @pure
237 */
238 public function fromInteger(string $integer) : UuidInterface
239 {
240 $hex = $this->numberConverter->toHex($integer);
241 $hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
242 return $this->fromString($hex);
243 }
244 public function fromDateTime(DateTimeInterface $dateTime, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface
245 {
246 $timeProvider = new FixedTimeProvider(new Time($dateTime->format('U'), $dateTime->format('u')));
247 $timeGenerator = new DefaultTimeGenerator($this->nodeProvider, $this->timeConverter, $timeProvider);
248 $bytes = $timeGenerator->generate($node?->toString(), $clockSeq);
249 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_TIME);
250 }
251 /**
252 * @pure
253 */
254 public function fromHexadecimal(Hexadecimal $hex) : UuidInterface
255 {
256 return $this->codec->decode($hex->__toString());
257 }
258 /**
259 * @inheritDoc
260 */
261 public function uuid1($node = null, ?int $clockSeq = null) : UuidInterface
262 {
263 $bytes = $this->timeGenerator->generate($node, $clockSeq);
264 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_TIME);
265 }
266 public function uuid2(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface
267 {
268 $bytes = $this->dceSecurityGenerator->generate($localDomain, $localIdentifier, $node, $clockSeq);
269 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_DCE_SECURITY);
270 }
271 /**
272 * @inheritDoc
273 * @pure
274 */
275 public function uuid3($ns, string $name) : UuidInterface
276 {
277 return $this->uuidFromNsAndName($ns, $name, Uuid::UUID_TYPE_HASH_MD5, 'md5');
278 }
279 public function uuid4() : UuidInterface
280 {
281 $bytes = $this->randomGenerator->generate(16);
282 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_RANDOM);
283 }
284 /**
285 * @inheritDoc
286 * @pure
287 */
288 public function uuid5($ns, string $name) : UuidInterface
289 {
290 return $this->uuidFromNsAndName($ns, $name, Uuid::UUID_TYPE_HASH_SHA1, 'sha1');
291 }
292 public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface
293 {
294 $bytes = $this->timeGenerator->generate($node?->toString(), $clockSeq);
295 // Rearrange the bytes, according to the UUID version 6 specification.
296 $v6 = $bytes[6] . $bytes[7] . $bytes[4] . $bytes[5] . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3];
297 $v6 = bin2hex($v6);
298 // Drop the first four bits, while adding an empty four bits for the version field. This allows us to
299 // reconstruct the correct time from the bytes of this UUID.
300 $v6Bytes = hex2bin(substr($v6, 1, 12) . '0' . substr($v6, -3));
301 $v6Bytes .= substr($bytes, 8);
302 return $this->uuidFromBytesAndVersion($v6Bytes, Uuid::UUID_TYPE_REORDERED_TIME);
303 }
304 /**
305 * Returns a version 7 (Unix Epoch time) UUID
306 *
307 * @param DateTimeInterface | null $dateTime An optional date/time from which to create the version 7 UUID. If not
308 * provided, the UUID is generated using the current date/time.
309 *
310 * @return UuidInterface A UuidInterface instance that represents a version 7 UUID
311 */
312 public function uuid7(?DateTimeInterface $dateTime = null) : UuidInterface
313 {
314 \assert($this->unixTimeGenerator instanceof UnixTimeGenerator);
315 $bytes = $this->unixTimeGenerator->generate(null, null, $dateTime);
316 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_UNIX_TIME);
317 }
318 /**
319 * Returns a version 8 (custom format) UUID
320 *
321 * The bytes provided may contain any value according to your application's needs. Be aware, however, that other
322 * applications may not understand the semantics of the value.
323 *
324 * @param string $bytes A 16-byte octet string. This is an open blob of data that you may fill with 128 bits of
325 * information. Be aware, however, bits 48 through 51 will be replaced with the UUID version field, and bits 64
326 * and 65 will be replaced with the UUID variant. You MUST NOT rely on these bits for your application needs.
327 *
328 * @return UuidInterface A UuidInterface instance that represents a version 8 UUID
329 *
330 * @pure
331 */
332 public function uuid8(string $bytes) : UuidInterface
333 {
334 /** @phpstan-ignore possiblyImpure.methodCall */
335 return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_CUSTOM);
336 }
337 /**
338 * Returns a Uuid created from the provided byte string
339 *
340 * Uses the configured builder and codec and the provided byte string to construct a Uuid object.
341 *
342 * @param string $bytes The byte string from which to construct a UUID
343 *
344 * @return UuidInterface An instance of UuidInterface, created from the provided bytes
345 *
346 * @pure
347 */
348 public function uuid(string $bytes) : UuidInterface
349 {
350 return $this->uuidBuilder->build($this->codec, $bytes);
351 }
352 /**
353 * Returns a version 3 or 5 namespaced Uuid
354 *
355 * @param UuidInterface | string $ns The namespace (must be a valid UUID)
356 * @param string $name The name to hash together with the namespace
357 * @param int $version The version of UUID to create (3 or 5)
358 * @param string $hashAlgorithm The hashing algorithm to use when hashing together the namespace and name
359 *
360 * @return UuidInterface An instance of UuidInterface, created by hashing together the provided namespace and name
361 *
362 * @pure
363 */
364 private function uuidFromNsAndName(UuidInterface|string $ns, string $name, int $version, string $hashAlgorithm) : UuidInterface
365 {
366 if (!$ns instanceof UuidInterface) {
367 $ns = $this->fromString($ns);
368 }
369 $bytes = $this->nameGenerator->generate($ns, $name, $hashAlgorithm);
370 /** @phpstan-ignore possiblyImpure.methodCall */
371 return $this->uuidFromBytesAndVersion(substr($bytes, 0, 16), $version);
372 }
373 /**
374 * Returns a Uuid created from the provided bytes and version
375 *
376 * @param string $bytes The byte string to convert to a UUID
377 * @param int $version The version to apply to the UUID
378 *
379 * @return UuidInterface An instance of UuidInterface, created from the byte string and version
380 */
381 private function uuidFromBytesAndVersion(string $bytes, int $version) : UuidInterface
382 {
383 /** @var int[] $unpackedTime */
384 $unpackedTime = unpack('n*', substr($bytes, 6, 2));
385 $timeHi = $unpackedTime[1];
386 $timeHiAndVersion = pack('n*', BinaryUtils::applyVersion($timeHi, $version));
387 /** @var int[] $unpackedClockSeq */
388 $unpackedClockSeq = unpack('n*', substr($bytes, 8, 2));
389 $clockSeqHi = $unpackedClockSeq[1];
390 $clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi));
391 $bytes = substr_replace($bytes, $timeHiAndVersion, 6, 2);
392 $bytes = substr_replace($bytes, $clockSeqHiAndReserved, 8, 2);
393 if ($this->isDefaultFeatureSet) {
394 return LazyUuidFromString::fromBytes($bytes);
395 }
396 return $this->uuid($bytes);
397 }
398 }
399