PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.21
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.21
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 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 All 169 releases
woocommerce-pos / vendor / ramsey / uuid / src / Rfc4122 / UuidBuilder.php

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

112 lines 3.6 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\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\TimeConverterInterface;
21 use Ramsey\Uuid\Exception\UnableToBuildUuidException;
22 use Ramsey\Uuid\Exception\UnsupportedOperationException;
23 use Ramsey\Uuid\Nonstandard\UuidV6;
24 use Ramsey\Uuid\Rfc4122\UuidInterface as Rfc4122UuidInterface;
25 use Ramsey\Uuid\UuidInterface;
26 use Throwable;
27
28 /**
29 * UuidBuilder builds instances of RFC 4122 UUIDs
30 *
31 * @psalm-immutable
32 */
33 class UuidBuilder implements UuidBuilderInterface
34 {
35 /**
36 * @var NumberConverterInterface
37 */
38 private $numberConverter;
39
40 /**
41 * @var TimeConverterInterface
42 */
43 private $timeConverter;
44
45 /**
46 * Constructs the DefaultUuidBuilder
47 *
48 * @param NumberConverterInterface $numberConverter The number converter to
49 * use when constructing the Uuid
50 * @param TimeConverterInterface $timeConverter The time converter to use
51 * for converting timestamps extracted from a UUID to Unix timestamps
52 */
53 public function __construct(
54 NumberConverterInterface $numberConverter,
55 TimeConverterInterface $timeConverter
56 ) {
57 $this->numberConverter = $numberConverter;
58 $this->timeConverter = $timeConverter;
59 }
60
61 /**
62 * Builds and returns a Uuid
63 *
64 * @param CodecInterface $codec The codec to use for building this Uuid instance
65 * @param string $bytes The byte string from which to construct a UUID
66 *
67 * @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface
68 *
69 * @psalm-pure
70 */
71 public function build(CodecInterface $codec, string $bytes): UuidInterface
72 {
73 try {
74 $fields = $this->buildFields($bytes);
75
76 if ($fields->isNil()) {
77 return new NilUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
78 }
79
80 switch ($fields->getVersion()) {
81 case 1:
82 return new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter);
83 case 2:
84 return new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter);
85 case 3:
86 return new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter);
87 case 4:
88 return new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter);
89 case 5:
90 return new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter);
91 case 6:
92 return new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter);
93 }
94
95 throw new UnsupportedOperationException(
96 'The UUID version in the given fields is not supported '
97 . 'by this UUID builder'
98 );
99 } catch (Throwable $e) {
100 throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e);
101 }
102 }
103
104 /**
105 * Proxy method to allow injecting a mock, for testing
106 */
107 protected function buildFields(string $bytes): FieldsInterface
108 {
109 return new Fields($bytes);
110 }
111 }
112