MyHomeCollection/app/Parser.php
2020-11-10 12:32:01 +01:00

52 lines
1.2 KiB
PHP

<?php
namespace App;
use App\Exceptions\UnknownParser;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Config;
abstract class Parser
{
protected $client;
protected $url;
protected function __construct($url)
{
$this->url = $url;
$this->client = new Client([
'timeout' => 60,
'verify' => false,
'headers' => [
'user-agent' => 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; D5803 Build/MOB30M.Z1)',
],
]);
}
/**
* @param string $url
*
* @return \App\Parser|null
*/
public static function factory(string $url): ?Parser
{
$parsers = Config::get('parser.parsers', []);
foreach ($parsers as $domain => $parser) {
if (false !== strpos($url, $domain)) {
return new $parser($url);
}
}
throw new UnknownParser(sprintf(
'Can\'t find an url in «%s» file with the following URL «%s». Please update the file accordingly.',
'parser.php',
$url
));
}
/**
* @return \App\ParsedHome
*/
abstract public function parse(): ParsedHome;
}