Upgrade framework
This commit is contained in:
52
vendor/ramsey/uuid/src/Codec/CodecInterface.php
vendored
52
vendor/ramsey/uuid/src/Codec/CodecInterface.php
vendored
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,49 +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 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
|
||||
* @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
|
||||
* @return UuidInterface
|
||||
* @param string $bytes The binary string representation to convert into a
|
||||
* UuidInterface instance
|
||||
*
|
||||
* @return UuidInterface An instance of a UUID decoded from a binary string
|
||||
* representation
|
||||
*/
|
||||
public function decodeBytes($bytes);
|
||||
public function decodeBytes(string $bytes): UuidInterface;
|
||||
}
|
||||
|
||||
96
vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
vendored
96
vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
vendored
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,94 +8,69 @@
|
||||
*
|
||||
* @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\Guid\Guid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
use function bin2hex;
|
||||
use function sprintf;
|
||||
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)
|
||||
public function encode(UuidInterface $uuid): string
|
||||
{
|
||||
$components = array_values($uuid->getFieldsHex());
|
||||
$hex = bin2hex($uuid->getFields()->getBytes());
|
||||
|
||||
// Swap byte-order on the first three fields
|
||||
$this->swapFields($components);
|
||||
|
||||
return vsprintf(
|
||||
'%08s-%04s-%04s-%02s%02s-%012s',
|
||||
$components
|
||||
/** @var non-empty-string */
|
||||
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),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
124
vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
vendored
124
vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
vendored
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,61 +8,106 @@
|
||||
*
|
||||
* @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();
|
||||
|
||||
$optimized = [
|
||||
$fields['time_hi_and_version'],
|
||||
$fields['time_mid'],
|
||||
$fields['time_low'],
|
||||
$fields['clock_seq_hi_and_reserved'],
|
||||
$fields['clock_seq_low'],
|
||||
$fields['node'],
|
||||
];
|
||||
|
||||
return hex2bin(implode('', $optimized));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes an optimized binary representation of a UUID into a UuidInterface object instance
|
||||
*
|
||||
* @param string $bytes
|
||||
* @return UuidInterface
|
||||
*/
|
||||
public function decodeBytes($bytes)
|
||||
{
|
||||
if (strlen($bytes) !== 16) {
|
||||
throw new InvalidArgumentException('$bytes string should contain 16 characters.');
|
||||
if (
|
||||
!($uuid->getFields() instanceof Rfc4122FieldsInterface)
|
||||
|| $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
|
||||
) {
|
||||
throw new InvalidArgumentException(
|
||||
'Expected RFC 4122 version 1 (time-based) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
$hex = unpack('H*', $bytes)[1];
|
||||
$bytes = $uuid->getFields()->getBytes();
|
||||
|
||||
// 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);
|
||||
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
|
||||
return $bytes[6] . $bytes[7]
|
||||
. $bytes[4] . $bytes[5]
|
||||
. $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
|
||||
. substr($bytes, 8);
|
||||
}
|
||||
|
||||
return $this->decode($hex);
|
||||
/**
|
||||
* Returns a UuidInterface derived from an ordered-time binary string
|
||||
* representation
|
||||
*
|
||||
* @throws InvalidArgumentException if $bytes is an invalid length
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function decodeBytes(string $bytes): UuidInterface
|
||||
{
|
||||
if (strlen($bytes) !== 16) {
|
||||
throw new InvalidArgumentException(
|
||||
'$bytes string should contain 16 characters.'
|
||||
);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
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 $uuid;
|
||||
}
|
||||
}
|
||||
|
||||
158
vendor/ramsey/uuid/src/Codec/StringCodec.php
vendored
158
vendor/ramsey/uuid/src/Codec/StringCodec.php
vendored
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,161 +8,124 @@
|
||||
*
|
||||
* @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\Uuid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
use function bin2hex;
|
||||
use function hex2bin;
|
||||
use function implode;
|
||||
use function sprintf;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* @var UuidBuilderInterface
|
||||
*/
|
||||
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)
|
||||
public function __construct(private 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());
|
||||
$hex = bin2hex($uuid->getFields()->getBytes());
|
||||
|
||||
return vsprintf(
|
||||
'%08s-%04s-%04s-%02s%02s-%012s',
|
||||
$fields
|
||||
/** @var non-empty-string */
|
||||
return sprintf(
|
||||
'%08s-%04s-%04s-%04s-%012s',
|
||||
substr($hex, 0, 8),
|
||||
substr($hex, 8, 4),
|
||||
substr($hex, 12, 4),
|
||||
substr($hex, 16, 4),
|
||||
substr($hex, 20),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
|
||||
return $uuid->getFields()->getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a string representation of a UUID into a UuidInterface object instance
|
||||
* @throws InvalidUuidStringException
|
||||
*
|
||||
* @param string $encodedUuid
|
||||
* @return UuidInterface
|
||||
* @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
|
||||
*/
|
||||
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
|
||||
* Returns a byte string of the UUID
|
||||
*/
|
||||
protected function extractComponents($encodedUuid)
|
||||
protected function getBytes(string $encodedUuid): string
|
||||
{
|
||||
$nameParsed = str_replace(array(
|
||||
'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.
|
||||
$components = array(
|
||||
substr($nameParsed, 0, 8),
|
||||
substr($nameParsed, 8, 4),
|
||||
substr($nameParsed, 12, 4),
|
||||
substr($nameParsed, 16, 4),
|
||||
substr($nameParsed, 20)
|
||||
$parsedUuid = str_replace(
|
||||
['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'],
|
||||
'',
|
||||
$encodedUuid
|
||||
);
|
||||
|
||||
$nameParsed = implode('-', $components);
|
||||
$components = [
|
||||
substr($parsedUuid, 0, 8),
|
||||
substr($parsedUuid, 8, 4),
|
||||
substr($parsedUuid, 12, 4),
|
||||
substr($parsedUuid, 16, 4),
|
||||
substr($parsedUuid, 20),
|
||||
];
|
||||
|
||||
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 array(
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,99 +8,106 @@
|
||||
*
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* TimestampLastCombCodec 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());
|
||||
$bytes = $this->swapBytes($uuid->getFields()->getBytes());
|
||||
|
||||
$this->swapTimestampAndRandomBits($sixPieceComponents);
|
||||
|
||||
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));
|
||||
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
|
||||
return $this->swapBytes($uuid->getFields()->getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a string representation of timestamp first COMB UUID into a UuidInterface object instance
|
||||
* @throws InvalidUuidStringException
|
||||
*
|
||||
* @param string $encodedUuid
|
||||
*
|
||||
* @return UuidInterface
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function decode($encodedUuid)
|
||||
public function decode(string $encodedUuid): UuidInterface
|
||||
{
|
||||
$fivePieceComponents = $this->extractComponents($encodedUuid);
|
||||
$bytes = $this->getBytes($encodedUuid);
|
||||
|
||||
$this->swapTimestampAndRandomBits($fivePieceComponents);
|
||||
return $this->getBuilder()->build($this, $this->swapBytes($bytes));
|
||||
}
|
||||
|
||||
return $this->getBuilder()->build($this, $this->getFields($fivePieceComponents));
|
||||
public function decodeBytes(string $bytes): UuidInterface
|
||||
{
|
||||
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
|
||||
* Swaps bytes according to the timestamp-first COMB rules
|
||||
*/
|
||||
public function decodeBytes($bytes)
|
||||
private function swapBytes(string $bytes): string
|
||||
{
|
||||
return $this->decode(bin2hex($bytes));
|
||||
}
|
||||
$first48Bits = substr($bytes, 0, 6);
|
||||
$last48Bits = substr($bytes, -6);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected function swapTimestampAndRandomBits(array &$components)
|
||||
{
|
||||
$last48Bits = $components[4];
|
||||
if (count($components) == 6) {
|
||||
$last48Bits = $components[5];
|
||||
$components[5] = $components[0] . $components[1];
|
||||
} else {
|
||||
$components[4] = $components[0] . $components[1];
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,17 +8,44 @@
|
||||
*
|
||||
* @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
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user