fedi-feed-router/app/Services/Factories/HomepageParserFactory.php

49 lines
1.3 KiB
PHP
Raw Normal View History

2025-06-29 21:33:18 +02:00
<?php
namespace App\Services\Factories;
use App\Contracts\HomepageParserInterface;
use App\Services\Parsers\VrtHomepageParserAdapter;
2025-06-29 21:39:28 +02:00
use App\Services\Parsers\BelgaHomepageParserAdapter;
2025-06-29 21:33:18 +02:00
use Exception;
class HomepageParserFactory
{
private static array $parsers = [
VrtHomepageParserAdapter::class,
2025-06-29 21:39:28 +02:00
BelgaHomepageParserAdapter::class,
2025-06-29 21:33:18 +02:00
];
public static function getParser(string $url): HomepageParserInterface
{
foreach (self::$parsers as $parserClass) {
$parser = new $parserClass();
if ($parser->canParse($url)) {
return $parser;
}
}
throw new Exception("No homepage parser found for URL: {$url}");
}
public static function getAllParsers(): array
{
return array_map(fn($parserClass) => new $parserClass(), self::$parsers);
}
public static function getSupportedSources(): array
{
return array_map(function($parserClass) {
$parser = new $parserClass();
return $parser->getSourceName();
}, self::$parsers);
}
public static function registerParser(string $parserClass): void
{
if (!in_array($parserClass, self::$parsers)) {
self::$parsers[] = $parserClass;
}
}
}