40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use DOMDocument;
|
|
use DOMXPath;
|
|
|
|
final class DirectoryCard
|
|
{
|
|
public static function links(array $article, string $body): array
|
|
{
|
|
$detail = self::hasText($article['summary']) || self::hasBody($body) ? '/articles/' . $article['slug'] : '';
|
|
$primary = ($article['card']['url'] ?? '') ?: $detail;
|
|
|
|
return ['primary' => $primary, 'detail' => $primary === $detail ? '' : $detail];
|
|
}
|
|
|
|
private static function hasText(string $text): bool
|
|
{
|
|
return preg_match('/[^\s\p{Z}\p{Cf}]/u', $text) === 1;
|
|
}
|
|
|
|
private static function hasBody(string $html): bool
|
|
{
|
|
if (self::hasText(html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8'))) {
|
|
return true;
|
|
}
|
|
if (!str_contains($html, 'src=')) {
|
|
return false;
|
|
}
|
|
$document = new DOMDocument();
|
|
$document->loadHTML('<meta charset="utf-8">' . $html, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
|
$xpath = new DOMXPath($document);
|
|
|
|
return (bool) $xpath->evaluate('boolean(//img[@src] | //audio[@src] | //video[@src] | //audio/source[@src] | //video/source[@src])');
|
|
}
|
|
}
|