← All changes
|
includes/sdk/google/ramsey/uuid/src/Codec/GuidStringCodec.php
+18
-8
1.2.2
→
1.4.1
View file →
| @@ -9,13 +9,14 @@ | ||
| 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\Guid\Guid; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Guid\Guid; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidInterface; | |
| 17 | 17 | use function bin2hex; |
| 18 | +use function sprintf; | |
| 18 | 19 | use function substr; |
| 19 | 20 | /** |
| 20 | 21 | * GuidStringCodec encodes and decodes globally unique identifiers (GUID) |
| 21 | 22 | * |
| @@ -20,26 +21,35 @@ | ||
| 20 | 21 | * GuidStringCodec encodes and decodes globally unique identifiers (GUID) |
| 21 | 22 | * |
| 22 | 23 | * @see Guid |
| 23 | 24 | * |
| 24 | - * @psalm-immutable | |
| 25 | + * @immutable | |
| 25 | 26 | */ |
| 26 | 27 | class GuidStringCodec extends StringCodec |
| 27 | 28 | { |
| 28 | - public function decode(string $encodedUuid): UuidInterface | |
| 29 | + public function encode(UuidInterface $uuid) : string | |
| 29 | 30 | { |
| 31 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 32 | + $hex = bin2hex($uuid->getFields()->getBytes()); | |
| 33 | + /** @var non-empty-string */ | |
| 34 | + return sprintf('%02s%02s%02s%02s-%02s%02s-%02s%02s-%04s-%012s', substr($hex, 6, 2), substr($hex, 4, 2), substr($hex, 2, 2), substr($hex, 0, 2), substr($hex, 10, 2), substr($hex, 8, 2), substr($hex, 14, 2), substr($hex, 12, 2), substr($hex, 16, 4), substr($hex, 20)); | |
| 35 | + } | |
| 36 | + public function decode(string $encodedUuid) : UuidInterface | |
| 37 | + { | |
| 38 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 30 | 39 | $bytes = $this->getBytes($encodedUuid); |
| 40 | + /** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */ | |
| 31 | 41 | return $this->getBuilder()->build($this, $this->swapBytes($bytes)); |
| 32 | 42 | } |
| 33 | - public function decodeBytes(string $bytes): UuidInterface | |
| 43 | + public function decodeBytes(string $bytes) : UuidInterface | |
| 34 | 44 | { |
| 35 | - // Specifically call parent::decode to preserve correct byte order | |
| 45 | + // Call parent::decode() to preserve the correct byte order. | |
| 36 | 46 | return parent::decode(bin2hex($bytes)); |
| 37 | 47 | } |
| 38 | 48 | /** |
| 39 | 49 | * Swaps bytes according to the GUID rules |
| 40 | 50 | */ |
| 41 | - private function swapBytes(string $bytes): string | |
| 51 | + private function swapBytes(string $bytes) : string | |
| 42 | 52 | { |
| 43 | 53 | return $bytes[3] . $bytes[2] . $bytes[1] . $bytes[0] . $bytes[5] . $bytes[4] . $bytes[7] . $bytes[6] . substr($bytes, 8); |
| 44 | 54 | } |
| 45 | 55 | } |