PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor / ramsey / uuid / src / UuidFactory.php

UuidFactory.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor/ramsey/uuid/src/UuidFactory.php

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