aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/uuid/src/Codec
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ramsey/uuid/src/Codec')
-rw-r--r--vendor/ramsey/uuid/src/Codec/CodecInterface.php57
-rw-r--r--vendor/ramsey/uuid/src/Codec/GuidStringCodec.php88
-rw-r--r--vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php106
-rw-r--r--vendor/ramsey/uuid/src/Codec/StringCodec.php157
-rw-r--r--vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php126
-rw-r--r--vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php39
6 files changed, 290 insertions, 283 deletions
diff --git a/vendor/ramsey/uuid/src/Codec/CodecInterface.php b/vendor/ramsey/uuid/src/Codec/CodecInterface.php
index c6c54c78a..85f8a7e99 100644
--- a/vendor/ramsey/uuid/src/Codec/CodecInterface.php
+++ b/vendor/ramsey/uuid/src/Codec/CodecInterface.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,54 +8,64 @@
*
* @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 InvalidArgumentException;
-use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\UuidInterface;
/**
- * CodecInterface represents a UUID coder-decoder
+ * A codec encodes and decodes a UUID according to defined rules
+ *
+ * @psalm-immutable
*/
interface CodecInterface
{
/**
- * Encodes a UuidInterface as a string representation of a UUID
+ * Returns a hexadecimal string representation of a UuidInterface
+ *
+ * @param UuidInterface $uuid The UUID for which to create a hexadecimal
+ * string representation
*
- * @param UuidInterface $uuid
* @return string Hexadecimal string representation of a UUID
+ *
+ * @psalm-return non-empty-string
*/
- public function encode(UuidInterface $uuid);
+ public function encode(UuidInterface $uuid): string;
/**
- * Encodes a UuidInterface as a binary representation of a UUID
+ * Returns a binary string representation of a UuidInterface
+ *
+ * @param UuidInterface $uuid The UUID for which to create a binary string
+ * representation
*
- * @param UuidInterface $uuid
* @return string Binary string representation of a UUID
+ *
+ * @psalm-return non-empty-string
*/
- public function encodeBinary(UuidInterface $uuid);
+ public function encodeBinary(UuidInterface $uuid): string;
/**
- * Decodes a string representation of a UUID into a UuidInterface object instance
+ * Returns a UuidInterface derived from a hexadecimal string representation
*
- * @param string $encodedUuid
- * @return UuidInterface
- * @throws InvalidUuidStringException
+ * @param string $encodedUuid The hexadecimal string representation to
+ * convert into a UuidInterface instance
+ *
+ * @return UuidInterface An instance of a UUID decoded from a hexadecimal
+ * string representation
*/
- public function decode($encodedUuid);
+ public function decode(string $encodedUuid): UuidInterface;
/**
- * Decodes a binary representation of a UUID into a UuidInterface object instance
+ * Returns a UuidInterface derived from a binary string representation
+ *
+ * @param string $bytes The binary string representation to convert into a
+ * UuidInterface instance
*
- * @param string $bytes
- * @return UuidInterface
- * @throws InvalidUuidStringException
- * @throws InvalidArgumentException if string has not 16 characters
+ * @return UuidInterface An instance of a UUID decoded from a binary string
+ * representation
*/
- public function decodeBytes($bytes);
+ public function decodeBytes(string $bytes): UuidInterface;
}
diff --git a/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php b/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
index 367548070..f11e9d50a 100644
--- a/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
+++ b/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,97 +8,48 @@
*
* @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\Guid\Guid;
use Ramsey\Uuid\UuidInterface;
+use function bin2hex;
+use function substr;
+
/**
* GuidStringCodec encodes and decodes globally unique identifiers (GUID)
*
- * @link https://en.wikipedia.org/wiki/Globally_unique_identifier
+ * @see Guid
+ *
+ * @psalm-immutable
*/
class GuidStringCodec extends StringCodec
{
- /**
- * Encodes a UuidInterface as a string representation of a GUID
- *
- * @param UuidInterface $uuid
- * @return string Hexadecimal string representation of a GUID
- */
- public function encode(UuidInterface $uuid)
- {
- $components = array_values($uuid->getFieldsHex());
-
- // Swap byte-order on the first three fields
- $this->swapFields($components);
-
- return vsprintf(
- '%08s-%04s-%04s-%02s%02s-%012s',
- $components
- );
- }
-
- /**
- * Encodes a UuidInterface as a binary representation of a GUID
- *
- * @param UuidInterface $uuid
- * @return string Binary string representation of a GUID
- */
- public function encodeBinary(UuidInterface $uuid)
+ public function decode(string $encodedUuid): UuidInterface
{
- $components = array_values($uuid->getFieldsHex());
+ $bytes = $this->getBytes($encodedUuid);
- return hex2bin(implode('', $components));
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
- /**
- * Decodes a string representation of a GUID into a UuidInterface object instance
- *
- * @param string $encodedUuid
- * @return UuidInterface
- * @throws InvalidUuidStringException
- */
- public function decode($encodedUuid)
- {
- $components = $this->extractComponents($encodedUuid);
-
- $this->swapFields($components);
-
- return $this->getBuilder()->build($this, $this->getFields($components));
- }
-
- /**
- * Decodes a binary representation of a GUID into a UuidInterface object instance
- *
- * @param string $bytes
- * @return UuidInterface
- * @throws InvalidUuidStringException
- */
- public function decodeBytes($bytes)
+ public function decodeBytes(string $bytes): UuidInterface
{
// Specifically call parent::decode to preserve correct byte order
return parent::decode(bin2hex($bytes));
}
/**
- * Swaps fields to support GUID byte order
- *
- * @param array $components An array of UUID components (the UUID exploded on its dashes)
- * @return void
+ * Swaps bytes according to the GUID rules
*/
- protected function swapFields(array &$components)
+ private function swapBytes(string $bytes): string
{
- $hex = unpack('H*', pack('L', hexdec($components[0])));
- $components[0] = $hex[1];
- $hex = unpack('H*', pack('S', hexdec($components[1])));
- $components[1] = $hex[1];
- $hex = unpack('H*', pack('S', hexdec($components[2])));
- $components[2] = $hex[1];
+ return $bytes[3] . $bytes[2] . $bytes[1] . $bytes[0]
+ . $bytes[5] . $bytes[4]
+ . $bytes[7] . $bytes[6]
+ . substr($bytes, 8);
}
}
diff --git a/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php b/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
index de91aab8d..fe9f57c76 100644
--- a/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
+++ b/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,62 +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 InvalidArgumentException;
+use Ramsey\Uuid\Exception\InvalidArgumentException;
+use Ramsey\Uuid\Exception\UnsupportedOperationException;
+use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
+use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
+use function strlen;
+use function substr;
+
/**
- * OrderedTimeCodec optimizes the bytes to increment UUIDs when time goes by, to improve database INSERTs.
- * The string value will be unchanged from StringCodec. Only works for UUID type 1.
+ * OrderedTimeCodec encodes and decodes a UUID, optimizing the byte order for
+ * more efficient storage
+ *
+ * For binary representations of version 1 UUID, this codec may be used to
+ * reorganize the time fields, making the UUID closer to sequential when storing
+ * the bytes. According to Percona, this optimization can improve database
+ * INSERTs and SELECTs using the UUID column as a key.
+ *
+ * The string representation of the UUID will remain unchanged. Only the binary
+ * representation is reordered.
+ *
+ * **PLEASE NOTE:** Binary representations of UUIDs encoded with this codec must
+ * be decoded with this codec. Decoding using another codec can result in
+ * malformed UUIDs.
+ *
+ * @link https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/ Storing UUID Values in MySQL
+ *
+ * @psalm-immutable
*/
class OrderedTimeCodec extends StringCodec
{
-
/**
- * Encodes a UuidInterface as an optimized binary representation of a UUID
+ * Returns a binary string representation of a UUID, with the timestamp
+ * fields rearranged for optimized storage
*
- * @param UuidInterface $uuid
- * @return string Binary string representation of a UUID
+ * @inheritDoc
+ * @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
{
- $fields = $uuid->getFieldsHex();
+ if (
+ !($uuid->getFields() instanceof Rfc4122FieldsInterface)
+ || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
+ ) {
+ throw new InvalidArgumentException(
+ 'Expected RFC 4122 version 1 (time-based) UUID'
+ );
+ }
- $optimized = [
- $fields['time_hi_and_version'],
- $fields['time_mid'],
- $fields['time_low'],
- $fields['clock_seq_hi_and_reserved'],
- $fields['clock_seq_low'],
- $fields['node'],
- ];
+ $bytes = $uuid->getFields()->getBytes();
- return hex2bin(implode('', $optimized));
+ return $bytes[6] . $bytes[7]
+ . $bytes[4] . $bytes[5]
+ . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
+ . substr($bytes, 8);
}
/**
- * Decodes an optimized binary representation of a UUID into a UuidInterface object instance
+ * Returns a UuidInterface derived from an ordered-time binary string
+ * representation
*
- * @param string $bytes
- * @return UuidInterface
- * @throws InvalidArgumentException if string has not 16 characters
+ * @throws InvalidArgumentException if $bytes is an invalid length
+ *
+ * @inheritDoc
*/
- public function decodeBytes($bytes)
+ public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
- throw new InvalidArgumentException('$bytes string should contain 16 characters.');
+ throw new InvalidArgumentException(
+ '$bytes string should contain 16 characters.'
+ );
}
- $hex = unpack('H*', $bytes)[1];
+ // Rearrange the bytes to their original order.
+ $rearrangedBytes = $bytes[4] . $bytes[5] . $bytes[6] . $bytes[7]
+ . $bytes[2] . $bytes[3]
+ . $bytes[0] . $bytes[1]
+ . substr($bytes, 8);
+
+ $uuid = parent::decodeBytes($rearrangedBytes);
- // Rearrange the fields to their original order
- $hex = substr($hex, 8, 4) . substr($hex, 12, 4) . substr($hex, 4, 4) . substr($hex, 0, 4) . substr($hex, 16);
+ if (
+ !($uuid->getFields() instanceof Rfc4122FieldsInterface)
+ || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
+ ) {
+ throw new UnsupportedOperationException(
+ 'Attempting to decode a non-time-based UUID using '
+ . 'OrderedTimeCodec'
+ );
+ }
- return $this->decode($hex);
+ return $uuid;
}
}
diff --git a/vendor/ramsey/uuid/src/Codec/StringCodec.php b/vendor/ramsey/uuid/src/Codec/StringCodec.php
index f1bc0249a..fff13bd81 100644
--- a/vendor/ramsey/uuid/src/Codec/StringCodec.php
+++ b/vendor/ramsey/uuid/src/Codec/StringCodec.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,23 +8,31 @@
*
* @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 InvalidArgumentException;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
+use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
+use Ramsey\Uuid\Rfc4122\FieldsInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
+use function hex2bin;
+use function implode;
+use function str_replace;
+use function strlen;
+use function substr;
+
/**
* StringCodec encodes and decodes RFC 4122 UUIDs
*
* @link http://tools.ietf.org/html/rfc4122
+ *
+ * @psalm-immutable
*/
class StringCodec implements CodecInterface
{
@@ -33,138 +42,96 @@ class StringCodec implements CodecInterface
private $builder;
/**
- * Constructs a StringCodec for use encoding and decoding UUIDs
+ * Constructs a StringCodec
*
- * @param UuidBuilderInterface $builder The UUID builder to use when encoding UUIDs
+ * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
*/
public function __construct(UuidBuilderInterface $builder)
{
$this->builder = $builder;
}
- /**
- * Encodes a UuidInterface as a string representation of a UUID
- *
- * @param UuidInterface $uuid
- * @return string Hexadecimal string representation of a UUID
- */
- public function encode(UuidInterface $uuid)
+ public function encode(UuidInterface $uuid): string
{
- $fields = array_values($uuid->getFieldsHex());
-
- return vsprintf(
- '%08s-%04s-%04s-%02s%02s-%012s',
- $fields
- );
+ /** @var FieldsInterface $fields */
+ $fields = $uuid->getFields();
+
+ return $fields->getTimeLow()->toString()
+ . '-'
+ . $fields->getTimeMid()->toString()
+ . '-'
+ . $fields->getTimeHiAndVersion()->toString()
+ . '-'
+ . $fields->getClockSeqHiAndReserved()->toString()
+ . $fields->getClockSeqLow()->toString()
+ . '-'
+ . $fields->getNode()->toString();
}
/**
- * Encodes a UuidInterface as a binary representation of a UUID
- *
- * @param UuidInterface $uuid
- * @return string Binary string representation of a 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
{
- return hex2bin($uuid->getHex());
+ return $uuid->getFields()->getBytes();
}
/**
- * Decodes a string representation of a UUID into a UuidInterface object instance
- *
- * @param string $encodedUuid
- * @return UuidInterface
* @throws InvalidUuidStringException
+ *
+ * @inheritDoc
*/
- public function decode($encodedUuid)
+ public function decode(string $encodedUuid): UuidInterface
{
- $components = $this->extractComponents($encodedUuid);
- $fields = $this->getFields($components);
-
- return $this->builder->build($this, $fields);
+ return $this->builder->build($this, $this->getBytes($encodedUuid));
}
- /**
- * Decodes a binary representation of a UUID into a UuidInterface object instance
- *
- * @param string $bytes
- * @return UuidInterface
- * @throws InvalidArgumentException if string has not 16 characters
- */
- public function decodeBytes($bytes)
+ public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
- throw new InvalidArgumentException('$bytes string should contain 16 characters.');
+ throw new InvalidArgumentException(
+ '$bytes string should contain 16 characters.'
+ );
}
- $hexUuid = unpack('H*', $bytes);
-
- return $this->decode($hexUuid[1]);
+ return $this->builder->build($this, $bytes);
}
/**
* Returns the UUID builder
- *
- * @return UuidBuilderInterface
*/
- protected function getBuilder()
+ protected function getBuilder(): UuidBuilderInterface
{
return $this->builder;
}
/**
- * Returns an array of UUID components (the UUID exploded on its dashes)
- *
- * @param string $encodedUuid
- * @return array
- * @throws InvalidUuidStringException
+ * Returns a byte string of the UUID
*/
- protected function extractComponents($encodedUuid)
+ protected function getBytes(string $encodedUuid): string
{
- $nameParsed = str_replace([
- 'urn:',
- 'uuid:',
- '{',
- '}',
- '-'
- ], '', $encodedUuid);
-
- // We have stripped out the dashes and are breaking up the string using
- // substr(). In this way, we can accept a full hex value that doesn't
- // contain dashes.
+ $parsedUuid = str_replace(
+ ['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'],
+ '',
+ $encodedUuid
+ );
+
$components = [
- substr($nameParsed, 0, 8),
- substr($nameParsed, 8, 4),
- substr($nameParsed, 12, 4),
- substr($nameParsed, 16, 4),
- substr($nameParsed, 20)
+ substr($parsedUuid, 0, 8),
+ substr($parsedUuid, 8, 4),
+ substr($parsedUuid, 12, 4),
+ substr($parsedUuid, 16, 4),
+ substr($parsedUuid, 20),
];
- $nameParsed = implode('-', $components);
-
- if (!Uuid::isValid($nameParsed)) {
- throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);
+ if (!Uuid::isValid(implode('-', $components))) {
+ throw new InvalidUuidStringException(
+ 'Invalid UUID string: ' . $encodedUuid
+ );
}
- return $components;
- }
-
- /**
- * Returns the fields that make up this UUID
- *
- * @see \Ramsey\Uuid\UuidInterface::getFieldsHex()
- * @param array $components
- * @return array
- */
- protected function getFields(array $components)
- {
- return [
- 'time_low' => str_pad($components[0], 8, '0', STR_PAD_LEFT),
- 'time_mid' => str_pad($components[1], 4, '0', STR_PAD_LEFT),
- 'time_hi_and_version' => str_pad($components[2], 4, '0', STR_PAD_LEFT),
- 'clock_seq_hi_and_reserved' => str_pad(substr($components[3], 0, 2), 2, '0', STR_PAD_LEFT),
- 'clock_seq_low' => str_pad(substr($components[3], 2), 2, '0', STR_PAD_LEFT),
- 'node' => str_pad($components[4], 12, '0', STR_PAD_LEFT)
- ];
+ return (string) hex2bin($parsedUuid);
}
}
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;
}
}
diff --git a/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php b/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php
index 240f613e2..4856deaed 100644
--- a/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php
+++ b/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php
@@ -1,4 +1,5 @@
<?php
+
/**
* This file is part of the ramsey/uuid library
*
@@ -7,15 +8,43 @@
*
* @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;
/**
- * TimestampLastCombCodec encodes and decodes COMB UUIDs which have the timestamp as the last 48 bits.
- * To be used with MSSQL.
+ * TimestampLastCombCodec encodes and decodes COMBs, with the timestamp as the
+ * last 48 bits
+ *
+ * The CombGenerator when used with the StringCodec (and, by proxy, the
+ * TimestampLastCombCodec) adds the timestamp to the last 48 bits of the COMB.
+ * The TimestampLastCombCodec is provided for the sake of consistency. In
+ * practice, it is identical to the standard StringCodec but, it may be used
+ * with the CombGenerator for additional context when reading code.
+ *
+ * Consider the following code. By default, the codec used by UuidFactory is the
+ * StringCodec, but here, we explicitly set the TimestampLastCombCodec. It is
+ * redundant, but it is clear that we intend this COMB to be generated with the
+ * timestamp appearing at the end.
+ *
+ * ``` php
+ * $factory = new UuidFactory();
+ *
+ * $factory->setCodec(new TimestampLastCombCodec($factory->getUuidBuilder()));
+ *
+ * $factory->setRandomGenerator(new CombGenerator(
+ * $factory->getRandomGenerator(),
+ * $factory->getNumberConverter()
+ * ));
+ *
+ * $timestampLastComb = $factory->uuid4();
+ * ```
+ *
+ * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
+ *
+ * @psalm-immutable
*/
class TimestampLastCombCodec extends StringCodec
{