| 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; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Converter\NumberConverterInterface; |
| 18 |
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
| 19 |
use Ramsey\Uuid\Provider\NodeProviderInterface; |
| 20 |
use Ramsey\Uuid\Generator\RandomGeneratorInterface; |
| 21 |
use Ramsey\Uuid\Generator\TimeGeneratorInterface; |
| 22 |
use Ramsey\Uuid\Codec\CodecInterface; |
| 23 |
use Ramsey\Uuid\Builder\UuidBuilderInterface; |
| 24 |
|
| 25 |
class UuidFactory implements UuidFactoryInterface |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var CodecInterface |
| 29 |
*/ |
| 30 |
private $codec = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var NodeProviderInterface |
| 34 |
*/ |
| 35 |
private $nodeProvider = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var NumberConverterInterface |
| 39 |
*/ |
| 40 |
private $numberConverter = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var RandomGeneratorInterface |
| 44 |
*/ |
| 45 |
private $randomGenerator = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var TimeGeneratorInterface |
| 49 |
*/ |
| 50 |
private $timeGenerator = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var UuidBuilderInterface |
| 54 |
*/ |
| 55 |
private $uuidBuilder = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Constructs a `UuidFactory` for creating `Ramsey\Uuid\UuidInterface` instances |
| 59 |
* |
| 60 |
* @param FeatureSet $features A set of features for use when creating UUIDs |
| 61 |
*/ |
| 62 |
public function __construct(FeatureSet $features = null) |
| 63 |
{ |
| 64 |
$features = $features ?: new FeatureSet(); |
| 65 |
|
| 66 |
$this->codec = $features->getCodec(); |
| 67 |
$this->nodeProvider = $features->getNodeProvider(); |
| 68 |
$this->numberConverter = $features->getNumberConverter(); |
| 69 |
$this->randomGenerator = $features->getRandomGenerator(); |
| 70 |
$this->timeGenerator = $features->getTimeGenerator(); |
| 71 |
$this->uuidBuilder = $features->getBuilder(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the UUID coder-decoder used by this factory |
| 76 |
* |
| 77 |
* @return CodecInterface |
| 78 |
*/ |
| 79 |
public function getCodec() |
| 80 |
{ |
| 81 |
return $this->codec; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sets the UUID coder-decoder used by this factory |
| 86 |
* |
| 87 |
* @param CodecInterface $codec |
| 88 |
*/ |
| 89 |
public function setCodec(CodecInterface $codec) |
| 90 |
{ |
| 91 |
$this->codec = $codec; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the system node ID provider used by this factory |
| 96 |
* |
| 97 |
* @return NodeProviderInterface |
| 98 |
*/ |
| 99 |
public function getNodeProvider() |
| 100 |
{ |
| 101 |
return $this->nodeProvider; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the random UUID generator used by this factory |
| 106 |
* |
| 107 |
* @return RandomGeneratorInterface |
| 108 |
*/ |
| 109 |
public function getRandomGenerator() |
| 110 |
{ |
| 111 |
return $this->randomGenerator; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns the time-based UUID generator used by this factory |
| 116 |
* |
| 117 |
* @return TimeGeneratorInterface |
| 118 |
*/ |
| 119 |
public function getTimeGenerator() |
| 120 |
{ |
| 121 |
return $this->timeGenerator; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Sets the time-based UUID generator this factory will use to generate version 1 UUIDs |
| 126 |
* |
| 127 |
* @param TimeGeneratorInterface $generator |
| 128 |
*/ |
| 129 |
public function setTimeGenerator(TimeGeneratorInterface $generator) |
| 130 |
{ |
| 131 |
$this->timeGenerator = $generator; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns the number converter used by this factory |
| 136 |
* |
| 137 |
* @return NumberConverterInterface |
| 138 |
*/ |
| 139 |
public function getNumberConverter() |
| 140 |
{ |
| 141 |
return $this->numberConverter; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sets the random UUID generator this factory will use to generate version 4 UUIDs |
| 146 |
* |
| 147 |
* @param RandomGeneratorInterface $generator |
| 148 |
*/ |
| 149 |
public function setRandomGenerator(RandomGeneratorInterface $generator) |
| 150 |
{ |
| 151 |
$this->randomGenerator = $generator; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Sets the number converter this factory will use |
| 156 |
* |
| 157 |
* @param NumberConverterInterface $converter |
| 158 |
*/ |
| 159 |
public function setNumberConverter(NumberConverterInterface $converter) |
| 160 |
{ |
| 161 |
$this->numberConverter = $converter; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns the UUID builder this factory uses when creating `Uuid` instances |
| 166 |
* |
| 167 |
* @return UuidBuilderInterface $builder |
| 168 |
*/ |
| 169 |
public function getUuidBuilder() |
| 170 |
{ |
| 171 |
return $this->uuidBuilder; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Sets the UUID builder this factory will use when creating `Uuid` instances |
| 176 |
* |
| 177 |
* @param UuidBuilderInterface $builder |
| 178 |
*/ |
| 179 |
public function setUuidBuilder(UuidBuilderInterface $builder) |
| 180 |
{ |
| 181 |
$this->uuidBuilder = $builder; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @inheritdoc |
| 186 |
*/ |
| 187 |
public function fromBytes($bytes) |
| 188 |
{ |
| 189 |
return $this->codec->decodeBytes($bytes); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @inheritdoc |
| 194 |
*/ |
| 195 |
public function fromString($uuid) |
| 196 |
{ |
| 197 |
$uuid = strtolower($uuid); |
| 198 |
return $this->codec->decode($uuid); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @inheritdoc |
| 203 |
*/ |
| 204 |
public function fromInteger($integer) |
| 205 |
{ |
| 206 |
$hex = $this->numberConverter->toHex($integer); |
| 207 |
$hex = str_pad($hex, 32, '0', STR_PAD_LEFT); |
| 208 |
|
| 209 |
return $this->fromString($hex); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @inheritdoc |
| 214 |
*/ |
| 215 |
public function uuid1($node = null, $clockSeq = null) |
| 216 |
{ |
| 217 |
$bytes = $this->timeGenerator->generate($node, $clockSeq); |
| 218 |
$hex = bin2hex($bytes); |
| 219 |
|
| 220 |
return $this->uuidFromHashedName($hex, 1); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @inheritdoc |
| 225 |
*/ |
| 226 |
public function uuid3($ns, $name) |
| 227 |
{ |
| 228 |
return $this->uuidFromNsAndName($ns, $name, 3, 'md5'); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @inheritdoc |
| 233 |
*/ |
| 234 |
public function uuid4() |
| 235 |
{ |
| 236 |
$bytes = $this->randomGenerator->generate(16); |
| 237 |
|
| 238 |
// When converting the bytes to hex, it turns into a 32-character |
| 239 |
// hexadecimal string that looks a lot like an MD5 hash, so at this |
| 240 |
// point, we can just pass it to uuidFromHashedName. |
| 241 |
$hex = bin2hex($bytes); |
| 242 |
|
| 243 |
return $this->uuidFromHashedName($hex, 4); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* @inheritdoc |
| 248 |
*/ |
| 249 |
public function uuid5($ns, $name) |
| 250 |
{ |
| 251 |
return $this->uuidFromNsAndName($ns, $name, 5, 'sha1'); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Returns a `Uuid` |
| 256 |
* |
| 257 |
* Uses the configured builder and codec and the provided array of hexadecimal |
| 258 |
* value UUID fields to construct a `Uuid` object. |
| 259 |
* |
| 260 |
* @param array $fields An array of fields from which to construct a UUID; |
| 261 |
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure. |
| 262 |
* @return UuidInterface |
| 263 |
*/ |
| 264 |
public function uuid(array $fields) |
| 265 |
{ |
| 266 |
return $this->uuidBuilder->build($this->codec, $fields); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Returns a version 3 or 5 namespaced `Uuid` |
| 271 |
* |
| 272 |
* @param string|UuidInterface $ns The UUID namespace to use |
| 273 |
* @param string $name The string to hash together with the namespace |
| 274 |
* @param int $version The version of UUID to create (3 or 5) |
| 275 |
* @param string $hashFunction The hash function to use when hashing together |
| 276 |
* the namespace and name |
| 277 |
* @return UuidInterface |
| 278 |
* @throws InvalidUuidStringException |
| 279 |
*/ |
| 280 |
protected function uuidFromNsAndName($ns, $name, $version, $hashFunction) |
| 281 |
{ |
| 282 |
if (!($ns instanceof UuidInterface)) { |
| 283 |
$ns = $this->codec->decode($ns); |
| 284 |
} |
| 285 |
|
| 286 |
$hash = call_user_func($hashFunction, ($ns->getBytes() . $name)); |
| 287 |
|
| 288 |
return $this->uuidFromHashedName($hash, $version); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns a `Uuid` created from `$hash` with the version field set to `$version` |
| 293 |
* and the variant field set for RFC 4122 |
| 294 |
* |
| 295 |
* @param string $hash The hash to use when creating the UUID |
| 296 |
* @param int $version The UUID version to set for this hash (1, 3, 4, or 5) |
| 297 |
* @return UuidInterface |
| 298 |
*/ |
| 299 |
protected function uuidFromHashedName($hash, $version) |
| 300 |
{ |
| 301 |
$timeHi = BinaryUtils::applyVersion(substr($hash, 12, 4), $version); |
| 302 |
$clockSeqHi = BinaryUtils::applyVariant(hexdec(substr($hash, 16, 2))); |
| 303 |
|
| 304 |
$fields = [ |
| 305 |
'time_low' => substr($hash, 0, 8), |
| 306 |
'time_mid' => substr($hash, 8, 4), |
| 307 |
'time_hi_and_version' => str_pad(dechex($timeHi), 4, '0', STR_PAD_LEFT), |
| 308 |
'clock_seq_hi_and_reserved' => str_pad(dechex($clockSeqHi), 2, '0', STR_PAD_LEFT), |
| 309 |
'clock_seq_low' => substr($hash, 18, 2), |
| 310 |
'node' => substr($hash, 20, 12), |
| 311 |
]; |
| 312 |
|
| 313 |
return $this->uuid($fields); |
| 314 |
} |
| 315 |
} |