Files
Mangarr/src/Service/ChapterUrlGenerator.php
Jérémy Guillot fafff5014c Added:
- Refactor MangaScraperService (not used everywhere now)
- Added JavascriptScraper.php
- Added alternatives slugs in Manga.php
- Improvement in manga edit form
2024-07-21 19:08:46 +02:00

35 lines
934 B
PHP

<?php
namespace App\Service;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
class ChapterUrlGenerator
{
private string $chapterUrlFormat;
public function __construct(string $chapterUrlFormat)
{
$this->chapterUrlFormat = $chapterUrlFormat;
$this->validateUrlFormat($chapterUrlFormat);
}
public function getChapterUrl(string $mangaTitle, float $chapterNumber): string
{
$placeholders = [
'{chapterNumber}' => $chapterNumber,
'{slug}' => $mangaTitle,
];
return str_replace(array_keys($placeholders), array_values($placeholders), $this->chapterUrlFormat);
}
private function validateUrlFormat(string $format): void
{
if (!str_contains($format, '{slug}')) {
throw new InvalidArgumentException("The URL format must contain both {slug} and {chapterNumber} placeholders.");
}
}
}