Sync global-autoresponder current state
This commit is contained in:
+21
-14
@@ -29,8 +29,16 @@ final class RuleValidator
|
||||
];
|
||||
|
||||
/** @param array<string,mixed> $input @return array<string,mixed> */
|
||||
public static function validate(array $input, Settings $settings): array
|
||||
public static function validate(array $input, Settings $settings, bool $allowPastSchedule = false): array
|
||||
{
|
||||
$name = trim(str_replace(["\r\n", "\r", "\0"], ["\n", "\n", ''], (string)($input['name'] ?? '')));
|
||||
if (str_contains($name, "\n")) {
|
||||
throw new InvalidArgumentException('Nazwa autorespondera nie może zawierać nowych linii.');
|
||||
}
|
||||
if (strlen($name) > 255) {
|
||||
throw new InvalidArgumentException('Nazwa autorespondera jest za długa.');
|
||||
}
|
||||
|
||||
$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.');
|
||||
@@ -52,15 +60,22 @@ final class RuleValidator
|
||||
$tz = new DateTimeZone($settings->timezone());
|
||||
[$startTs, $startValue] = self::parseScheduleValue((string)($input['start_value'] ?? ''), $mode, $tz, true);
|
||||
[$endTs, $endValue] = self::parseScheduleValue((string)($input['end_value'] ?? ''), $mode, $tz, false);
|
||||
if (substr($startValue, 0, 10) < (new DateTimeImmutable('today', $tz))->format('Y-m-d')) {
|
||||
throw new InvalidArgumentException('Data rozpoczęcia nie może być wcześniejsza niż aktualna data.');
|
||||
if (!$allowPastSchedule) {
|
||||
$today = (new DateTimeImmutable('today', $tz))->format('Y-m-d');
|
||||
if (substr($startValue, 0, 10) < $today) {
|
||||
throw new InvalidArgumentException('Data rozpoczęcia nie może być wcześniejsza niż aktualna data.');
|
||||
}
|
||||
if (substr($endValue, 0, 10) < $today) {
|
||||
throw new InvalidArgumentException('Data zakończenia nie może być wcześniejsza niż aktualna data.');
|
||||
}
|
||||
}
|
||||
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'), $settings);
|
||||
$repeatMinutes = self::parseReplyFrequency((string)($input['reply_frequency'] ?? '1440'));
|
||||
|
||||
return [
|
||||
'name' => $name,
|
||||
'subject_prefix' => $subjectPrefix,
|
||||
'body' => $body,
|
||||
'schedule_mode' => $mode,
|
||||
@@ -70,24 +85,16 @@ 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, Settings $settings): array
|
||||
private static function parseReplyFrequency(string $value): int
|
||||
{
|
||||
$value = trim(strtolower($value));
|
||||
if ($value === 'every') {
|
||||
if ($settings->backendMode() === 'da') {
|
||||
throw new InvalidArgumentException('Częstość odpowiedzi jest nieprawidłowa.');
|
||||
}
|
||||
return [true, 0];
|
||||
}
|
||||
if (isset(self::REPLY_FREQUENCIES[$value])) {
|
||||
return [false, self::REPLY_FREQUENCIES[$value]];
|
||||
return self::REPLY_FREQUENCIES[$value];
|
||||
}
|
||||
throw new InvalidArgumentException('Częstość odpowiedzi jest nieprawidłowa.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user