[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/ramsey/uuid/src/Codec/ -> OrderedTimeCodec.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 InvalidArgumentException;
  17  use Ramsey\Uuid\UuidInterface;
  18  
  19  /**
  20   * OrderedTimeCodec optimizes the bytes to increment UUIDs when time goes by, to improve database INSERTs.
  21   * The string value will be unchanged from StringCodec. Only works for UUID type 1.
  22   */
  23  class OrderedTimeCodec extends StringCodec
  24  {
  25  
  26      /**
  27       * Encodes a UuidInterface as an optimized binary representation of a UUID
  28       *
  29       * @param UuidInterface $uuid
  30       * @return string Binary string representation of a UUID
  31       */
  32      public function encodeBinary(UuidInterface $uuid)
  33      {
  34          $fields = $uuid->getFieldsHex();
  35  
  36          $optimized = [
  37              $fields['time_hi_and_version'],
  38              $fields['time_mid'],
  39              $fields['time_low'],
  40              $fields['clock_seq_hi_and_reserved'],
  41              $fields['clock_seq_low'],
  42              $fields['node'],
  43          ];
  44  
  45          return hex2bin(implode('', $optimized));
  46      }
  47  
  48      /**
  49       * Decodes an optimized binary representation of a UUID into a UuidInterface object instance
  50       *
  51       * @param string $bytes
  52       * @return UuidInterface
  53       * @throws InvalidArgumentException if string has not 16 characters
  54       */
  55      public function decodeBytes($bytes)
  56      {
  57          if (strlen($bytes) !== 16) {
  58              throw new InvalidArgumentException('$bytes string should contain 16 characters.');
  59          }
  60  
  61          $hex = unpack('H*', $bytes)[1];
  62  
  63          // Rearrange the fields to their original order
  64          $hex = substr($hex, 8, 4) . substr($hex, 12, 4) . substr($hex, 4, 4) . substr($hex, 0, 4) . substr($hex, 16);
  65  
  66          return $this->decode($hex);
  67      }
  68  }


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