| 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\Rfc4122; |
| 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\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; |
| 23 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Uuid; |
| 24 |
use Throwable; |
| 25 |
use function str_pad; |
| 26 |
use const STR_PAD_LEFT; |
| 27 |
/** |
| 28 |
* Time-based, or version 1, UUIDs include timestamp, clock sequence, and node |
| 29 |
* values that are combined into a 128-bit unsigned integer |
| 30 |
* |
| 31 |
* @psalm-immutable |
| 32 |
*/ |
| 33 |
final class UuidV1 extends Uuid implements UuidInterface |
| 34 |
{ |
| 35 |
/** |
| 36 |
* Creates a version 1 (time-based) UUID |
| 37 |
* |
| 38 |
* @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID |
| 39 |
* @param NumberConverterInterface $numberConverter The number converter to use |
| 40 |
* for converting hex values to/from integers |
| 41 |
* @param CodecInterface $codec The codec to use when encoding or decoding |
| 42 |
* UUID strings |
| 43 |
* @param TimeConverterInterface $timeConverter The time converter to use |
| 44 |
* for converting timestamps extracted from a UUID to unix timestamps |
| 45 |
*/ |
| 46 |
public function __construct(Rfc4122FieldsInterface $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, TimeConverterInterface $timeConverter) |
| 47 |
{ |
| 48 |
if ($fields->getVersion() !== Uuid::UUID_TYPE_TIME) { |
| 49 |
throw new InvalidArgumentException('Fields used to create a UuidV1 must represent a ' . 'version 1 (time-based) UUID'); |
| 50 |
} |
| 51 |
parent::__construct($fields, $numberConverter, $codec, $timeConverter); |
| 52 |
} |
| 53 |
/** |
| 54 |
* Returns a DateTimeInterface object representing the timestamp associated |
| 55 |
* with the UUID |
| 56 |
* |
| 57 |
* The timestamp value is only meaningful in a time-based UUID, which |
| 58 |
* has version type 1. |
| 59 |
* |
| 60 |
* @return DateTimeImmutable A PHP DateTimeImmutable instance representing |
| 61 |
* the timestamp of a version 1 UUID |
| 62 |
*/ |
| 63 |
public function getDateTime(): DateTimeInterface |
| 64 |
{ |
| 65 |
$time = $this->timeConverter->convertTime($this->fields->getTimestamp()); |
| 66 |
try { |
| 67 |
return new DateTimeImmutable('@' . $time->getSeconds()->toString() . '.' . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)); |
| 68 |
} catch (Throwable $e) { |
| 69 |
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|