nhl-schedule/src/Domain/Entity/AEntity.php

79 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Domain\Entity;
abstract class AEntity
{
protected string $id;
public function __construct()
{
$this->id = $this->generateUUIDv4();
}
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*
* https://github.com/symfony/polyfill-uuid/blob/a41886c1c81dc075a09c71fe6db5b9d68c79de23/Uuid.php#L378-L438
*/
private function generateUUIDv4(): string
{
$timeOffsetInt = 0x01B21DD213814000;
$time = microtime(false);
$time = substr($time, 11) . substr($time, 2, 7);
$time = str_pad(dechex((int) $time + $timeOffsetInt), 16, '0', \STR_PAD_LEFT);
// https://tools.ietf.org/html/rfc4122#section-4.1.5
// We are using a random data for the sake of simplicity: since we are
// not able to get a super precise timeOfDay as a unique sequence
$clockSeq = random_int(0, 0x3FFF);
static $node;
if (null === $node) {
if (\function_exists('apcu_fetch')) {
$node = apcu_fetch('__symfony_uuid_node');
if (false === $node) {
$node = sprintf(
'%06x%06x',
random_int(0, 0xFFFFFF) | 0x010000,
random_int(0, 0xFFFFFF)
);
apcu_store('__symfony_uuid_node', $node);
}
} else {
$node = sprintf(
'%06x%06x',
random_int(0, 0xFFFFFF) | 0x010000,
random_int(0, 0xFFFFFF)
);
}
}
return sprintf(
'%08s-%04s-1%03s-%04x-%012s',
// 32 bits for "time_low"
substr($time, -8),
// 16 bits for "time_mid"
substr($time, -12, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 1
substr($time, -15, 3),
// 16 bits:
// * 8 bits for "clk_seq_hi_res",
// * 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
$clockSeq | 0x8000,
// 48 bits for "node"
$node
);
}
}