[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/ramsey/uuid/src/Codec/ -> GuidStringCodec.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  
  15  namespace Ramsey\Uuid\Codec;
  16  
  17  use Ramsey\Uuid\Exception\InvalidUuidStringException;
  18  use Ramsey\Uuid\UuidInterface;
  19  
  20  /**
  21   * GuidStringCodec encodes and decodes globally unique identifiers (GUID)
  22   *
  23   * @link https://en.wikipedia.org/wiki/Globally_unique_identifier
  24   */
  25  class GuidStringCodec extends StringCodec
  26  {
  27      /**
  28       * Encodes a UuidInterface as a string representation of a GUID
  29       *
  30       * @param UuidInterface $uuid
  31       * @return string Hexadecimal string representation of a GUID
  32       */
  33      public function encode(UuidInterface $uuid)
  34      {
  35          $components = array_values($uuid->getFieldsHex());
  36  
  37          // Swap byte-order on the first three fields
  38          $this->swapFields($components);
  39  
  40          return vsprintf(
  41              '%08s-%04s-%04s-%02s%02s-%012s',
  42              $components
  43          );
  44      }
  45  
  46      /**
  47       * Encodes a UuidInterface as a binary representation of a GUID
  48       *
  49       * @param UuidInterface $uuid
  50       * @return string Binary string representation of a GUID
  51       */
  52      public function encodeBinary(UuidInterface $uuid)
  53      {
  54          $components = array_values($uuid->getFieldsHex());
  55  
  56          return hex2bin(implode('', $components));
  57      }
  58  
  59      /**
  60       * Decodes a string representation of a GUID into a UuidInterface object instance
  61       *
  62       * @param string $encodedUuid
  63       * @return UuidInterface
  64       * @throws InvalidUuidStringException
  65       */
  66      public function decode($encodedUuid)
  67      {
  68          $components = $this->extractComponents($encodedUuid);
  69  
  70          $this->swapFields($components);
  71  
  72          return $this->getBuilder()->build($this, $this->getFields($components));
  73      }
  74  
  75      /**
  76       * Decodes a binary representation of a GUID into a UuidInterface object instance
  77       *
  78       * @param string $bytes
  79       * @return UuidInterface
  80       * @throws InvalidUuidStringException
  81       */
  82      public function decodeBytes($bytes)
  83      {
  84          // Specifically call parent::decode to preserve correct byte order
  85          return parent::decode(bin2hex($bytes));
  86      }
  87  
  88      /**
  89       * Swaps fields to support GUID byte order
  90       *
  91       * @param array $components An array of UUID components (the UUID exploded on its dashes)
  92       * @return void
  93       */
  94      protected function swapFields(array &$components)
  95      {
  96          $hex = unpack('H*', pack('L', hexdec($components[0])));
  97          $components[0] = $hex[1];
  98          $hex = unpack('H*', pack('S', hexdec($components[1])));
  99          $components[1] = $hex[1];
 100          $hex = unpack('H*', pack('S', hexdec($components[2])));
 101          $components[2] = $hex[1];
 102      }
 103  }


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