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

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