| 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 <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Nonstandard; |
| 14 |
|
| 15 |
use DateTimeImmutable; |
| 16 |
use DateTimeInterface; |
| 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\Exception\DateTimeException; |
| 21 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 22 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Lazy\LazyUuidFromString; |
| 23 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; |
| 24 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\UuidInterface; |
| 25 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\UuidV1; |
| 26 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Uuid; |
| 27 |
use Throwable; |
| 28 |
use function hex2bin; |
| 29 |
use function str_pad; |
| 30 |
use function substr; |
| 31 |
use const STR_PAD_LEFT; |
| 32 |
/** |
| 33 |
* Ordered-time, or version 6, UUIDs include timestamp, clock sequence, and node |
| 34 |
* values that are combined into a 128-bit unsigned integer |
| 35 |
* |
| 36 |
* @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft |
| 37 |
* @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs |
| 38 |
* |
| 39 |
* @psalm-immutable |
| 40 |
*/ |
| 41 |
final class UuidV6 extends Uuid implements UuidInterface |
| 42 |
{ |
| 43 |
/** |
| 44 |
* Creates a version 6 (time-based) UUID |
| 45 |
* |
| 46 |
* @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID |
| 47 |
* @param NumberConverterInterface $numberConverter The number converter to use |
| 48 |
* for converting hex values to/from integers |
| 49 |
* @param CodecInterface $codec The codec to use when encoding or decoding |
| 50 |
* UUID strings |
| 51 |
* @param TimeConverterInterface $timeConverter The time converter to use |
| 52 |
* for converting timestamps extracted from a UUID to unix timestamps |
| 53 |
*/ |
| 54 |
public function __construct(Rfc4122FieldsInterface $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, TimeConverterInterface $timeConverter) |
| 55 |
{ |
| 56 |
if ($fields->getVersion() !== Uuid::UUID_TYPE_PEABODY) { |
| 57 |
throw new InvalidArgumentException('Fields used to create a UuidV6 must represent a ' . 'version 6 (ordered-time) UUID'); |
| 58 |
} |
| 59 |
parent::__construct($fields, $numberConverter, $codec, $timeConverter); |
| 60 |
} |
| 61 |
/** |
| 62 |
* Returns a DateTimeInterface object representing the timestamp associated |
| 63 |
* with the UUID |
| 64 |
* |
| 65 |
* @return DateTimeImmutable A PHP DateTimeImmutable instance representing |
| 66 |
* the timestamp of a version 6 UUID |
| 67 |
*/ |
| 68 |
public function getDateTime(): DateTimeInterface |
| 69 |
{ |
| 70 |
$time = $this->timeConverter->convertTime($this->fields->getTimestamp()); |
| 71 |
try { |
| 72 |
return new DateTimeImmutable('@' . $time->getSeconds()->toString() . '.' . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)); |
| 73 |
} catch (Throwable $e) { |
| 74 |
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e); |
| 75 |
} |
| 76 |
} |
| 77 |
/** |
| 78 |
* Converts this UUID into an instance of a version 1 UUID |
| 79 |
*/ |
| 80 |
public function toUuidV1(): UuidV1 |
| 81 |
{ |
| 82 |
$hex = $this->getHex()->toString(); |
| 83 |
$hex = substr($hex, 7, 5) . substr($hex, 13, 3) . substr($hex, 3, 4) . '1' . substr($hex, 0, 3) . substr($hex, 16); |
| 84 |
/** @var LazyUuidFromString $uuid */ |
| 85 |
$uuid = Uuid::fromBytes((string) hex2bin($hex)); |
| 86 |
return $uuid->toUuidV1(); |
| 87 |
} |
| 88 |
/** |
| 89 |
* Converts a version 1 UUID into an instance of a version 6 UUID |
| 90 |
*/ |
| 91 |
public static function fromUuidV1(UuidV1 $uuidV1): UuidV6 |
| 92 |
{ |
| 93 |
$hex = $uuidV1->getHex()->toString(); |
| 94 |
$hex = substr($hex, 13, 3) . substr($hex, 8, 4) . substr($hex, 0, 5) . '6' . substr($hex, 5, 3) . substr($hex, 16); |
| 95 |
/** @var LazyUuidFromString $uuid */ |
| 96 |
$uuid = Uuid::fromBytes((string) hex2bin($hex)); |
| 97 |
return $uuid->toUuidV6(); |
| 98 |
} |
| 99 |
} |
| 100 |
|