/vendor/ramsey/uuid/src/UuidFactory.php

https://gitlab.com/madwanz64/laravel · PHP · 315 lines · 133 code · 39 blank · 143 comment · 1 complexity · 3843f8353d8cfceaeb17d541a215d879 MD5 · raw file

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