| 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 <[email protected]> |
| 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\Exception\InvalidUuidStringException; |
| 18 |
use Ramsey\Uuid\UuidInterface; |
| 19 |
|
| 20 |
use function bin2hex; |
| 21 |
use function sprintf; |
| 22 |
use function substr; |
| 23 |
use function substr_replace; |
| 24 |
|
| 25 |
/** |
| 26 |
* TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the |
| 27 |
* first 48 bits |
| 28 |
* |
| 29 |
* In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec |
| 30 |
* adds the timestamp to the first 48 bits of the COMB. To generate a |
| 31 |
* timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along |
| 32 |
* with the CombGenerator as the random generator. |
| 33 |
* |
| 34 |
* ``` php |
| 35 |
* $factory = new UuidFactory(); |
| 36 |
* |
| 37 |
* $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 38 |
* |
| 39 |
* $factory->setRandomGenerator(new CombGenerator( |
| 40 |
* $factory->getRandomGenerator(), |
| 41 |
* $factory->getNumberConverter() |
| 42 |
* )); |
| 43 |
* |
| 44 |
* $timestampFirstComb = $factory->uuid4(); |
| 45 |
* ``` |
| 46 |
* |
| 47 |
* @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys |
| 48 |
* |
| 49 |
* @psalm-immutable |
| 50 |
*/ |
| 51 |
class TimestampFirstCombCodec extends StringCodec |
| 52 |
{ |
| 53 |
/** |
| 54 |
* @psalm-return non-empty-string |
| 55 |
* @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty |
| 56 |
* @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty |
| 57 |
*/ |
| 58 |
public function encode(UuidInterface $uuid): string |
| 59 |
{ |
| 60 |
$bytes = $this->swapBytes($uuid->getFields()->getBytes()); |
| 61 |
|
| 62 |
return sprintf( |
| 63 |
'%08s-%04s-%04s-%04s-%012s', |
| 64 |
bin2hex(substr($bytes, 0, 4)), |
| 65 |
bin2hex(substr($bytes, 4, 2)), |
| 66 |
bin2hex(substr($bytes, 6, 2)), |
| 67 |
bin2hex(substr($bytes, 8, 2)), |
| 68 |
bin2hex(substr($bytes, 10)) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @psalm-return non-empty-string |
| 74 |
* @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty |
| 75 |
* @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty |
| 76 |
*/ |
| 77 |
public function encodeBinary(UuidInterface $uuid): string |
| 78 |
{ |
| 79 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 80 |
return $this->swapBytes($uuid->getFields()->getBytes()); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @throws InvalidUuidStringException |
| 85 |
* |
| 86 |
* @inheritDoc |
| 87 |
*/ |
| 88 |
public function decode(string $encodedUuid): UuidInterface |
| 89 |
{ |
| 90 |
$bytes = $this->getBytes($encodedUuid); |
| 91 |
|
| 92 |
return $this->getBuilder()->build($this, $this->swapBytes($bytes)); |
| 93 |
} |
| 94 |
|
| 95 |
public function decodeBytes(string $bytes): UuidInterface |
| 96 |
{ |
| 97 |
return $this->getBuilder()->build($this, $this->swapBytes($bytes)); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Swaps bytes according to the timestamp-first COMB rules |
| 102 |
*/ |
| 103 |
private function swapBytes(string $bytes): string |
| 104 |
{ |
| 105 |
$first48Bits = substr($bytes, 0, 6); |
| 106 |
$last48Bits = substr($bytes, -6); |
| 107 |
|
| 108 |
$bytes = substr_replace($bytes, $last48Bits, 0, 6); |
| 109 |
$bytes = substr_replace($bytes, $first48Bits, -6); |
| 110 |
|
| 111 |
return $bytes; |
| 112 |
} |
| 113 |
} |
| 114 |
|