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