1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Factories; |
6
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Hyde\Markdown\Models\FrontMatter; |
11
|
|
|
use Hyde\Foundation\Kernel\Hyperlinks; |
12
|
|
|
use Hyde\Framework\Features\Blogging\Models\FeaturedImage; |
13
|
|
|
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema; |
14
|
|
|
|
15
|
|
|
use function str_starts_with; |
16
|
|
|
use function is_string; |
17
|
|
|
use function Hyde\unslash; |
18
|
|
|
|
19
|
|
|
class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedImageSchema |
20
|
|
|
{ |
21
|
|
|
final public const SCHEMA = FeaturedImageSchema::FEATURED_IMAGE_SCHEMA; |
22
|
|
|
|
23
|
|
|
protected readonly string $source; |
24
|
|
|
protected readonly ?string $altText; |
25
|
|
|
protected readonly ?string $titleText; |
26
|
|
|
protected readonly ?string $authorName; |
27
|
|
|
protected readonly ?string $authorUrl; |
28
|
|
|
protected readonly ?string $copyrightText; |
29
|
|
|
protected readonly ?string $licenseName; |
30
|
|
|
protected readonly ?string $licenseUrl; |
31
|
|
|
protected readonly ?string $caption; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
private readonly FrontMatter $matter, |
35
|
|
|
private readonly ?string $filePath = null, |
36
|
|
|
) { |
37
|
|
|
$this->source = $this->makeSource(); |
|
|
|
|
38
|
|
|
$this->altText = $this->getStringMatter('image.altText') ?? $this->getStringMatter('image.alt'); |
|
|
|
|
39
|
|
|
$this->titleText = $this->getStringMatter('image.titleText'); |
|
|
|
|
40
|
|
|
$this->authorName = $this->getStringMatter('image.authorName'); |
|
|
|
|
41
|
|
|
$this->authorUrl = $this->getStringMatter('image.authorUrl'); |
|
|
|
|
42
|
|
|
$this->copyrightText = $this->getStringMatter('image.copyright'); |
|
|
|
|
43
|
|
|
$this->licenseName = $this->getStringMatter('image.licenseName'); |
|
|
|
|
44
|
|
|
$this->licenseUrl = $this->getStringMatter('image.licenseUrl'); |
|
|
|
|
45
|
|
|
$this->caption = $this->getStringMatter('image.caption'); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array{source: string, altText: string|null, titleText: string|null, authorName: string|null, authorUrl: string|null, copyrightText: string|null, licenseName: string|null, licenseUrl: string|null, caption: string|null} |
50
|
|
|
*/ |
51
|
|
|
public function toArray(): array |
52
|
|
|
{ |
53
|
|
|
return [ |
|
|
|
|
54
|
|
|
'source' => $this->source, |
55
|
|
|
'altText' => $this->altText, |
56
|
|
|
'titleText' => $this->titleText, |
57
|
|
|
'authorName' => $this->authorName, |
58
|
|
|
'authorUrl' => $this->authorUrl, |
59
|
|
|
'copyrightText' => $this->copyrightText, |
60
|
|
|
'licenseName' => $this->licenseName, |
61
|
|
|
'licenseUrl' => $this->licenseUrl, |
62
|
|
|
'caption' => $this->caption, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function make(FrontMatter $matter, ?string $filePath = null): FeaturedImage |
67
|
|
|
{ |
68
|
|
|
return new FeaturedImage(...(new static($matter, $filePath))->toArray()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function makeSource(): string |
72
|
|
|
{ |
73
|
|
|
$value = $this->getStringMatter('image') ?? $this->getStringMatter('image.source'); |
74
|
|
|
|
75
|
|
|
if (empty($value)) { |
76
|
|
|
throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (Hyperlinks::isRemote($value)) { |
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return self::normalizeLocalImagePath($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function normalizeLocalImagePath(string $path): string |
87
|
|
|
{ |
88
|
|
|
$path = Hyde::pathToRelative($path); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$path = Str::after($path, Hyde::getMediaDirectory()); |
|
|
|
|
91
|
|
|
$path = Str::after($path, Hyde::getMediaOutputDirectory()); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return str_starts_with($path, '//') ? $path : unslash($path); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function getStringMatter(string $key): ?string |
97
|
|
|
{ |
98
|
|
|
/** @var string|null $value */ |
99
|
|
|
$value = $this->matter->get($key); |
100
|
|
|
|
101
|
|
|
return is_string($value) ? $value : null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|