- Implement automated synchronization of employees via Arcadia API (EmployeeSyncService, SynchronizeEmployeesWithArcadiaApi, FetchAndUpsertArcadiaEmployees) - Add Back-Office application settings for employee sites (Setting singleton, GetSettingsQuery, UpdateSettings, SettingsController, EmployeeSitesInputComponent.vue) - Strongly type Employee attributes using Domain Value Objects (ShopperEstablishmentCode, ShopperSiteCode, ShopperServiceCode) and Eloquent Casts - Migrate pagination from Domain to Shared\Pagination namespace across queries and inputs - Remove legacy and dead CSV employee import/export code - Restore Domain\Ports\Services\CsvGenerator interface for Regate order generation - Add comprehensive Pest unit, integration, and feature test coverage
84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shared\Helpers;
|
|
|
|
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
use Shared\Exceptions\InvalidUuidException;
|
|
|
|
final readonly class UuidHelper
|
|
{
|
|
private function __construct()
|
|
{
|
|
// Static class
|
|
}
|
|
|
|
public static function isV4(UuidInterface $uuid): bool
|
|
{
|
|
return self::version($uuid) === 4;
|
|
}
|
|
|
|
public static function isV7(UuidInterface $uuid): bool
|
|
{
|
|
return self::version($uuid) === 7;
|
|
}
|
|
|
|
public static function version(UuidInterface $uuid): int
|
|
{
|
|
$fields = $uuid->getFields();
|
|
|
|
if ($fields instanceof Rfc4122FieldsInterface) {
|
|
$version = $fields->getVersion();
|
|
|
|
if ($version !== null) {
|
|
return $version;
|
|
}
|
|
}
|
|
|
|
$uuidString = $uuid->toString();
|
|
|
|
if (strlen($uuidString) >= 15) {
|
|
return (int) hexdec($uuidString[14]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function validateUuid(UuidInterface $uuid): void
|
|
{
|
|
if (! Uuid::isValid($uuid->toString())) {
|
|
throw InvalidUuidException::forValue($uuid->toString());
|
|
}
|
|
}
|
|
|
|
public static function toDateTime(UuidInterface $uuid): \DateTimeImmutable
|
|
{
|
|
if (! self::isV7($uuid)) {
|
|
throw new InvalidUuidException("Cannot extract datetime from non-V7 UUID: {$uuid->toString()}");
|
|
}
|
|
|
|
$fields = $uuid->getFields();
|
|
|
|
if ($fields instanceof Rfc4122FieldsInterface) {
|
|
$timeMsHex = $fields->getTimeLow()->toString() . $fields->getTimeMid()->toString();
|
|
} else {
|
|
$hex = str_replace('-', '', $uuid->toString());
|
|
$timeMsHex = substr($hex, 0, 12);
|
|
}
|
|
|
|
$timeMs = (int) hexdec($timeMsHex);
|
|
$sec = intdiv($timeMs, 1000);
|
|
$ms = $timeMs % 1000;
|
|
|
|
$dateTime = \DateTimeImmutable::createFromFormat('U.u', sprintf('%d.%06d', $sec, $ms * 1000));
|
|
|
|
if ($dateTime === false) {
|
|
throw new InvalidUuidException("Failed to extract datetime from UUID: {$uuid->toString()}");
|
|
}
|
|
|
|
return $dateTime->setTimezone(new \DateTimeZone('UTC'));
|
|
}
|
|
}
|