| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/uuid library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid; |
| 14 |
|
| 15 |
use DateTimeImmutable; |
| 16 |
use DateTimeInterface; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 18 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\TimeConverterInterface; |
| 19 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\DateTimeException; |
| 20 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 21 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; |
| 22 |
use Throwable; |
| 23 |
use function str_pad; |
| 24 |
use function substr; |
| 25 |
use const STR_PAD_LEFT; |
| 26 |
/** |
| 27 |
* This trait encapsulates deprecated methods for ramsey/uuid; this trait and |
| 28 |
* its methods will be removed in ramsey/uuid 5.0.0. |
| 29 |
* |
| 30 |
* @psalm-immutable |
| 31 |
*/ |
| 32 |
trait DeprecatedUuidMethodsTrait |
| 33 |
{ |
| 34 |
/** |
| 35 |
* @var Rfc4122FieldsInterface |
| 36 |
*/ |
| 37 |
protected $fields; |
| 38 |
/** |
| 39 |
* @var NumberConverterInterface |
| 40 |
*/ |
| 41 |
protected $numberConverter; |
| 42 |
/** |
| 43 |
* @var TimeConverterInterface |
| 44 |
*/ |
| 45 |
protected $timeConverter; |
| 46 |
/** |
| 47 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 48 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 49 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()} |
| 50 |
* and use the arbitrary-precision math library of your choice to |
| 51 |
* convert it to a string integer. |
| 52 |
*/ |
| 53 |
public function getClockSeqHiAndReserved(): string |
| 54 |
{ |
| 55 |
return $this->numberConverter->fromHex($this->fields->getClockSeqHiAndReserved()->toString()); |
| 56 |
} |
| 57 |
/** |
| 58 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 59 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 60 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()}. |
| 61 |
*/ |
| 62 |
public function getClockSeqHiAndReservedHex(): string |
| 63 |
{ |
| 64 |
return $this->fields->getClockSeqHiAndReserved()->toString(); |
| 65 |
} |
| 66 |
/** |
| 67 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 68 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 69 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()} |
| 70 |
* and use the arbitrary-precision math library of your choice to |
| 71 |
* convert it to a string integer. |
| 72 |
*/ |
| 73 |
public function getClockSeqLow(): string |
| 74 |
{ |
| 75 |
return $this->numberConverter->fromHex($this->fields->getClockSeqLow()->toString()); |
| 76 |
} |
| 77 |
/** |
| 78 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 79 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 80 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()}. |
| 81 |
*/ |
| 82 |
public function getClockSeqLowHex(): string |
| 83 |
{ |
| 84 |
return $this->fields->getClockSeqLow()->toString(); |
| 85 |
} |
| 86 |
/** |
| 87 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 88 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 89 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()} |
| 90 |
* and use the arbitrary-precision math library of your choice to |
| 91 |
* convert it to a string integer. |
| 92 |
*/ |
| 93 |
public function getClockSequence(): string |
| 94 |
{ |
| 95 |
return $this->numberConverter->fromHex($this->fields->getClockSeq()->toString()); |
| 96 |
} |
| 97 |
/** |
| 98 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 99 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 100 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()}. |
| 101 |
*/ |
| 102 |
public function getClockSequenceHex(): string |
| 103 |
{ |
| 104 |
return $this->fields->getClockSeq()->toString(); |
| 105 |
} |
| 106 |
/** |
| 107 |
* @deprecated This method will be removed in 5.0.0. There is no alternative |
| 108 |
* recommendation, so plan accordingly. |
| 109 |
*/ |
| 110 |
public function getNumberConverter(): NumberConverterInterface |
| 111 |
{ |
| 112 |
return $this->numberConverter; |
| 113 |
} |
| 114 |
/** |
| 115 |
* @deprecated In ramsey/uuid version 5.0.0, this will be removed. |
| 116 |
* It is available at {@see UuidV1::getDateTime()}. |
| 117 |
* |
| 118 |
* @return DateTimeImmutable An immutable instance of DateTimeInterface |
| 119 |
* |
| 120 |
* @throws UnsupportedOperationException if UUID is not time-based |
| 121 |
* @throws DateTimeException if DateTime throws an exception/error |
| 122 |
*/ |
| 123 |
public function getDateTime(): DateTimeInterface |
| 124 |
{ |
| 125 |
if ($this->fields->getVersion() !== 1) { |
| 126 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 127 |
} |
| 128 |
$time = $this->timeConverter->convertTime($this->fields->getTimestamp()); |
| 129 |
try { |
| 130 |
return new DateTimeImmutable('@' . $time->getSeconds()->toString() . '.' . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)); |
| 131 |
} catch (Throwable $e) { |
| 132 |
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e); |
| 133 |
} |
| 134 |
} |
| 135 |
/** |
| 136 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 137 |
* {@see FieldsInterface} instance. |
| 138 |
* |
| 139 |
* @return string[] |
| 140 |
*/ |
| 141 |
public function getFieldsHex(): array |
| 142 |
{ |
| 143 |
return ['time_low' => $this->fields->getTimeLow()->toString(), 'time_mid' => $this->fields->getTimeMid()->toString(), 'time_hi_and_version' => $this->fields->getTimeHiAndVersion()->toString(), 'clock_seq_hi_and_reserved' => $this->fields->getClockSeqHiAndReserved()->toString(), 'clock_seq_low' => $this->fields->getClockSeqLow()->toString(), 'node' => $this->fields->getNode()->toString()]; |
| 144 |
} |
| 145 |
/** |
| 146 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 147 |
* alternative, but the same information may be obtained by splitting |
| 148 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 149 |
*/ |
| 150 |
public function getLeastSignificantBits(): string |
| 151 |
{ |
| 152 |
$leastSignificantHex = substr($this->getHex()->toString(), 16); |
| 153 |
return $this->numberConverter->fromHex($leastSignificantHex); |
| 154 |
} |
| 155 |
/** |
| 156 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 157 |
* alternative, but the same information may be obtained by splitting |
| 158 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 159 |
*/ |
| 160 |
public function getLeastSignificantBitsHex(): string |
| 161 |
{ |
| 162 |
return substr($this->getHex()->toString(), 16); |
| 163 |
} |
| 164 |
/** |
| 165 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 166 |
* alternative, but the same information may be obtained by splitting |
| 167 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 168 |
*/ |
| 169 |
public function getMostSignificantBits(): string |
| 170 |
{ |
| 171 |
$mostSignificantHex = substr($this->getHex()->toString(), 0, 16); |
| 172 |
return $this->numberConverter->fromHex($mostSignificantHex); |
| 173 |
} |
| 174 |
/** |
| 175 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 176 |
* alternative, but the same information may be obtained by splitting |
| 177 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 178 |
*/ |
| 179 |
public function getMostSignificantBitsHex(): string |
| 180 |
{ |
| 181 |
return substr($this->getHex()->toString(), 0, 16); |
| 182 |
} |
| 183 |
/** |
| 184 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 185 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 186 |
* instance, you may call {@see Rfc4122FieldsInterface::getNode()} |
| 187 |
* and use the arbitrary-precision math library of your choice to |
| 188 |
* convert it to a string integer. |
| 189 |
*/ |
| 190 |
public function getNode(): string |
| 191 |
{ |
| 192 |
return $this->numberConverter->fromHex($this->fields->getNode()->toString()); |
| 193 |
} |
| 194 |
/** |
| 195 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 196 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 197 |
* instance, you may call {@see Rfc4122FieldsInterface::getNode()}. |
| 198 |
*/ |
| 199 |
public function getNodeHex(): string |
| 200 |
{ |
| 201 |
return $this->fields->getNode()->toString(); |
| 202 |
} |
| 203 |
/** |
| 204 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 205 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 206 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()} |
| 207 |
* and use the arbitrary-precision math library of your choice to |
| 208 |
* convert it to a string integer. |
| 209 |
*/ |
| 210 |
public function getTimeHiAndVersion(): string |
| 211 |
{ |
| 212 |
return $this->numberConverter->fromHex($this->fields->getTimeHiAndVersion()->toString()); |
| 213 |
} |
| 214 |
/** |
| 215 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 216 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 217 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()}. |
| 218 |
*/ |
| 219 |
public function getTimeHiAndVersionHex(): string |
| 220 |
{ |
| 221 |
return $this->fields->getTimeHiAndVersion()->toString(); |
| 222 |
} |
| 223 |
/** |
| 224 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 225 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 226 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()} |
| 227 |
* and use the arbitrary-precision math library of your choice to |
| 228 |
* convert it to a string integer. |
| 229 |
*/ |
| 230 |
public function getTimeLow(): string |
| 231 |
{ |
| 232 |
return $this->numberConverter->fromHex($this->fields->getTimeLow()->toString()); |
| 233 |
} |
| 234 |
/** |
| 235 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 236 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 237 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()}. |
| 238 |
*/ |
| 239 |
public function getTimeLowHex(): string |
| 240 |
{ |
| 241 |
return $this->fields->getTimeLow()->toString(); |
| 242 |
} |
| 243 |
/** |
| 244 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 245 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 246 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()} |
| 247 |
* and use the arbitrary-precision math library of your choice to |
| 248 |
* convert it to a string integer. |
| 249 |
*/ |
| 250 |
public function getTimeMid(): string |
| 251 |
{ |
| 252 |
return $this->numberConverter->fromHex($this->fields->getTimeMid()->toString()); |
| 253 |
} |
| 254 |
/** |
| 255 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 256 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 257 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()}. |
| 258 |
*/ |
| 259 |
public function getTimeMidHex(): string |
| 260 |
{ |
| 261 |
return $this->fields->getTimeMid()->toString(); |
| 262 |
} |
| 263 |
/** |
| 264 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 265 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 266 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} |
| 267 |
* and use the arbitrary-precision math library of your choice to |
| 268 |
* convert it to a string integer. |
| 269 |
*/ |
| 270 |
public function getTimestamp(): string |
| 271 |
{ |
| 272 |
if ($this->fields->getVersion() !== 1) { |
| 273 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 274 |
} |
| 275 |
return $this->numberConverter->fromHex($this->fields->getTimestamp()->toString()); |
| 276 |
} |
| 277 |
/** |
| 278 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 279 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 280 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()}. |
| 281 |
*/ |
| 282 |
public function getTimestampHex(): string |
| 283 |
{ |
| 284 |
if ($this->fields->getVersion() !== 1) { |
| 285 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 286 |
} |
| 287 |
return $this->fields->getTimestamp()->toString(); |
| 288 |
} |
| 289 |
/** |
| 290 |
* @deprecated This has moved to {@see Rfc4122FieldsInterface::getUrn()} and |
| 291 |
* is available on {@see \Ramsey\Uuid\Rfc4122\UuidV1}, |
| 292 |
* {@see \Ramsey\Uuid\Rfc4122\UuidV3}, {@see \Ramsey\Uuid\Rfc4122\UuidV4}, |
| 293 |
* and {@see \Ramsey\Uuid\Rfc4122\UuidV5}. |
| 294 |
*/ |
| 295 |
public function getUrn(): string |
| 296 |
{ |
| 297 |
return 'urn:uuid:' . $this->toString(); |
| 298 |
} |
| 299 |
/** |
| 300 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 301 |
* {@see FieldsInterface} instance. If it is a |
| 302 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call |
| 303 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVariant()}. |
| 304 |
*/ |
| 305 |
public function getVariant(): ?int |
| 306 |
{ |
| 307 |
return $this->fields->getVariant(); |
| 308 |
} |
| 309 |
/** |
| 310 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 311 |
* {@see FieldsInterface} instance. If it is a |
| 312 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call |
| 313 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVersion()}. |
| 314 |
*/ |
| 315 |
public function getVersion(): ?int |
| 316 |
{ |
| 317 |
return $this->fields->getVersion(); |
| 318 |
} |
| 319 |
} |
| 320 |
|