← All changes
|
includes/sdk/google/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
+27
-27
1.2.10
→
1.4.1
View file →
| @@ -9,26 +9,24 @@ | ||
| 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\Exception\InvalidUuidStringException; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidUuidStringException; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidInterface; | |
| 17 | 17 | use function bin2hex; |
| 18 | 18 | use function sprintf; |
| 19 | 19 | use function substr; |
| 20 | 20 | use function substr_replace; |
| 21 | 21 | /** |
| 22 | - * TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the | |
| 23 | - * first 48 bits | |
| 22 | + * TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the first 48 bits | |
| 24 | 23 | * |
| 25 | - * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec | |
| 26 | - * adds the timestamp to the first 48 bits of the COMB. To generate a | |
| 27 | - * timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along | |
| 28 | - * with the CombGenerator as the random generator. | |
| 24 | + * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec adds the timestamp to the first 48 bits of | |
| 25 | + * the COMB. To generate a timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along with the | |
| 26 | + * CombGenerator as the random generator. | |
| 29 | 27 | * |
| 30 | - * ``` php | |
| 28 | + * ``` | |
| 31 | 29 | * $factory = new UuidFactory(); |
| 32 | 30 | * |
| 33 | 31 | * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 34 | 32 | * |
| @@ -33,36 +31,35 @@ | ||
| 33 | 31 | * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 34 | 32 | * |
| 35 | 33 | * $factory->setRandomGenerator(new CombGenerator( |
| 36 | 34 | * $factory->getRandomGenerator(), |
| 37 | - * $factory->getNumberConverter() | |
| 35 | + * $factory->getNumberConverter(), | |
| 38 | 36 | * )); |
| 39 | 37 | * |
| 40 | 38 | * $timestampFirstComb = $factory->uuid4(); |
| 41 | 39 | * ``` |
| 42 | 40 | * |
| 43 | - * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys | |
| 41 | + * @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version7.html Version 7, Unix Epoch Time UUIDs}. | |
| 44 | 42 | * |
| 45 | - * @psalm-immutable | |
| 43 | + * @link https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys | |
| 44 | + * | |
| 45 | + * @immutable | |
| 46 | 46 | */ |
| 47 | 47 | class TimestampFirstCombCodec extends StringCodec |
| 48 | 48 | { |
| 49 | 49 | /** |
| 50 | - * @psalm-return non-empty-string | |
| 51 | - * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty | |
| 52 | - * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty | |
| 50 | + * @return non-empty-string | |
| 53 | 51 | */ |
| 54 | - public function encode(UuidInterface $uuid): string | |
| 52 | + public function encode(UuidInterface $uuid) : string | |
| 55 | 53 | { |
| 54 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 56 | 55 | $bytes = $this->swapBytes($uuid->getFields()->getBytes()); |
| 57 | 56 | return sprintf('%08s-%04s-%04s-%04s-%012s', bin2hex(substr($bytes, 0, 4)), bin2hex(substr($bytes, 4, 2)), bin2hex(substr($bytes, 6, 2)), bin2hex(substr($bytes, 8, 2)), bin2hex(substr($bytes, 10))); |
| 58 | 57 | } |
| 59 | 58 | /** |
| 60 | - * @psalm-return non-empty-string | |
| 61 | - * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty | |
| 62 | - * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty | |
| 59 | + * @return non-empty-string | |
| 63 | 60 | */ |
| 64 | - public function encodeBinary(UuidInterface $uuid): string | |
| 61 | + public function encodeBinary(UuidInterface $uuid) : string | |
| 65 | 62 | { |
| 66 | 63 | /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 67 | 64 | return $this->swapBytes($uuid->getFields()->getBytes()); |
| 68 | 65 | } |
| @@ -70,25 +67,28 @@ | ||
| 70 | 67 | * @throws InvalidUuidStringException |
| 71 | 68 | * |
| 72 | 69 | * @inheritDoc |
| 73 | 70 | */ |
| 74 | - public function decode(string $encodedUuid): UuidInterface | |
| 71 | + public function decode(string $encodedUuid) : UuidInterface | |
| 75 | 72 | { |
| 73 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 76 | 74 | $bytes = $this->getBytes($encodedUuid); |
| 75 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 77 | 76 | return $this->getBuilder()->build($this, $this->swapBytes($bytes)); |
| 78 | 77 | } |
| 79 | - public function decodeBytes(string $bytes): UuidInterface | |
| 78 | + public function decodeBytes(string $bytes) : UuidInterface | |
| 80 | 79 | { |
| 80 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 81 | 81 | return $this->getBuilder()->build($this, $this->swapBytes($bytes)); |
| 82 | 82 | } |
| 83 | 83 | /** |
| 84 | 84 | * Swaps bytes according to the timestamp-first COMB rules |
| 85 | + * | |
| 86 | + * @pure | |
| 85 | 87 | */ |
| 86 | - private function swapBytes(string $bytes): string | |
| 88 | + private function swapBytes(string $bytes) : string | |
| 87 | 89 | { |
| 88 | 90 | $first48Bits = substr($bytes, 0, 6); |
| 89 | 91 | $last48Bits = substr($bytes, -6); |
| 90 | - $bytes = substr_replace($bytes, $last48Bits, 0, 6); | |
| 91 | - $bytes = substr_replace($bytes, $first48Bits, -6); | |
| 92 | - return $bytes; | |
| 92 | + return substr_replace(substr_replace($bytes, $last48Bits, 0, 6), $first48Bits, -6); | |
| 93 | 93 | } |
| 94 | 94 | } |