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

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

91 lines 2.4 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\Exception\InvalidBytesException;
18 use Ramsey\Uuid\Uuid;
19
20 use function decbin;
21 use function str_pad;
22 use function strlen;
23 use function strpos;
24 use function substr;
25 use function unpack;
26
27 use const STR_PAD_LEFT;
28
29 /**
30 * Provides common functionality for handling the variant, as defined by RFC 4122
31 *
32 * @psalm-immutable
33 */
34 trait VariantTrait
35 {
36 /**
37 * Returns the bytes that comprise the fields
38 */
39 abstract public function getBytes(): string;
40
41 /**
42 * Returns the variant identifier, according to RFC 4122, for the given bytes
43 *
44 * The following values may be returned:
45 *
46 * - `0` -- Reserved, NCS backward compatibility.
47 * - `2` -- The variant specified in RFC 4122.
48 * - `6` -- Reserved, Microsoft Corporation backward compatibility.
49 * - `7` -- Reserved for future definition.
50 *
51 * @link https://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
52 *
53 * @return int The variant identifier, according to RFC 4122
54 */
55 public function getVariant(): int
56 {
57 if (strlen($this->getBytes()) !== 16) {
58 throw new InvalidBytesException('Invalid number of bytes');
59 }
60
61 /** @var array $parts */
62 $parts = unpack('n*', $this->getBytes());
63
64 // $parts[5] is a 16-bit, unsigned integer containing the variant bits
65 // of the UUID. We convert this integer into a string containing a
66 // binary representation, padded to 16 characters. We analyze the first
67 // three characters (three most-significant bits) to determine the
68 // variant.
69 $binary = str_pad(
70 decbin((int) $parts[5]),
71 16,
72 '0',
73 STR_PAD_LEFT
74 );
75
76 $msb = substr($binary, 0, 3);
77
78 if ($msb === '111') {
79 $variant = Uuid::RESERVED_FUTURE;
80 } elseif ($msb === '110') {
81 $variant = Uuid::RESERVED_MICROSOFT;
82 } elseif (strpos($msb, '10') === 0) {
83 $variant = Uuid::RFC_4122;
84 } else {
85 $variant = Uuid::RESERVED_NCS;
86 }
87
88 return $variant;
89 }
90 }
91