49 lines
No EOL
1.3 KiB
PHP
49 lines
No EOL
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Factories;
|
|
|
|
use App\Contracts\HomepageParserInterface;
|
|
use App\Services\Parsers\VrtHomepageParserAdapter;
|
|
use App\Services\Parsers\BelgaHomepageParserAdapter;
|
|
use Exception;
|
|
|
|
class HomepageParserFactory
|
|
{
|
|
private static array $parsers = [
|
|
VrtHomepageParserAdapter::class,
|
|
BelgaHomepageParserAdapter::class,
|
|
];
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |