aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-01-13 09:50:53 +0000
committerMario <mario@mariovavti.com>2021-01-13 09:50:53 +0000
commit5eefdc6485b2f6082f6fe5dfd6f1731fae7e3a2a (patch)
tree7521f4800e393538d19c393c6f495ea2d41cbf5a /vendor/ramsey/uuid/src/Codec/GuidStringCodec.php
parent0bc4c7d1a0e4348018e533be600ad1c648fd97fb (diff)
parent4d2bcbc5837a7d99dc541595ca8087c335242af0 (diff)
downloadvolse-hubzilla-5eefdc6485b2f6082f6fe5dfd6f1731fae7e3a2a.tar.gz
volse-hubzilla-5eefdc6485b2f6082f6fe5dfd6f1731fae7e3a2a.tar.bz2
volse-hubzilla-5eefdc6485b2f6082f6fe5dfd6f1731fae7e3a2a.zip
Merge branch '5.2RC'5.2
Diffstat (limited to 'vendor/ramsey/uuid/src/Codec/GuidStringCodec.php')
-rw-r--r--vendor/ramsey/uuid/src/Codec/GuidStringCodec.php88
1 files changed, 20 insertions, 68 deletions
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);
}
}