settings->get(self::KEY)) ?? self::DEFAULT_MINUTES; } /** * The saved hours in Finnish form (e.g. "7,5"), or null when not set. */ public function hours(): ?string { $minutes = self::minutesOf($this->settings->get(self::KEY)); return $minutes === null ? null : self::format($minutes); } /** * Saves the hours ("7,5" or "7.5"); empty removes the setting. * * @throws InvalidDailyCapacity */ public function save(?string $hours): void { $hours = trim((string) $hours); if ($hours === '') { $this->settings->put(self::KEY, null); return; } $minutes = self::minutesOf($hours) ?? throw new InvalidDailyCapacity; $this->settings->put(self::KEY, str_replace(',', '.', self::format($minutes))); } /** * Whether save() accepts the hours: empty, or more than 0 and at most 24. */ public static function isValid(?string $hours): bool { return trim((string) $hours) === '' || self::minutesOf($hours) !== null; } private static function minutesOf(?string $hours): ?int { $hours = str_replace(',', '.', trim((string) $hours)); if (! is_numeric($hours)) { return null; } $minutes = (int) round((float) $hours * 60); return $minutes > 0 && $minutes <= 24 * 60 ? $minutes : null; } private static function format(int $minutes): string { return str_replace('.', ',', rtrim(rtrim(number_format($minutes / 60, 2, '.', ''), '0'), '.')); } }