uiharu/src/Lookup/NicoNicoLookupResult.php

92 lines
2.7 KiB
PHP

<?php
namespace Uiharu\Lookup;
use DOMElement;
use Index\MediaType;
use Uiharu\Url;
final class NicoNicoLookupResult implements \Uiharu\ILookupResult {
private DOMElement|false|null $title = null;
private DOMElement|false|null $description = null;
private DOMElement|false|null $previewImage = null;
public function __construct(
private Url $url,
private string $videoId,
private DOMElement $thumbInfo,
private array $urlQuery,
) {}
public function getUrl(): Url {
return $this->url;
}
public function getObjectType(): string {
return 'niconico:video';
}
public function getNicoNicoVideoId(): string {
return $this->videoId;
}
public function getNicoNicoThumbInfo(): DOMElement {
return $this->thumbInfo;
}
public function getNicoNicoUrlQuery(): array {
return $this->urlQuery;
}
public function hasNicoNicoVideoStartTime(): bool {
return isset($this->urlQuery['from']);
}
public function getNicoNicoVideoStartTime(): string {
return $this->urlQuery['from'] ?? '';
}
public function hasMediaType(): bool {
return false;
}
public function getMediaType(): MediaType {
throw new RuntimeException('Unsupported');
}
public function hasColour(): bool {
return true;
}
public function getColour(): int {
return 0x252525;
}
public function hasTitle(): bool {
if($this->title === null)
$this->title = $this->thumbInfo->getElementsByTagName('title')[0] ?? false;
return $this->title !== false;
}
public function getTitle(): string {
return $this->hasTitle() ? trim($this->title->textContent) : 'No Title';
}
public function hasSiteName(): bool {
return true;
}
public function getSiteName(): string {
return 'ニコニコ動画';
}
public function hasDescription(): bool {
if($this->description === null)
$this->description = $this->thumbInfo->getElementsByTagName('description')[0] ?? false;
return $this->description !== false && !empty($this->description->textContent);
}
public function getDescription(): string {
return $this->hasDescription() ? trim($this->description->textContent) : '';
}
public function hasPreviewImage(): bool {
if($this->previewImage === null)
$this->previewImage = $this->thumbInfo->getElementsByTagName('thumbnail_url')[0] ?? false;
return $this->previewImage !== false && !empty($this->previewImage->textContent);
}
public function getPreviewImage(): string {
return $this->hasPreviewImage() ? (trim($this->previewImage->textContent) . '.L') : '';
}
}