vendor/ramsey/uuid/src/Codec/StringCodec.php line 28

Open in your IDE?
  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 <ben@benramsey.com>
  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. use InvalidArgumentException;
  16. use Ramsey\Uuid\Builder\UuidBuilderInterface;
  17. use Ramsey\Uuid\Exception\InvalidUuidStringException;
  18. use Ramsey\Uuid\Uuid;
  19. use Ramsey\Uuid\UuidInterface;
  20. /**
  21.  * StringCodec encodes and decodes RFC 4122 UUIDs
  22.  *
  23.  * @link http://tools.ietf.org/html/rfc4122
  24.  */
  25. class StringCodec implements CodecInterface
  26. {
  27.     /**
  28.      * @var UuidBuilderInterface
  29.      */
  30.     private $builder;
  31.     /**
  32.      * Constructs a StringCodec for use encoding and decoding UUIDs
  33.      *
  34.      * @param UuidBuilderInterface $builder The UUID builder to use when encoding UUIDs
  35.      */
  36.     public function __construct(UuidBuilderInterface $builder)
  37.     {
  38.         $this->builder $builder;
  39.     }
  40.     /**
  41.      * Encodes a UuidInterface as a string representation of a UUID
  42.      *
  43.      * @param UuidInterface $uuid
  44.      * @return string Hexadecimal string representation of a UUID
  45.      */
  46.     public function encode(UuidInterface $uuid)
  47.     {
  48.         $fields array_values($uuid->getFieldsHex());
  49.         return vsprintf(
  50.             '%08s-%04s-%04s-%02s%02s-%012s',
  51.             $fields
  52.         );
  53.     }
  54.     /**
  55.      * Encodes a UuidInterface as a binary representation of a UUID
  56.      *
  57.      * @param UuidInterface $uuid
  58.      * @return string Binary string representation of a UUID
  59.      */
  60.     public function encodeBinary(UuidInterface $uuid)
  61.     {
  62.         return hex2bin($uuid->getHex());
  63.     }
  64.     /**
  65.      * Decodes a string representation of a UUID into a UuidInterface object instance
  66.      *
  67.      * @param string $encodedUuid
  68.      * @return UuidInterface
  69.      * @throws InvalidUuidStringException
  70.      */
  71.     public function decode($encodedUuid)
  72.     {
  73.         $components $this->extractComponents($encodedUuid);
  74.         $fields $this->getFields($components);
  75.         return $this->builder->build($this$fields);
  76.     }
  77.     /**
  78.      * Decodes a binary representation of a UUID into a UuidInterface object instance
  79.      *
  80.      * @param string $bytes
  81.      * @return UuidInterface
  82.      * @throws InvalidArgumentException if string has not 16 characters
  83.      */
  84.     public function decodeBytes($bytes)
  85.     {
  86.         if (strlen($bytes) !== 16) {
  87.             throw new InvalidArgumentException('$bytes string should contain 16 characters.');
  88.         }
  89.         $hexUuid unpack('H*'$bytes);
  90.         return $this->decode($hexUuid[1]);
  91.     }
  92.     /**
  93.      * Returns the UUID builder
  94.      *
  95.      * @return UuidBuilderInterface
  96.      */
  97.     protected function getBuilder()
  98.     {
  99.         return $this->builder;
  100.     }
  101.     /**
  102.      * Returns an array of UUID components (the UUID exploded on its dashes)
  103.      *
  104.      * @param string $encodedUuid
  105.      * @return array
  106.      * @throws InvalidUuidStringException
  107.      */
  108.     protected function extractComponents($encodedUuid)
  109.     {
  110.         $nameParsed str_replace([
  111.             'urn:',
  112.             'uuid:',
  113.             '{',
  114.             '}',
  115.             '-'
  116.         ], ''$encodedUuid);
  117.         // We have stripped out the dashes and are breaking up the string using
  118.         // substr(). In this way, we can accept a full hex value that doesn't
  119.         // contain dashes.
  120.         $components = [
  121.             substr($nameParsed08),
  122.             substr($nameParsed84),
  123.             substr($nameParsed124),
  124.             substr($nameParsed164),
  125.             substr($nameParsed20)
  126.         ];
  127.         $nameParsed implode('-'$components);
  128.         if (!Uuid::isValid($nameParsed)) {
  129.             throw new InvalidUuidStringException('Invalid UUID string: ' $encodedUuid);
  130.         }
  131.         return $components;
  132.     }
  133.     /**
  134.      * Returns the fields that make up this UUID
  135.      *
  136.      * @see \Ramsey\Uuid\UuidInterface::getFieldsHex()
  137.      * @param array $components
  138.      * @return array
  139.      */
  140.     protected function getFields(array $components)
  141.     {
  142.         return [
  143.             'time_low' => str_pad($components[0], 8'0'STR_PAD_LEFT),
  144.             'time_mid' => str_pad($components[1], 4'0'STR_PAD_LEFT),
  145.             'time_hi_and_version' => str_pad($components[2], 4'0'STR_PAD_LEFT),
  146.             'clock_seq_hi_and_reserved' => str_pad(substr($components[3], 02), 2'0'STR_PAD_LEFT),
  147.             'clock_seq_low' => str_pad(substr($components[3], 2), 2'0'STR_PAD_LEFT),
  148.             'node' => str_pad($components[4], 12'0'STR_PAD_LEFT)
  149.         ];
  150.     }
  151. }