Files
myep/src/BusProNet/Model/XmlExportInfo.php
T

49 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\BusProNet\Model;
final readonly class XmlExportInfo
{
private const INFO_FILENAME = 'uebertragung.info';
private const TIMESTAMP_FORMAT = 'd.m.Y H:i:s';
public function __construct(
public \DateTimeImmutable $lastTransfer,
public int $fileCount,
) {
}
public static function getFilename(): string
{
return self::INFO_FILENAME;
}
public static function fromString(string $content): self
{
$content = mb_convert_encoding($content, 'UTF-8', 'Windows-1252');
$lines = array_filter(array_map('trim', explode("\n", $content)));
if (count($lines) < 3) {
throw new \InvalidArgumentException('Invalid uebertragung.info format: expected at least 3 lines');
}
$timestamp = \DateTimeImmutable::createFromFormat(self::TIMESTAMP_FORMAT, $lines[0]);
if (false === $timestamp) {
throw new \InvalidArgumentException(sprintf('Invalid timestamp format in uebertragung.info: expected "%s", got "%s"', self::TIMESTAMP_FORMAT, $lines[0]));
}
if (1 !== preg_match('/(\d+)/', $lines[2], $matches)) {
throw new \InvalidArgumentException(sprintf('Could not extract file count from: "%s"', $lines[2]));
}
return new self($timestamp, (int) $matches[1]);
}
public function isNewerThan(self $other): bool
{
return $this->lastTransfer > $other->lastTransfer;
}
}