38 lines
1.0 KiB
PHP
38 lines
1.0 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace AeonDumpManager\Service;
|
|
|
|
/**
|
|
* Dumps are named dump_YYYY-MM-DD_HH-mm-ss.sql.gz — lexicographically sortable,
|
|
* and the filename is the single source of truth for a dump's datetime (not
|
|
* filesystem mtime, which can drift across copies/touches).
|
|
*/
|
|
class DumpFilenameParser
|
|
{
|
|
private const PATTERN = '/^dump_(\d{4}-\d{2}-\d{2})_(\d{2}-\d{2}-\d{2})\.sql\.gz$/';
|
|
|
|
public function format(\DateTimeImmutable $dateTime): string
|
|
{
|
|
return 'dump_' . $dateTime->format('Y-m-d_H-i-s') . '.sql.gz';
|
|
}
|
|
|
|
public function parse(string $filename): ?\DateTimeImmutable
|
|
{
|
|
if (preg_match(self::PATTERN, $filename, $matches) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
$dateTime = \DateTimeImmutable::createFromFormat(
|
|
'Y-m-d_H-i-s',
|
|
$matches[1] . '_' . $matches[2]
|
|
);
|
|
|
|
return $dateTime ?: null;
|
|
}
|
|
|
|
public function isValid(string $filename): bool
|
|
{
|
|
return $this->parse($filename) !== null;
|
|
}
|
|
}
|