PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
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 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / UuidFactory.php

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

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