feat: use vacation subject prefix

This commit is contained in:
Marek Miklewicz
2026-06-02 21:28:31 +02:00
parent 64b62a5793
commit 56709817b5
18 changed files with 220 additions and 68 deletions
+34 -9
View File
@@ -3,18 +3,27 @@ declare(strict_types=1);
final class RuleValidator
{
/** @var array<string,int> */
private const REPLY_FREQUENCIES = [
'30' => 30,
'60' => 60,
'120' => 120,
'360' => 360,
'720' => 720,
'1440' => 1440,
'2880' => 2880,
'10080' => 10080,
];
/** @param array<string,mixed> $input @return array<string,mixed> */
public static function validate(array $input, Settings $settings): array
{
$subject = trim((string)($input['subject'] ?? ''));
if ($subject === '') {
throw new InvalidArgumentException('Temat jest wymagany.');
$subjectPrefix = trim((string)($input['subject_prefix'] ?? $input['subject'] ?? ''));
if (str_contains($subjectPrefix, "\r") || str_contains($subjectPrefix, "\n")) {
throw new InvalidArgumentException('Przedrostek tematu nie może zawierać nowych linii.');
}
if (str_contains($subject, "\r") || str_contains($subject, "\n")) {
throw new InvalidArgumentException('Temat nie może zawierać nowych linii.');
}
if (strlen($subject) > $settings->maxSubjectBytes()) {
throw new InvalidArgumentException('Temat jest za długi.');
if (strlen($subjectPrefix) > $settings->maxSubjectBytes()) {
throw new InvalidArgumentException('Przedrostek tematu jest za długi.');
}
$body = str_replace(["\r\n", "\r", "\0"], ["\n", "\n", ''], (string)($input['body'] ?? ''));
@@ -33,9 +42,10 @@ final class RuleValidator
if ($endTs <= $startTs) {
throw new InvalidArgumentException('Data zakończenia musi być późniejsza niż data rozpoczęcia.');
}
[$replyEveryMessage, $repeatMinutes] = self::parseReplyFrequency((string)($input['reply_frequency'] ?? '1440'));
return [
'subject' => $subject,
'subject_prefix' => $subjectPrefix,
'body' => $body,
'schedule_mode' => $mode,
'timezone' => $settings->timezone(),
@@ -44,10 +54,25 @@ final class RuleValidator
'start_ts' => $startTs,
'end_ts' => $endTs,
'enabled' => self::truthy($input['enabled'] ?? false),
'reply_every_message' => $replyEveryMessage,
'repeat_minutes' => $repeatMinutes,
'dynamic_scope' => $settings->dynamicMailboxScope(),
];
}
/** @return array{0:bool,1:int} */
private static function parseReplyFrequency(string $value): array
{
$value = trim(strtolower($value));
if ($value === 'every') {
return [true, 0];
}
if (isset(self::REPLY_FREQUENCIES[$value])) {
return [false, self::REPLY_FREQUENCIES[$value]];
}
throw new InvalidArgumentException('Częstość odpowiedzi jest nieprawidłowa.');
}
/** @return array{0:int,1:string} */
private static function parseScheduleValue(string $value, string $mode, DateTimeZone $tz, bool $start): array
{