ldc-arcadia-genesis-php-client/src/Utils/DateUtils.php
Jérémy Munsch 9c47b9cd66
refactor: rename namespace from ArcadiaGenesisPhpClient to ArcadiaApiPhpClient
This change updates the namespace across multiple files to reflect the new naming convention.
2026-09-11 16:55:12 +02:00

89 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Uwil\ArcadiaApiPhpClient\Utils;
final readonly class DateUtils
{
private const FRENCH_DATE_REGEX = '/^\d{4}-\d{2}-\d{2}$/';
private const ISO8601_REGEX = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/';
private const RFC3339_REGEX = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+\-]\d{2}:\d{2})$/';
public function __construct()
{
throw new \Error('Abstract class');
}
public static function toRfc3339(\DateTimeInterface | string $datetime): string
{
$datetime = is_string($datetime) ? self::parseDateTime($datetime) : $datetime;
return $datetime->format(\DATE_RFC3339);
}
public static function parseDateTime(string $datetime): \DateTimeInterface
{
$datetime = trim($datetime);
/** @var \DateTimeInterface|null */
$output = null;
switch (true) {
case (bool) preg_match(self::FRENCH_DATE_REGEX, $datetime):
$output = self::parseFrench($datetime);
break;
case (bool) preg_match(self::RFC3339_REGEX, $datetime):
$output = self::parseRfc3339($datetime);
break;
case (bool) preg_match(self::ISO8601_REGEX, $datetime):
$output = self::parseIso8601($datetime);
break;
default:
try {
$output = new \DateTimeImmutable($datetime);
} catch (\Exception $e) {
throw new \InvalidArgumentException('Unrecognized date/time format: ' . $datetime);
}
}
return $output;
}
protected static function parseFrench(string $datetime): \DateTimeInterface
{
$dt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $datetime . ' 00:00:00');
return $dt ?: self::parseFallback($datetime);
}
protected static function parseRfc3339(string $datetime): \DateTimeInterface
{
$dt = \DateTimeImmutable::createFromFormat(\DateTime::RFC3339, $datetime);
if ($dt !== false) {
return $dt;
}
$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $datetime);
return $dt ?: self::parseFallback($datetime);
}
protected static function parseIso8601(string $datetime): \DateTimeInterface
{
$dt = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $datetime);
return $dt ?: self::parseFallback($datetime);
}
protected static function parseFallback(string $datetime): \DateTimeInterface
{
try {
return new \DateTimeImmutable($datetime);
} catch (\Throwable $e) {
throw new \InvalidArgumentException("Unrecognized date/time format: {$datetime}");
}
}
}