[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/ramsey/uuid/src/Codec/ -> TimestampFirstCombCodec.php (source)

   1  <?php
   2  /**
   3   * This file is part of the ramsey/uuid library
   4   *
   5   * For the full copyright and license information, please view the LICENSE
   6   * file that was distributed with this source code.
   7   *
   8   * @copyright Copyright (c) Ben Ramsey <[email protected]>
   9   * @license http://opensource.org/licenses/MIT MIT
  10   * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
  11   * @link https://packagist.org/packages/ramsey/uuid Packagist
  12   * @link https://github.com/ramsey/uuid GitHub
  13   */
  14  namespace Ramsey\Uuid\Codec;
  15  
  16  use Ramsey\Uuid\Exception\InvalidUuidStringException;
  17  use Ramsey\Uuid\UuidInterface;
  18  
  19  /**
  20   * TimestampFirstCombCodec encodes and decodes COMB UUIDs which have the timestamp as the first 48 bits.
  21   * To be used with MySQL, PostgreSQL, Oracle.
  22   */
  23  class TimestampFirstCombCodec extends StringCodec
  24  {
  25      /**
  26       * Encodes a UuidInterface as a string representation of a timestamp first COMB UUID
  27       *
  28       * @param UuidInterface $uuid
  29       *
  30       * @return string Hexadecimal string representation of a GUID
  31       */
  32      public function encode(UuidInterface $uuid)
  33      {
  34          $sixPieceComponents = array_values($uuid->getFieldsHex());
  35  
  36          $this->swapTimestampAndRandomBits($sixPieceComponents);
  37  
  38          return vsprintf(
  39              '%08s-%04s-%04s-%02s%02s-%012s',
  40              $sixPieceComponents
  41          );
  42      }
  43  
  44      /**
  45       * Encodes a UuidInterface as a binary representation of timestamp first COMB UUID
  46       *
  47       * @param UuidInterface $uuid
  48       *
  49       * @return string Binary string representation of timestamp first COMB UUID
  50       */
  51      public function encodeBinary(UuidInterface $uuid)
  52      {
  53          $stringEncoding = $this->encode($uuid);
  54  
  55          return hex2bin(str_replace('-', '', $stringEncoding));
  56      }
  57  
  58      /**
  59       * Decodes a string representation of timestamp first COMB UUID into a UuidInterface object instance
  60       *
  61       * @param string $encodedUuid
  62       *
  63       * @return UuidInterface
  64       * @throws InvalidUuidStringException
  65       */
  66      public function decode($encodedUuid)
  67      {
  68          $fivePieceComponents = $this->extractComponents($encodedUuid);
  69  
  70          $this->swapTimestampAndRandomBits($fivePieceComponents);
  71  
  72          return $this->getBuilder()->build($this, $this->getFields($fivePieceComponents));
  73      }
  74  
  75      /**
  76       * Decodes a binary representation of timestamp first COMB UUID into a UuidInterface object instance
  77       *
  78       * @param string $bytes
  79       *
  80       * @return UuidInterface
  81       * @throws InvalidUuidStringException
  82       */
  83      public function decodeBytes($bytes)
  84      {
  85          return $this->decode(bin2hex($bytes));
  86      }
  87  
  88      /**
  89       * Swaps the first 48 bits with the last 48 bits
  90       *
  91       * @param array $components An array of UUID components (the UUID exploded on its dashes)
  92       *
  93       * @return void
  94       */
  95      protected function swapTimestampAndRandomBits(array &$components)
  96      {
  97          $last48Bits = $components[4];
  98          if (count($components) == 6) {
  99              $last48Bits = $components[5];
 100              $components[5] = $components[0] . $components[1];
 101          } else {
 102              $components[4] = $components[0] . $components[1];
 103          }
 104  
 105          $components[0] = substr($last48Bits, 0, 8);
 106          $components[1] = substr($last48Bits, 8, 4);
 107      }
 108  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer