summaryrefslogtreecommitdiffstats
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.php71
-rw-r--r--vendor/ramsey/uuid/src/Codec/GuidStringCodec.php55
-rw-r--r--vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php113
-rw-r--r--vendor/ramsey/uuid/src/Codec/StringCodec.php138
-rw-r--r--vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php113
-rw-r--r--vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php51
6 files changed, 541 insertions, 0 deletions
diff --git a/vendor/ramsey/uuid/src/Codec/CodecInterface.php b/vendor/ramsey/uuid/src/Codec/CodecInterface.php
new file mode 100644
index 0000000..85f8a7e
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/CodecInterface.php
@@ -0,0 +1,71 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Uuid\Codec;
+
+use Ramsey\Uuid\UuidInterface;
+
+/**
+ * A codec encodes and decodes a UUID according to defined rules
+ *
+ * @psalm-immutable
+ */
+interface CodecInterface
+{
+ /**
+ * Returns a hexadecimal string representation of a UuidInterface
+ *
+ * @param UuidInterface $uuid The UUID for which to create a hexadecimal
+ * string representation
+ *
+ * @return string Hexadecimal string representation of a UUID
+ *
+ * @psalm-return non-empty-string
+ */
+ public function encode(UuidInterface $uuid): string;
+
+ /**
+ * Returns a binary string representation of a UuidInterface
+ *
+ * @param UuidInterface $uuid The UUID for which to create a binary string
+ * representation
+ *
+ * @return string Binary string representation of a UUID
+ *
+ * @psalm-return non-empty-string
+ */
+ public function encodeBinary(UuidInterface $uuid): string;
+
+ /**
+ * Returns a UuidInterface derived from a hexadecimal string representation
+ *
+ * @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(string $encodedUuid): UuidInterface;
+
+ /**
+ * Returns a UuidInterface derived from a binary string representation
+ *
+ * @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(string $bytes): UuidInterface;
+}
diff --git a/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php b/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
new file mode 100644
index 0000000..f11e9d5
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Uuid\Codec;
+
+use Ramsey\Uuid\Guid\Guid;
+use Ramsey\Uuid\UuidInterface;
+
+use function bin2hex;
+use function substr;
+
+/**
+ * GuidStringCodec encodes and decodes globally unique identifiers (GUID)
+ *
+ * @see Guid
+ *
+ * @psalm-immutable
+ */
+class GuidStringCodec extends StringCodec
+{
+ public function decode(string $encodedUuid): UuidInterface
+ {
+ $bytes = $this->getBytes($encodedUuid);
+
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
+ }
+
+ public function decodeBytes(string $bytes): UuidInterface
+ {
+ // Specifically call parent::decode to preserve correct byte order
+ return parent::decode(bin2hex($bytes));
+ }
+
+ /**
+ * Swaps bytes according to the GUID rules
+ */
+ private function swapBytes(string $bytes): string
+ {
+ 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
new file mode 100644
index 0000000..0798ebc
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php
@@ -0,0 +1,113 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Uuid\Codec;
+
+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 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
+{
+ /**
+ * Returns a binary string representation of a UUID, with the timestamp
+ * fields rearranged for optimized storage
+ *
+ * @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): string
+ {
+ if (
+ !($uuid->getFields() instanceof Rfc4122FieldsInterface)
+ || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
+ ) {
+ throw new InvalidArgumentException(
+ 'Expected RFC 4122 version 1 (time-based) UUID'
+ );
+ }
+
+ $bytes = $uuid->getFields()->getBytes();
+
+ /** @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);
+ }
+
+ /**
+ * 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;
+ }
+}
diff --git a/vendor/ramsey/uuid/src/Codec/StringCodec.php b/vendor/ramsey/uuid/src/Codec/StringCodec.php
new file mode 100644
index 0000000..58c9f58
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/StringCodec.php
@@ -0,0 +1,138 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Uuid\Codec;
+
+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
+{
+ /**
+ * @var UuidBuilderInterface
+ */
+ private $builder;
+
+ /**
+ * Constructs a StringCodec
+ *
+ * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
+ */
+ public function __construct(UuidBuilderInterface $builder)
+ {
+ $this->builder = $builder;
+ }
+
+ public function encode(UuidInterface $uuid): string
+ {
+ /** @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();
+ }
+
+ /**
+ * @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): string
+ {
+ /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
+ return $uuid->getFields()->getBytes();
+ }
+
+ /**
+ * @throws InvalidUuidStringException
+ *
+ * @inheritDoc
+ */
+ public function decode(string $encodedUuid): UuidInterface
+ {
+ return $this->builder->build($this, $this->getBytes($encodedUuid));
+ }
+
+ public function decodeBytes(string $bytes): UuidInterface
+ {
+ if (strlen($bytes) !== 16) {
+ throw new InvalidArgumentException(
+ '$bytes string should contain 16 characters.'
+ );
+ }
+
+ return $this->builder->build($this, $bytes);
+ }
+
+ /**
+ * Returns the UUID builder
+ */
+ protected function getBuilder(): UuidBuilderInterface
+ {
+ return $this->builder;
+ }
+
+ /**
+ * Returns a byte string of the UUID
+ */
+ protected function getBytes(string $encodedUuid): string
+ {
+ $parsedUuid = str_replace(
+ ['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'],
+ '',
+ $encodedUuid
+ );
+
+ $components = [
+ substr($parsedUuid, 0, 8),
+ substr($parsedUuid, 8, 4),
+ substr($parsedUuid, 12, 4),
+ substr($parsedUuid, 16, 4),
+ substr($parsedUuid, 20),
+ ];
+
+ if (!Uuid::isValid(implode('-', $components))) {
+ throw new InvalidUuidStringException(
+ 'Invalid UUID string: ' . $encodedUuid
+ );
+ }
+
+ return (string) hex2bin($parsedUuid);
+ }
+}
diff --git a/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php b/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
new file mode 100644
index 0000000..0e0042d
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php
@@ -0,0 +1,113 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+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 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
+{
+ /**
+ * @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): string
+ {
+ $bytes = $this->swapBytes($uuid->getFields()->getBytes());
+
+ 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))
+ );
+ }
+
+ /**
+ * @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): string
+ {
+ /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
+ return $this->swapBytes($uuid->getFields()->getBytes());
+ }
+
+ /**
+ * @throws InvalidUuidStringException
+ *
+ * @inheritDoc
+ */
+ public function decode(string $encodedUuid): UuidInterface
+ {
+ $bytes = $this->getBytes($encodedUuid);
+
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
+ }
+
+ public function decodeBytes(string $bytes): UuidInterface
+ {
+ return $this->getBuilder()->build($this, $this->swapBytes($bytes));
+ }
+
+ /**
+ * Swaps bytes according to the timestamp-first COMB rules
+ */
+ private function swapBytes(string $bytes): string
+ {
+ $first48Bits = substr($bytes, 0, 6);
+ $last48Bits = substr($bytes, -6);
+
+ $bytes = substr_replace($bytes, $last48Bits, 0, 6);
+ $bytes = substr_replace($bytes, $first48Bits, -6);
+
+ return $bytes;
+ }
+}
diff --git a/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php b/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php
new file mode 100644
index 0000000..4856dea
--- /dev/null
+++ b/vendor/ramsey/uuid/src/Codec/TimestampLastCombCodec.php
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * This file is part of the ramsey/uuid library
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
+ * @license http://opensource.org/licenses/MIT MIT
+ */
+
+declare(strict_types=1);
+
+namespace Ramsey\Uuid\Codec;
+
+/**
+ * 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
+{
+}