|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Factories; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Framework\Features\Blogging\Models\FeaturedImage; |
|
8
|
|
|
use Hyde\Framework\Features\Blogging\Models\LocalFeaturedImage; |
|
9
|
|
|
use Hyde\Framework\Features\Blogging\Models\RemoteFeaturedImage; |
|
10
|
|
|
use Hyde\Hyde; |
|
11
|
|
|
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema; |
|
12
|
|
|
use Hyde\Markdown\Models\FrontMatter; |
|
13
|
|
|
use Illuminate\Support\Str; |
|
14
|
|
|
use function is_string; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use function str_starts_with; |
|
17
|
|
|
|
|
18
|
|
|
class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedImageSchema |
|
19
|
|
|
{ |
|
20
|
|
|
final public const SCHEMA = FeaturedImageSchema::FEATURED_IMAGE_SCHEMA; |
|
21
|
|
|
|
|
22
|
|
|
protected readonly string $source; |
|
23
|
|
|
protected readonly ?string $altText; |
|
24
|
|
|
protected readonly ?string $titleText; |
|
25
|
|
|
protected readonly ?string $authorName; |
|
26
|
|
|
protected readonly ?string $authorUrl; |
|
27
|
|
|
protected readonly ?string $copyrightText; |
|
28
|
|
|
protected readonly ?string $licenseName; |
|
29
|
|
|
protected readonly ?string $licenseUrl; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
private readonly FrontMatter $matter, |
|
33
|
|
|
) { |
|
34
|
|
|
$this->source = $this->makeSource(); |
|
|
|
|
|
|
35
|
|
|
$this->altText = $this->makeAltText(); |
|
|
|
|
|
|
36
|
|
|
$this->titleText = $this->makeTitleText(); |
|
|
|
|
|
|
37
|
|
|
$this->authorName = $this->makeAuthorName(); |
|
|
|
|
|
|
38
|
|
|
$this->authorUrl = $this->makeAuthorUrl(); |
|
|
|
|
|
|
39
|
|
|
$this->copyrightText = $this->makeCopyrightText(); |
|
|
|
|
|
|
40
|
|
|
$this->licenseName = $this->makeLicenseName(); |
|
|
|
|
|
|
41
|
|
|
$this->licenseUrl = $this->makeLicenseUrl(); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @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} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function toArray(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
|
|
|
|
|
50
|
|
|
'source' => $this->source, |
|
51
|
|
|
'altText' => $this->altText, |
|
52
|
|
|
'titleText' => $this->titleText, |
|
53
|
|
|
'authorName' => $this->authorName, |
|
54
|
|
|
'authorUrl' => $this->authorUrl, |
|
55
|
|
|
'copyrightText' => $this->copyrightText, |
|
56
|
|
|
'licenseName' => $this->licenseName, |
|
57
|
|
|
'licenseUrl' => $this->licenseUrl, |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function make(FrontMatter $matter): FeaturedImage |
|
62
|
|
|
{ |
|
63
|
|
|
$data = (new static($matter))->toArray(); |
|
64
|
|
|
|
|
65
|
|
|
if (self::isRemote($matter)) { |
|
66
|
|
|
return new RemoteFeaturedImage(...$data); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new LocalFeaturedImage(...$data); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function makeSource(): string |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_string($this->getStringMatter('image'))) { |
|
75
|
|
|
if (str_starts_with($this->getStringMatter('image'), 'http')) { |
|
|
|
|
|
|
76
|
|
|
return $this->getStringMatter('image'); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return self::normalizeLocalImagePath($this->getStringMatter('image')); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($this->getStringMatter('image.url') !== null) { |
|
83
|
|
|
return $this->getStringMatter('image.url'); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($this->getStringMatter('image.path') !== null) { |
|
87
|
|
|
return $this->normalizeLocalImagePath($this->getStringMatter('image.path')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Todo, we might want to add a note about which file caused the error. |
|
91
|
|
|
// We could also check for these before calling the factory, and just ignore the image if it's not valid. |
|
92
|
|
|
throw new RuntimeException('No featured image source was found'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function makeAltText(): ?string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->getStringMatter('image.description'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function makeTitleText(): ?string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->getStringMatter('image.title'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function makeAuthorName(): ?string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getStringMatter('image.author'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function makeAuthorUrl(): ?string |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getStringMatter('image.attributionUrl'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function makeCopyrightText(): ?string |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->getStringMatter('image.copyright'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function makeLicenseName(): ?string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getStringMatter('image.license'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function makeLicenseUrl(): ?string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getStringMatter('image.licenseUrl'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected static function normalizeLocalImagePath(string $path): string |
|
131
|
|
|
{ |
|
132
|
|
|
$path = Hyde::pathToRelative($path); |
|
133
|
|
|
|
|
134
|
|
|
$path = Str::after($path, Hyde::getMediaDirectory()); |
|
135
|
|
|
$path = Str::after($path, Hyde::getMediaOutputDirectory()); |
|
136
|
|
|
|
|
137
|
|
|
return unslash($path); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
protected static function isRemote(FrontMatter $matter): bool |
|
141
|
|
|
{ |
|
142
|
|
|
if (is_string($matter->get('image')) && str_starts_with($matter->get('image'), 'http')) { |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $matter->get('image.url') !== null; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
protected function getStringMatter(string $key): ?string |
|
150
|
|
|
{ |
|
151
|
|
|
return is_string($this->matter->get($key)) ? $this->matter->get($key) : null; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|