aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2020-11-27 08:04:00 +0000
committerMario <mario@mariovavti.com>2020-11-27 08:04:00 +0000
commitf4bb7bcbff3770387c2fecfa91ce4a60b916a474 (patch)
treeea007e664d435f1f3d63c87bfe1600484d2bd46c /vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
parent07e5b8295ea9d342f66d8119d88bd58124b548e6 (diff)
downloadvolse-hubzilla-f4bb7bcbff3770387c2fecfa91ce4a60b916a474.tar.gz
volse-hubzilla-f4bb7bcbff3770387c2fecfa91ce4a60b916a474.tar.bz2
volse-hubzilla-f4bb7bcbff3770387c2fecfa91ce4a60b916a474.zip
update composer libs
Diffstat (limited to 'vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php')
-rw-r--r--vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php126
1 files changed, 65 insertions, 61 deletions
diff --git a/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php b/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
index 9d13af70c..06ce38fbb 100644
--- a/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
+++ b/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,102 +8,105 @@
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
- * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
- * @link https://packagist.org/packages/ramsey/uuid Packagist
- * @link https://github.com/ramsey/uuid GitHub
*/
+
+declare(strict_types=1);
+
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\UuidInterface;
+use function bin2hex;
+use function sprintf;
+use function substr;
+use function substr_replace;
+
/**
- * TimestampFirstCombCodec encodes and decodes COMB UUIDs which have the timestamp as the first 48 bits.
- * To be used with MySQL, PostgreSQL, Oracle.
+ * TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the
+ * first 48 bits
+ *
+ * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec
+ * adds the timestamp to the first 48 bits of the COMB. To generate a
+ * timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along
+ * with the CombGenerator as the random generator.
+ *
+ * ``` php
+ * $factory = new UuidFactory();
+ *
+ * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
+ *
+ * $factory->setRandomGenerator(new CombGenerator(
+ * $factory->getRandomGenerator(),
+ * $factory->getNumberConverter()
+ * ));
+ *
+ * $timestampFirstComb = $factory->uuid4();
+ * ```
+ *
+ * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
+ *
+ * @psalm-immutable
*/
class TimestampFirstCombCodec extends StringCodec
{
/**
- * Encodes a UuidInterface as a string representation of a timestamp first COMB UUID
- *
- * @param UuidInterface $uuid
- *
- * @return string Hexadecimal string representation of a GUID
+ * @psalm-return non-empty-string
+ * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
+ * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
*/
- public function encode(UuidInterface $uuid)
+ public function encode(UuidInterface $uuid): string
{
- $sixPieceComponents = array_values($uuid->getFieldsHex());
-
- $this->swapTimestampAndRandomBits($sixPieceComponents);
+ $bytes = $this->swapBytes($uuid->getFields()->getBytes());
- return vsprintf(
- '%08s-%04s-%04s-%02s%02s-%012s',
- $sixPieceComponents
+ 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))
);
}
/**
- * Encodes a UuidInterface as a binary representation of timestamp first COMB UUID
- *
- * @param UuidInterface $uuid
- *
- * @return string Binary string representation of timestamp first COMB UUID
+ * @psalm-return non-empty-string
+ * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
+ * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
*/
- public function encodeBinary(UuidInterface $uuid)
+ public function encodeBinary(UuidInterface $uuid): string
{
- $stringEncoding = $this->encode($uuid);
-
- return hex2bin(str_replace('-', '', $stringEncoding));
+ return $this->swapBytes($uuid->getFields()->getBytes());
}
/**
- * Decodes a string representation of timestamp first COMB UUID into a UuidInterface object instance
- *
- * @param string $encodedUuid
- *
- * @return UuidInterface
* @throws InvalidUuidStringException
+ *
+ * @inheritDoc
*/
- public function decode($encodedUuid)
+ public function decode(string $encodedUuid): UuidInterface
{
- $fivePieceComponents = $this->extractComponents($encodedUuid);
-
- $this->swapTimestampAndRandomBits($fivePieceComponents);
+ $bytes = $this->getBytes($encodedUuid);
- return $this->getBuilder()->build($this, $this->getFields($fivePieceComponents));
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
- /**
- * Decodes a binary representation of timestamp first COMB UUID into a UuidInterface object instance
- *
- * @param string $bytes
- *
- * @return UuidInterface
- * @throws InvalidUuidStringException
- */
- public function decodeBytes($bytes)
+ public function decodeBytes(string $bytes): UuidInterface
{
- return $this->decode(bin2hex($bytes));
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
/**
- * Swaps the first 48 bits with the last 48 bits
- *
- * @param array $components An array of UUID components (the UUID exploded on its dashes)
- *
- * @return void
+ * Swaps bytes according to the timestamp-first COMB rules
*/
- protected function swapTimestampAndRandomBits(array &$components)
+ private function swapBytes(string $bytes): string
{
- $last48Bits = $components[4];
- if (count($components) == 6) {
- $last48Bits = $components[5];
- $components[5] = $components[0] . $components[1];
- } else {
- $components[4] = $components[0] . $components[1];
- }
+ $first48Bits = substr($bytes, 0, 6);
+ $last48Bits = substr($bytes, -6);
+
+ $bytes = substr_replace($bytes, $last48Bits, 0, 6);
+ $bytes = substr_replace($bytes, $first48Bits, -6);
- $components[0] = substr($last48Bits, 0, 8);
- $components[1] = substr($last48Bits, 8, 4);
+ return $bytes;
}
}