PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / Ramsey / Uuid / Codec / StringCodec.php

StringCodec.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at lib/packages/Ramsey/Uuid/Codec/StringCodec.php

122 lines 3.2 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 WP_DEFENDER_VENDOR\Ramsey\Uuid\Codec;
16
17 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Builder\UuidBuilderInterface;
18 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Exception\InvalidArgumentException;
19 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Exception\InvalidUuidStringException;
20 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Uuid;
21 use WP_DEFENDER_VENDOR\Ramsey\Uuid\UuidInterface;
22
23 use function bin2hex;
24 use function hex2bin;
25 use function implode;
26 use function sprintf;
27 use function str_replace;
28 use function strlen;
29 use function substr;
30
31 /**
32 * StringCodec encodes and decodes RFC 9562 (formerly RFC 4122) UUIDs
33 *
34 * @immutable
35 */
36 class StringCodec implements CodecInterface
37 {
38 /**
39 * Constructs a StringCodec
40 *
41 * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
42 */
43 public function __construct(private UuidBuilderInterface $builder)
44 {
45 }
46
47 public function encode(UuidInterface $uuid): string
48 {
49 /** @phpstan-ignore possiblyImpure.methodCall */
50 $hex = bin2hex($uuid->getFields()->getBytes());
51
52 /** @var non-empty-string */
53 return sprintf(
54 '%08s-%04s-%04s-%04s-%012s',
55 substr($hex, 0, 8),
56 substr($hex, 8, 4),
57 substr($hex, 12, 4),
58 substr($hex, 16, 4),
59 substr($hex, 20),
60 );
61 }
62
63 /**
64 * @return non-empty-string
65 */
66 public function encodeBinary(UuidInterface $uuid): string
67 {
68 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
69 return $uuid->getFields()->getBytes();
70 }
71
72 /**
73 * @throws InvalidUuidStringException
74 *
75 * @inheritDoc
76 */
77 public function decode(string $encodedUuid): UuidInterface
78 {
79 /** @phpstan-ignore possiblyImpure.methodCall */
80 return $this->builder->build($this, $this->getBytes($encodedUuid));
81 }
82
83 public function decodeBytes(string $bytes): UuidInterface
84 {
85 if (strlen($bytes) !== 16) {
86 throw new InvalidArgumentException('$bytes string should contain 16 characters.');
87 }
88
89 return $this->builder->build($this, $bytes);
90 }
91
92 /**
93 * Returns the UUID builder
94 */
95 protected function getBuilder(): UuidBuilderInterface
96 {
97 return $this->builder;
98 }
99
100 /**
101 * Returns a byte string of the UUID
102 */
103 protected function getBytes(string $encodedUuid): string
104 {
105 $parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid);
106
107 $components = [
108 substr($parsedUuid, 0, 8),
109 substr($parsedUuid, 8, 4),
110 substr($parsedUuid, 12, 4),
111 substr($parsedUuid, 16, 4),
112 substr($parsedUuid, 20),
113 ];
114
115 if (!Uuid::isValid(implode('-', $components))) {
116 throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);
117 }
118
119 return (string) hex2bin($parsedUuid);
120 }
121 }
122