← All changes
|
includes/sdk/google/ramsey/uuid/src/UuidFactory.php
+143
-145
1.2.10
→
1.4.2
View file →
| @@ -9,27 +9,28 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Uuid; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid; | |
| 14 | 14 | |
| 15 | 15 | use DateTimeInterface; |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Builder\UuidBuilderInterface; | |
| 17 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Codec\CodecInterface; | |
| 18 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; | |
| 19 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\TimeConverterInterface; | |
| 20 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Generator\DceSecurityGeneratorInterface; | |
| 21 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Generator\DefaultTimeGenerator; | |
| 22 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Generator\NameGeneratorInterface; | |
| 23 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Generator\RandomGeneratorInterface; | |
| 24 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Generator\TimeGeneratorInterface; | |
| 25 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Lazy\LazyUuidFromString; | |
| 26 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\NodeProviderInterface; | |
| 27 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\Time\FixedTimeProvider; | |
| 28 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; | |
| 29 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; | |
| 30 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Time; | |
| 31 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Validator\ValidatorInterface; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Builder\UuidBuilderInterface; | |
| 17 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec\CodecInterface; | |
| 18 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface; | |
| 19 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\TimeConverterInterface; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\DceSecurityGeneratorInterface; | |
| 21 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\DefaultTimeGenerator; | |
| 22 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\NameGeneratorInterface; | |
| 23 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\RandomGeneratorInterface; | |
| 24 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\TimeGeneratorInterface; | |
| 25 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator\UnixTimeGenerator; | |
| 26 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Lazy\LazyUuidFromString; | |
| 27 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\NodeProviderInterface; | |
| 28 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\Time\FixedTimeProvider; | |
| 29 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; | |
| 30 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject; | |
| 31 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Time; | |
| 32 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Validator\ValidatorInterface; | |
| 32 | 33 | use function bin2hex; |
| 33 | 34 | use function hex2bin; |
| 34 | 35 | use function pack; |
| 35 | 36 | use function str_pad; |
| @@ -39,53 +40,26 @@ | ||
| 39 | 40 | use function unpack; |
| 40 | 41 | use const STR_PAD_LEFT; |
| 41 | 42 | class UuidFactory implements UuidFactoryInterface |
| 42 | 43 | { |
| 44 | + private CodecInterface $codec; | |
| 45 | + private DceSecurityGeneratorInterface $dceSecurityGenerator; | |
| 46 | + private NameGeneratorInterface $nameGenerator; | |
| 47 | + private NodeProviderInterface $nodeProvider; | |
| 48 | + private NumberConverterInterface $numberConverter; | |
| 49 | + private RandomGeneratorInterface $randomGenerator; | |
| 50 | + private TimeConverterInterface $timeConverter; | |
| 51 | + private TimeGeneratorInterface $timeGenerator; | |
| 52 | + private TimeGeneratorInterface $unixTimeGenerator; | |
| 53 | + private UuidBuilderInterface $uuidBuilder; | |
| 54 | + private ValidatorInterface $validator; | |
| 43 | 55 | /** |
| 44 | - * @var CodecInterface | |
| 56 | + * @var bool whether the feature set was provided from outside, or we can operate under "default" assumptions | |
| 45 | 57 | */ |
| 46 | - private $codec; | |
| 58 | + private bool $isDefaultFeatureSet; | |
| 47 | 59 | /** |
| 48 | - * @var DceSecurityGeneratorInterface | |
| 60 | + * @param FeatureSet | null $features A set of available features in the current environment | |
| 49 | 61 | */ |
| 50 | - private $dceSecurityGenerator; | |
| 51 | - /** | |
| 52 | - * @var NameGeneratorInterface | |
| 53 | - */ | |
| 54 | - private $nameGenerator; | |
| 55 | - /** | |
| 56 | - * @var NodeProviderInterface | |
| 57 | - */ | |
| 58 | - private $nodeProvider; | |
| 59 | - /** | |
| 60 | - * @var NumberConverterInterface | |
| 61 | - */ | |
| 62 | - private $numberConverter; | |
| 63 | - /** | |
| 64 | - * @var RandomGeneratorInterface | |
| 65 | - */ | |
| 66 | - private $randomGenerator; | |
| 67 | - /** | |
| 68 | - * @var TimeConverterInterface | |
| 69 | - */ | |
| 70 | - private $timeConverter; | |
| 71 | - /** | |
| 72 | - * @var TimeGeneratorInterface | |
| 73 | - */ | |
| 74 | - private $timeGenerator; | |
| 75 | - /** | |
| 76 | - * @var UuidBuilderInterface | |
| 77 | - */ | |
| 78 | - private $uuidBuilder; | |
| 79 | - /** | |
| 80 | - * @var ValidatorInterface | |
| 81 | - */ | |
| 82 | - private $validator; | |
| 83 | - /** @var bool whether the feature set was provided from outside, or we can operate under "default" assumptions */ | |
| 84 | - private $isDefaultFeatureSet; | |
| 85 | - /** | |
| 86 | - * @param FeatureSet $features A set of available features in the current environment | |
| 87 | - */ | |
| 88 | 62 | public function __construct(?FeatureSet $features = null) |
| 89 | 63 | { |
| 90 | 64 | $this->isDefaultFeatureSet = $features === null; |
| 91 | 65 | $features = $features ?: new FeatureSet(); |
| @@ -98,13 +72,14 @@ | ||
| 98 | 72 | $this->timeConverter = $features->getTimeConverter(); |
| 99 | 73 | $this->timeGenerator = $features->getTimeGenerator(); |
| 100 | 74 | $this->uuidBuilder = $features->getBuilder(); |
| 101 | 75 | $this->validator = $features->getValidator(); |
| 76 | + $this->unixTimeGenerator = $features->getUnixTimeGenerator(); | |
| 102 | 77 | } |
| 103 | 78 | /** |
| 104 | 79 | * Returns the codec used by this factory |
| 105 | 80 | */ |
| 106 | - public function getCodec(): CodecInterface | |
| 81 | + public function getCodec() : CodecInterface | |
| 107 | 82 | { |
| 108 | 83 | return $this->codec; |
| 109 | 84 | } |
| 110 | 85 | /** |
| @@ -111,9 +86,9 @@ | ||
| 111 | 86 | * Sets the codec to use for this factory |
| 112 | 87 | * |
| 113 | 88 | * @param CodecInterface $codec A UUID encoder-decoder |
| 114 | 89 | */ |
| 115 | - public function setCodec(CodecInterface $codec): void | |
| 90 | + public function setCodec(CodecInterface $codec) : void | |
| 116 | 91 | { |
| 117 | 92 | $this->isDefaultFeatureSet = \false; |
| 118 | 93 | $this->codec = $codec; |
| 119 | 94 | } |
| @@ -119,9 +94,9 @@ | ||
| 119 | 94 | } |
| 120 | 95 | /** |
| 121 | 96 | * Returns the name generator used by this factory |
| 122 | 97 | */ |
| 123 | - public function getNameGenerator(): NameGeneratorInterface | |
| 98 | + public function getNameGenerator() : NameGeneratorInterface | |
| 124 | 99 | { |
| 125 | 100 | return $this->nameGenerator; |
| 126 | 101 | } |
| 127 | 102 | /** |
| @@ -126,12 +101,11 @@ | ||
| 126 | 101 | } |
| 127 | 102 | /** |
| 128 | 103 | * Sets the name generator to use for this factory |
| 129 | 104 | * |
| 130 | - * @param NameGeneratorInterface $nameGenerator A generator to generate | |
| 131 | - * binary data, based on a namespace and name | |
| 105 | + * @param NameGeneratorInterface $nameGenerator A generator to generate binary data, based on a namespace and name | |
| 132 | 106 | */ |
| 133 | - public function setNameGenerator(NameGeneratorInterface $nameGenerator): void | |
| 107 | + public function setNameGenerator(NameGeneratorInterface $nameGenerator) : void | |
| 134 | 108 | { |
| 135 | 109 | $this->isDefaultFeatureSet = \false; |
| 136 | 110 | $this->nameGenerator = $nameGenerator; |
| 137 | 111 | } |
| @@ -137,9 +111,9 @@ | ||
| 137 | 111 | } |
| 138 | 112 | /** |
| 139 | 113 | * Returns the node provider used by this factory |
| 140 | 114 | */ |
| 141 | - public function getNodeProvider(): NodeProviderInterface | |
| 115 | + public function getNodeProvider() : NodeProviderInterface | |
| 142 | 116 | { |
| 143 | 117 | return $this->nodeProvider; |
| 144 | 118 | } |
| 145 | 119 | /** |
| @@ -144,9 +118,9 @@ | ||
| 144 | 118 | } |
| 145 | 119 | /** |
| 146 | 120 | * Returns the random generator used by this factory |
| 147 | 121 | */ |
| 148 | - public function getRandomGenerator(): RandomGeneratorInterface | |
| 122 | + public function getRandomGenerator() : RandomGeneratorInterface | |
| 149 | 123 | { |
| 150 | 124 | return $this->randomGenerator; |
| 151 | 125 | } |
| 152 | 126 | /** |
| @@ -151,9 +125,9 @@ | ||
| 151 | 125 | } |
| 152 | 126 | /** |
| 153 | 127 | * Returns the time generator used by this factory |
| 154 | 128 | */ |
| 155 | - public function getTimeGenerator(): TimeGeneratorInterface | |
| 129 | + public function getTimeGenerator() : TimeGeneratorInterface | |
| 156 | 130 | { |
| 157 | 131 | return $this->timeGenerator; |
| 158 | 132 | } |
| 159 | 133 | /** |
| @@ -158,12 +132,11 @@ | ||
| 158 | 132 | } |
| 159 | 133 | /** |
| 160 | 134 | * Sets the time generator to use for this factory |
| 161 | 135 | * |
| 162 | - * @param TimeGeneratorInterface $generator A generator to generate binary | |
| 163 | - * data, based on the time | |
| 136 | + * @param TimeGeneratorInterface $generator A generator to generate binary data, based on the time | |
| 164 | 137 | */ |
| 165 | - public function setTimeGenerator(TimeGeneratorInterface $generator): void | |
| 138 | + public function setTimeGenerator(TimeGeneratorInterface $generator) : void | |
| 166 | 139 | { |
| 167 | 140 | $this->isDefaultFeatureSet = \false; |
| 168 | 141 | $this->timeGenerator = $generator; |
| 169 | 142 | } |
| @@ -169,9 +142,9 @@ | ||
| 169 | 142 | } |
| 170 | 143 | /** |
| 171 | 144 | * Returns the DCE Security generator used by this factory |
| 172 | 145 | */ |
| 173 | - public function getDceSecurityGenerator(): DceSecurityGeneratorInterface | |
| 146 | + public function getDceSecurityGenerator() : DceSecurityGeneratorInterface | |
| 174 | 147 | { |
| 175 | 148 | return $this->dceSecurityGenerator; |
| 176 | 149 | } |
| 177 | 150 | /** |
| @@ -176,12 +149,12 @@ | ||
| 176 | 149 | } |
| 177 | 150 | /** |
| 178 | 151 | * Sets the DCE Security generator to use for this factory |
| 179 | 152 | * |
| 180 | - * @param DceSecurityGeneratorInterface $generator A generator to generate | |
| 181 | - * binary data, based on a local domain and local identifier | |
| 153 | + * @param DceSecurityGeneratorInterface $generator A generator to generate binary data, based on a local domain and | |
| 154 | + * local identifier | |
| 182 | 155 | */ |
| 183 | - public function setDceSecurityGenerator(DceSecurityGeneratorInterface $generator): void | |
| 156 | + public function setDceSecurityGenerator(DceSecurityGeneratorInterface $generator) : void | |
| 184 | 157 | { |
| 185 | 158 | $this->isDefaultFeatureSet = \false; |
| 186 | 159 | $this->dceSecurityGenerator = $generator; |
| 187 | 160 | } |
| @@ -187,9 +160,9 @@ | ||
| 187 | 160 | } |
| 188 | 161 | /** |
| 189 | 162 | * Returns the number converter used by this factory |
| 190 | 163 | */ |
| 191 | - public function getNumberConverter(): NumberConverterInterface | |
| 164 | + public function getNumberConverter() : NumberConverterInterface | |
| 192 | 165 | { |
| 193 | 166 | return $this->numberConverter; |
| 194 | 167 | } |
| 195 | 168 | /** |
| @@ -194,12 +167,11 @@ | ||
| 194 | 167 | } |
| 195 | 168 | /** |
| 196 | 169 | * Sets the random generator to use for this factory |
| 197 | 170 | * |
| 198 | - * @param RandomGeneratorInterface $generator A generator to generate binary | |
| 199 | - * data, based on some random input | |
| 171 | + * @param RandomGeneratorInterface $generator A generator to generate binary data, based on some random input | |
| 200 | 172 | */ |
| 201 | - public function setRandomGenerator(RandomGeneratorInterface $generator): void | |
| 173 | + public function setRandomGenerator(RandomGeneratorInterface $generator) : void | |
| 202 | 174 | { |
| 203 | 175 | $this->isDefaultFeatureSet = \false; |
| 204 | 176 | $this->randomGenerator = $generator; |
| 205 | 177 | } |
| @@ -205,12 +177,12 @@ | ||
| 205 | 177 | } |
| 206 | 178 | /** |
| 207 | 179 | * Sets the number converter to use for this factory |
| 208 | 180 | * |
| 209 | - * @param NumberConverterInterface $converter A converter to use for working | |
| 210 | - * with large integers (i.e. integers greater than PHP_INT_MAX) | |
| 181 | + * @param NumberConverterInterface $converter A converter to use for working with large integers (i.e., integers | |
| 182 | + * greater than PHP_INT_MAX) | |
| 211 | 183 | */ |
| 212 | - public function setNumberConverter(NumberConverterInterface $converter): void | |
| 184 | + public function setNumberConverter(NumberConverterInterface $converter) : void | |
| 213 | 185 | { |
| 214 | 186 | $this->isDefaultFeatureSet = \false; |
| 215 | 187 | $this->numberConverter = $converter; |
| 216 | 188 | } |
| @@ -216,9 +188,9 @@ | ||
| 216 | 188 | } |
| 217 | 189 | /** |
| 218 | 190 | * Returns the UUID builder used by this factory |
| 219 | 191 | */ |
| 220 | - public function getUuidBuilder(): UuidBuilderInterface | |
| 192 | + public function getUuidBuilder() : UuidBuilderInterface | |
| 221 | 193 | { |
| 222 | 194 | return $this->uuidBuilder; |
| 223 | 195 | } |
| 224 | 196 | /** |
| @@ -223,20 +195,16 @@ | ||
| 223 | 195 | } |
| 224 | 196 | /** |
| 225 | 197 | * Sets the UUID builder to use for this factory |
| 226 | 198 | * |
| 227 | - * @param UuidBuilderInterface $builder A builder for constructing instances | |
| 228 | - * of UuidInterface | |
| 199 | + * @param UuidBuilderInterface $builder A builder for constructing instances of UuidInterface | |
| 229 | 200 | */ |
| 230 | - public function setUuidBuilder(UuidBuilderInterface $builder): void | |
| 201 | + public function setUuidBuilder(UuidBuilderInterface $builder) : void | |
| 231 | 202 | { |
| 232 | 203 | $this->isDefaultFeatureSet = \false; |
| 233 | 204 | $this->uuidBuilder = $builder; |
| 234 | 205 | } |
| 235 | - /** | |
| 236 | - * @psalm-mutation-free | |
| 237 | - */ | |
| 238 | - public function getValidator(): ValidatorInterface | |
| 206 | + public function getValidator() : ValidatorInterface | |
| 239 | 207 | { |
| 240 | 208 | return $this->validator; |
| 241 | 209 | } |
| 242 | 210 | /** |
| @@ -241,110 +209,144 @@ | ||
| 241 | 209 | } |
| 242 | 210 | /** |
| 243 | 211 | * Sets the validator to use for this factory |
| 244 | 212 | * |
| 245 | - * @param ValidatorInterface $validator A validator to use for validating | |
| 246 | - * whether a string is a valid UUID | |
| 213 | + * @param ValidatorInterface $validator A validator to use for validating whether a string is a valid UUID | |
| 247 | 214 | */ |
| 248 | - public function setValidator(ValidatorInterface $validator): void | |
| 215 | + public function setValidator(ValidatorInterface $validator) : void | |
| 249 | 216 | { |
| 250 | 217 | $this->isDefaultFeatureSet = \false; |
| 251 | 218 | $this->validator = $validator; |
| 252 | 219 | } |
| 253 | 220 | /** |
| 254 | - * @psalm-pure | |
| 221 | + * @pure | |
| 255 | 222 | */ |
| 256 | - public function fromBytes(string $bytes): UuidInterface | |
| 223 | + public function fromBytes(string $bytes) : UuidInterface | |
| 257 | 224 | { |
| 258 | 225 | return $this->codec->decodeBytes($bytes); |
| 259 | 226 | } |
| 260 | 227 | /** |
| 261 | - * @psalm-pure | |
| 228 | + * @pure | |
| 262 | 229 | */ |
| 263 | - public function fromString(string $uuid): UuidInterface | |
| 230 | + public function fromString(string $uuid) : UuidInterface | |
| 264 | 231 | { |
| 265 | 232 | $uuid = strtolower($uuid); |
| 266 | 233 | return $this->codec->decode($uuid); |
| 267 | 234 | } |
| 268 | 235 | /** |
| 269 | - * @psalm-pure | |
| 236 | + * @pure | |
| 270 | 237 | */ |
| 271 | - public function fromInteger(string $integer): UuidInterface | |
| 238 | + public function fromInteger(string $integer) : UuidInterface | |
| 272 | 239 | { |
| 273 | 240 | $hex = $this->numberConverter->toHex($integer); |
| 274 | 241 | $hex = str_pad($hex, 32, '0', STR_PAD_LEFT); |
| 275 | 242 | return $this->fromString($hex); |
| 276 | 243 | } |
| 277 | - public function fromDateTime(DateTimeInterface $dateTime, ?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface | |
| 244 | + public function fromDateTime(DateTimeInterface $dateTime, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface | |
| 278 | 245 | { |
| 279 | 246 | $timeProvider = new FixedTimeProvider(new Time($dateTime->format('U'), $dateTime->format('u'))); |
| 280 | 247 | $timeGenerator = new DefaultTimeGenerator($this->nodeProvider, $this->timeConverter, $timeProvider); |
| 281 | - $nodeHex = $node ? $node->toString() : null; | |
| 282 | - $bytes = $timeGenerator->generate($nodeHex, $clockSeq); | |
| 283 | - return $this->uuidFromBytesAndVersion($bytes, 1); | |
| 248 | + $bytes = $timeGenerator->generate($node?->toString(), $clockSeq); | |
| 249 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_TIME); | |
| 284 | 250 | } |
| 285 | 251 | /** |
| 252 | + * @pure | |
| 253 | + */ | |
| 254 | + public function fromHexadecimal(Hexadecimal $hex) : UuidInterface | |
| 255 | + { | |
| 256 | + return $this->codec->decode($hex->__toString()); | |
| 257 | + } | |
| 258 | + /** | |
| 286 | 259 | * @inheritDoc |
| 287 | 260 | */ |
| 288 | - public function uuid1($node = null, ?int $clockSeq = null): UuidInterface | |
| 261 | + public function uuid1($node = null, ?int $clockSeq = null) : UuidInterface | |
| 289 | 262 | { |
| 290 | 263 | $bytes = $this->timeGenerator->generate($node, $clockSeq); |
| 291 | - return $this->uuidFromBytesAndVersion($bytes, 1); | |
| 264 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_TIME); | |
| 292 | 265 | } |
| 293 | - public function uuid2(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface | |
| 266 | + public function uuid2(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface | |
| 294 | 267 | { |
| 295 | 268 | $bytes = $this->dceSecurityGenerator->generate($localDomain, $localIdentifier, $node, $clockSeq); |
| 296 | - return $this->uuidFromBytesAndVersion($bytes, 2); | |
| 269 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_DCE_SECURITY); | |
| 297 | 270 | } |
| 298 | 271 | /** |
| 299 | 272 | * @inheritDoc |
| 300 | - * @psalm-pure | |
| 273 | + * @pure | |
| 301 | 274 | */ |
| 302 | - public function uuid3($ns, string $name): UuidInterface | |
| 275 | + public function uuid3($ns, string $name) : UuidInterface | |
| 303 | 276 | { |
| 304 | - return $this->uuidFromNsAndName($ns, $name, 3, 'md5'); | |
| 277 | + return $this->uuidFromNsAndName($ns, $name, Uuid::UUID_TYPE_HASH_MD5, 'md5'); | |
| 305 | 278 | } |
| 306 | - public function uuid4(): UuidInterface | |
| 279 | + public function uuid4() : UuidInterface | |
| 307 | 280 | { |
| 308 | 281 | $bytes = $this->randomGenerator->generate(16); |
| 309 | - return $this->uuidFromBytesAndVersion($bytes, 4); | |
| 282 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_RANDOM); | |
| 310 | 283 | } |
| 311 | 284 | /** |
| 312 | 285 | * @inheritDoc |
| 313 | - * @psalm-pure | |
| 286 | + * @pure | |
| 314 | 287 | */ |
| 315 | - public function uuid5($ns, string $name): UuidInterface | |
| 288 | + public function uuid5($ns, string $name) : UuidInterface | |
| 316 | 289 | { |
| 317 | - return $this->uuidFromNsAndName($ns, $name, 5, 'sha1'); | |
| 290 | + return $this->uuidFromNsAndName($ns, $name, Uuid::UUID_TYPE_HASH_SHA1, 'sha1'); | |
| 318 | 291 | } |
| 319 | - public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface | |
| 292 | + public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface | |
| 320 | 293 | { |
| 321 | - $nodeHex = $node ? $node->toString() : null; | |
| 322 | - $bytes = $this->timeGenerator->generate($nodeHex, $clockSeq); | |
| 294 | + $bytes = $this->timeGenerator->generate($node?->toString(), $clockSeq); | |
| 323 | 295 | // Rearrange the bytes, according to the UUID version 6 specification. |
| 324 | 296 | $v6 = $bytes[6] . $bytes[7] . $bytes[4] . $bytes[5] . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]; |
| 325 | 297 | $v6 = bin2hex($v6); |
| 326 | - // Drop the first four bits, while adding an empty four bits for the | |
| 327 | - // version field. This allows us to reconstruct the correct time from | |
| 328 | - // the bytes of this UUID. | |
| 298 | + // Drop the first four bits, while adding an empty four bits for the version field. This allows us to | |
| 299 | + // reconstruct the correct time from the bytes of this UUID. | |
| 329 | 300 | $v6Bytes = hex2bin(substr($v6, 1, 12) . '0' . substr($v6, -3)); |
| 330 | 301 | $v6Bytes .= substr($bytes, 8); |
| 331 | - return $this->uuidFromBytesAndVersion($v6Bytes, 6); | |
| 302 | + return $this->uuidFromBytesAndVersion($v6Bytes, Uuid::UUID_TYPE_REORDERED_TIME); | |
| 332 | 303 | } |
| 333 | 304 | /** |
| 305 | + * Returns a version 7 (Unix Epoch time) UUID | |
| 306 | + * | |
| 307 | + * @param DateTimeInterface | null $dateTime An optional date/time from which to create the version 7 UUID. If not | |
| 308 | + * provided, the UUID is generated using the current date/time. | |
| 309 | + * | |
| 310 | + * @return UuidInterface A UuidInterface instance that represents a version 7 UUID | |
| 311 | + */ | |
| 312 | + public function uuid7(?DateTimeInterface $dateTime = null) : UuidInterface | |
| 313 | + { | |
| 314 | + \assert($this->unixTimeGenerator instanceof UnixTimeGenerator); | |
| 315 | + $bytes = $this->unixTimeGenerator->generate(null, null, $dateTime); | |
| 316 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_UNIX_TIME); | |
| 317 | + } | |
| 318 | + /** | |
| 319 | + * Returns a version 8 (custom format) UUID | |
| 320 | + * | |
| 321 | + * The bytes provided may contain any value according to your application's needs. Be aware, however, that other | |
| 322 | + * applications may not understand the semantics of the value. | |
| 323 | + * | |
| 324 | + * @param string $bytes A 16-byte octet string. This is an open blob of data that you may fill with 128 bits of | |
| 325 | + * information. Be aware, however, bits 48 through 51 will be replaced with the UUID version field, and bits 64 | |
| 326 | + * and 65 will be replaced with the UUID variant. You MUST NOT rely on these bits for your application needs. | |
| 327 | + * | |
| 328 | + * @return UuidInterface A UuidInterface instance that represents a version 8 UUID | |
| 329 | + * | |
| 330 | + * @pure | |
| 331 | + */ | |
| 332 | + public function uuid8(string $bytes) : UuidInterface | |
| 333 | + { | |
| 334 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 335 | + return $this->uuidFromBytesAndVersion($bytes, Uuid::UUID_TYPE_CUSTOM); | |
| 336 | + } | |
| 337 | + /** | |
| 334 | 338 | * Returns a Uuid created from the provided byte string |
| 335 | 339 | * |
| 336 | - * Uses the configured builder and codec and the provided byte string to | |
| 337 | - * construct a Uuid object. | |
| 340 | + * Uses the configured builder and codec and the provided byte string to construct a Uuid object. | |
| 338 | 341 | * |
| 339 | 342 | * @param string $bytes The byte string from which to construct a UUID |
| 340 | 343 | * |
| 341 | - * @return UuidInterface An instance of UuidInterface, created from the | |
| 342 | - * provided bytes | |
| 344 | + * @return UuidInterface An instance of UuidInterface, created from the provided bytes | |
| 343 | 345 | * |
| 344 | - * @psalm-pure | |
| 346 | + * @pure | |
| 345 | 347 | */ |
| 346 | - public function uuid(string $bytes): UuidInterface | |
| 348 | + public function uuid(string $bytes) : UuidInterface | |
| 347 | 349 | { |
| 348 | 350 | return $this->uuidBuilder->build($this->codec, $bytes); |
| 349 | 351 | } |
| 350 | 352 | /** |
| @@ -349,47 +351,43 @@ | ||
| 349 | 351 | } |
| 350 | 352 | /** |
| 351 | 353 | * Returns a version 3 or 5 namespaced Uuid |
| 352 | 354 | * |
| 353 | - * @param string|UuidInterface $ns The namespace (must be a valid UUID) | |
| 355 | + * @param UuidInterface | string $ns The namespace (must be a valid UUID) | |
| 354 | 356 | * @param string $name The name to hash together with the namespace |
| 355 | 357 | * @param int $version The version of UUID to create (3 or 5) |
| 356 | - * @param string $hashAlgorithm The hashing algorithm to use when hashing | |
| 357 | - * together the namespace and name | |
| 358 | + * @param string $hashAlgorithm The hashing algorithm to use when hashing together the namespace and name | |
| 358 | 359 | * |
| 359 | - * @return UuidInterface An instance of UuidInterface, created by hashing | |
| 360 | - * together the provided namespace and name | |
| 360 | + * @return UuidInterface An instance of UuidInterface, created by hashing together the provided namespace and name | |
| 361 | 361 | * |
| 362 | - * @psalm-pure | |
| 362 | + * @pure | |
| 363 | 363 | */ |
| 364 | - private function uuidFromNsAndName($ns, string $name, int $version, string $hashAlgorithm): UuidInterface | |
| 364 | + private function uuidFromNsAndName(UuidInterface|string $ns, string $name, int $version, string $hashAlgorithm) : UuidInterface | |
| 365 | 365 | { |
| 366 | 366 | if (!$ns instanceof UuidInterface) { |
| 367 | 367 | $ns = $this->fromString($ns); |
| 368 | 368 | } |
| 369 | 369 | $bytes = $this->nameGenerator->generate($ns, $name, $hashAlgorithm); |
| 370 | + /** @phpstan-ignore possiblyImpure.methodCall */ | |
| 370 | 371 | return $this->uuidFromBytesAndVersion(substr($bytes, 0, 16), $version); |
| 371 | 372 | } |
| 372 | 373 | /** |
| 373 | - * Returns an RFC 4122 variant Uuid, created from the provided bytes and version | |
| 374 | + * Returns a Uuid created from the provided bytes and version | |
| 374 | 375 | * |
| 375 | 376 | * @param string $bytes The byte string to convert to a UUID |
| 376 | - * @param int $version The RFC 4122 version to apply to the UUID | |
| 377 | + * @param int $version The version to apply to the UUID | |
| 377 | 378 | * |
| 378 | - * @return UuidInterface An instance of UuidInterface, created from the | |
| 379 | - * byte string and version | |
| 380 | - * | |
| 381 | - * @psalm-pure | |
| 379 | + * @return UuidInterface An instance of UuidInterface, created from the byte string and version | |
| 382 | 380 | */ |
| 383 | - private function uuidFromBytesAndVersion(string $bytes, int $version): UuidInterface | |
| 381 | + private function uuidFromBytesAndVersion(string $bytes, int $version) : UuidInterface | |
| 384 | 382 | { |
| 385 | - /** @var array $unpackedTime */ | |
| 383 | + /** @var int[] $unpackedTime */ | |
| 386 | 384 | $unpackedTime = unpack('n*', substr($bytes, 6, 2)); |
| 387 | - $timeHi = (int) $unpackedTime[1]; | |
| 385 | + $timeHi = $unpackedTime[1]; | |
| 388 | 386 | $timeHiAndVersion = pack('n*', BinaryUtils::applyVersion($timeHi, $version)); |
| 389 | - /** @var array $unpackedClockSeq */ | |
| 387 | + /** @var int[] $unpackedClockSeq */ | |
| 390 | 388 | $unpackedClockSeq = unpack('n*', substr($bytes, 8, 2)); |
| 391 | - $clockSeqHi = (int) $unpackedClockSeq[1]; | |
| 389 | + $clockSeqHi = $unpackedClockSeq[1]; | |
| 392 | 390 | $clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi)); |
| 393 | 391 | $bytes = substr_replace($bytes, $timeHiAndVersion, 6, 2); |
| 394 | 392 | $bytes = substr_replace($bytes, $clockSeqHiAndReserved, 8, 2); |
| 395 | 393 | if ($this->isDefaultFeatureSet) { |