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