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