forked from leftypol/leftypol
EmbedService.php: add WIP
This commit is contained in:
parent
231fcb9ca9
commit
698451a6d5
1 changed files with 64 additions and 0 deletions
64
inc/Services/Embed/EmbedService.php
Normal file
64
inc/Services/Embed/EmbedService.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
namespace Vichan\Services\Embed;
|
||||
|
||||
use RuntimeException;
|
||||
use Vichan\Context;
|
||||
use Vichan\Data\Driver\LogDriver;
|
||||
|
||||
class EmbedService {
|
||||
private array $tuples;
|
||||
private LogDriver $log;
|
||||
|
||||
public function __construct(LogDriver $log) {
|
||||
$this->log = $log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param Context $ctx
|
||||
* @param string $rawText
|
||||
* @return void
|
||||
*/
|
||||
public function matchEmbed(Context $ctx, string $rawText) {
|
||||
if (\filter_var($rawText, \FILTER_VALIDATE_URL) === false) {
|
||||
return null;
|
||||
}
|
||||
$rawText = \trim($rawText);
|
||||
|
||||
foreach ($this->tuples as $cfg) {
|
||||
if (!isset($cfg['match_regex'])) {
|
||||
throw new \RuntimeException('Missing \'match_regex\' field');
|
||||
}
|
||||
$match_regex = $cfg['match_regex'];
|
||||
|
||||
if (\preg_match($match_regex, $rawText, $matches)) {
|
||||
if (!isset($cfg['type'])) {
|
||||
throw new \RuntimeException('Missing \'type\' field');
|
||||
}
|
||||
$type = $cfg['type'];
|
||||
|
||||
if ($type === 'oembed') {
|
||||
if (!isset($cfg['provider'])) {
|
||||
throw new \RuntimeException('Missing \'provider\' field');
|
||||
}
|
||||
$provider = $cfg['provider'];
|
||||
|
||||
$extractor = $ctx->get(OembedExtractor::class);
|
||||
$oembed_resp = $extractor->fetch($provider, $rawText);
|
||||
|
||||
|
||||
} elseif ($type === 'regex') {
|
||||
if (!isset($cfg['thumbnail_url'])) {
|
||||
throw new \RuntimeException('Missing \'thumbnail_url\' field');
|
||||
}
|
||||
$thumbnail_url_regex = $cfg['thumbnail_url'];
|
||||
// Plz somebody review this.
|
||||
$thumbnail_url = \preg_replace($match_regex, $thumbnail_url_regex, $rawText);
|
||||
} else {
|
||||
$this->log->log(LogDriver::ERROR, "Unknown embed type '$type', ignoring");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue