PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Codec / StringCodec.php

StringCodec.php in Media Cloud Sync 1.4.1, at includes/sdk/google/ramsey/uuid/src/Codec/StringCodec.php

94 lines 3.1 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 <ben@benramsey.com>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec;
14
15 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Builder\UuidBuilderInterface;
16 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidArgumentException;
17 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidUuidStringException;
18 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid;
19 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidInterface;
20 use function bin2hex;
21 use function hex2bin;
22 use function implode;
23 use function sprintf;
24 use function str_replace;
25 use function strlen;
26 use function substr;
27 /**
28 * StringCodec encodes and decodes RFC 9562 (formerly RFC 4122) UUIDs
29 *
30 * @immutable
31 */
32 class StringCodec implements CodecInterface
33 {
34 /**
35 * Constructs a StringCodec
36 *
37 * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
38 */
39 public function __construct(private UuidBuilderInterface $builder)
40 {
41 }
42 public function encode(UuidInterface $uuid) : string
43 {
44 /** @phpstan-ignore possiblyImpure.methodCall */
45 $hex = bin2hex($uuid->getFields()->getBytes());
46 /** @var non-empty-string */
47 return sprintf('%08s-%04s-%04s-%04s-%012s', substr($hex, 0, 8), substr($hex, 8, 4), substr($hex, 12, 4), substr($hex, 16, 4), substr($hex, 20));
48 }
49 /**
50 * @return non-empty-string
51 */
52 public function encodeBinary(UuidInterface $uuid) : string
53 {
54 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
55 return $uuid->getFields()->getBytes();
56 }
57 /**
58 * @throws InvalidUuidStringException
59 *
60 * @inheritDoc
61 */
62 public function decode(string $encodedUuid) : UuidInterface
63 {
64 /** @phpstan-ignore possiblyImpure.methodCall */
65 return $this->builder->build($this, $this->getBytes($encodedUuid));
66 }
67 public function decodeBytes(string $bytes) : UuidInterface
68 {
69 if (strlen($bytes) !== 16) {
70 throw new InvalidArgumentException('$bytes string should contain 16 characters.');
71 }
72 return $this->builder->build($this, $bytes);
73 }
74 /**
75 * Returns the UUID builder
76 */
77 protected function getBuilder() : UuidBuilderInterface
78 {
79 return $this->builder;
80 }
81 /**
82 * Returns a byte string of the UUID
83 */
84 protected function getBytes(string $encodedUuid) : string
85 {
86 $parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid);
87 $components = [substr($parsedUuid, 0, 8), substr($parsedUuid, 8, 4), substr($parsedUuid, 12, 4), substr($parsedUuid, 16, 4), substr($parsedUuid, 20)];
88 if (!Uuid::isValid(implode('-', $components))) {
89 throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);
90 }
91 return (string) hex2bin($parsedUuid);
92 }
93 }
94