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 / Codec / StringCodec.php

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

139 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\Codec;
16
17 use Ramsey\Uuid\Builder\UuidBuilderInterface;
18 use Ramsey\Uuid\Exception\InvalidArgumentException;
19 use Ramsey\Uuid\Exception\InvalidUuidStringException;
20 use Ramsey\Uuid\Rfc4122\FieldsInterface;
21 use Ramsey\Uuid\Uuid;
22 use Ramsey\Uuid\UuidInterface;
23
24 use function hex2bin;
25 use function implode;
26 use function str_replace;
27 use function strlen;
28 use function substr;
29
30 /**
31 * StringCodec encodes and decodes RFC 4122 UUIDs
32 *
33 * @link http://tools.ietf.org/html/rfc4122
34 *
35 * @psalm-immutable
36 */
37 class StringCodec implements CodecInterface
38 {
39 /**
40 * @var UuidBuilderInterface
41 */
42 private $builder;
43
44 /**
45 * Constructs a StringCodec
46 *
47 * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
48 */
49 public function __construct(UuidBuilderInterface $builder)
50 {
51 $this->builder = $builder;
52 }
53
54 public function encode(UuidInterface $uuid): string
55 {
56 /** @var FieldsInterface $fields */
57 $fields = $uuid->getFields();
58
59 return $fields->getTimeLow()->toString()
60 . '-'
61 . $fields->getTimeMid()->toString()
62 . '-'
63 . $fields->getTimeHiAndVersion()->toString()
64 . '-'
65 . $fields->getClockSeqHiAndReserved()->toString()
66 . $fields->getClockSeqLow()->toString()
67 . '-'
68 . $fields->getNode()->toString();
69 }
70
71 /**
72 * @psalm-return non-empty-string
73 * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
74 * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
75 */
76 public function encodeBinary(UuidInterface $uuid): string
77 {
78 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
79 return $uuid->getFields()->getBytes();
80 }
81
82 /**
83 * @throws InvalidUuidStringException
84 *
85 * @inheritDoc
86 */
87 public function decode(string $encodedUuid): UuidInterface
88 {
89 return $this->builder->build($this, $this->getBytes($encodedUuid));
90 }
91
92 public function decodeBytes(string $bytes): UuidInterface
93 {
94 if (strlen($bytes) !== 16) {
95 throw new InvalidArgumentException(
96 '$bytes string should contain 16 characters.'
97 );
98 }
99
100 return $this->builder->build($this, $bytes);
101 }
102
103 /**
104 * Returns the UUID builder
105 */
106 protected function getBuilder(): UuidBuilderInterface
107 {
108 return $this->builder;
109 }
110
111 /**
112 * Returns a byte string of the UUID
113 */
114 protected function getBytes(string $encodedUuid): string
115 {
116 $parsedUuid = str_replace(
117 ['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'],
118 '',
119 $encodedUuid
120 );
121
122 $components = [
123 substr($parsedUuid, 0, 8),
124 substr($parsedUuid, 8, 4),
125 substr($parsedUuid, 12, 4),
126 substr($parsedUuid, 16, 4),
127 substr($parsedUuid, 20),
128 ];
129
130 if (!Uuid::isValid(implode('-', $components))) {
131 throw new InvalidUuidStringException(
132 'Invalid UUID string: ' . $encodedUuid
133 );
134 }
135
136 return (string) hex2bin($parsedUuid);
137 }
138 }
139