PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.1.4
ShareThis Dashboard for Google Analytics v3.1.4
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / ramsey / uuid / src / Codec / StringCodec.php
googleanalytics / lib / analytics-admin / vendor / ramsey / uuid / src / Codec Last commit date
CodecInterface.php 3 years ago GuidStringCodec.php 3 years ago OrderedTimeCodec.php 3 years ago StringCodec.php 3 years ago TimestampFirstCombCodec.php 3 years ago TimestampLastCombCodec.php 3 years ago
StringCodec.php
139 lines
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 <ben@benramsey.com>
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