-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBlogPostDataFactory.php
121 lines (100 loc) · 3.71 KB
/
BlogPostDataFactory.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace Hyde\Framework\Factories;
use Illuminate\Support\Str;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Actions\ConvertsMarkdownToPlainText;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Markdown\Contracts\FrontMatter\BlogPostSchema;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Markdown\Models\Markdown;
use Hyde\Support\Models\DateString;
/**
* Streamlines the data construction specific to a blog post.
*
* Simply pass along the data the class needs to run, then access the data using the toArray() method.
*
* All data can be set using front matter in the page source file. If no front matter is set for the given key,
* this class will attempt to generate and discover the values based on the page and the project's configuration.
*/
class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSchema
{
/**
* The front matter properties supported by this factory.
*
* Note that this class does not add the title, as that is already added to all pages.
*/
final public const SCHEMA = BlogPostSchema::BLOG_POST_SCHEMA;
private readonly FrontMatter $matter;
private readonly Markdown $markdown;
protected readonly ?string $description;
protected readonly ?string $category;
protected readonly ?DateString $date;
protected readonly ?PostAuthor $author;
protected readonly ?FeaturedImage $image;
private readonly string $filePath;
public function __construct(CoreDataObject $pageData)
{
$this->matter = $pageData->matter;
$this->markdown = $pageData->markdown;
$this->filePath = $pageData->sourcePath;
$this->description = $this->makeDescription();
$this->category = $this->makeCategory();
$this->date = $this->makeDate();
$this->author = $this->makeAuthor();
$this->image = $this->makeImage();
}
/**
* @return array{description: string|null, category: string|null, date: \Hyde\Support\Models\DateString|null, author: \Hyde\Framework\Features\Blogging\Models\PostAuthor|null, image: \Hyde\Framework\Features\Blogging\Models\FeaturedImage|null}
*/
public function toArray(): array
{
return [
'description' => $this->description,
'category' => $this->category,
'date' => $this->date,
'author' => $this->author,
'image' => $this->image,
];
}
protected function makeDescription(): string
{
return $this->getMatter('description') ?? $this->makeDescriptionFromMarkdownBody();
}
protected function makeCategory(): ?string
{
return $this->getMatter('category');
}
protected function makeDate(): ?DateString
{
if ($this->getMatter('date')) {
return new DateString($this->getMatter('date'));
}
return null;
}
protected function makeAuthor(): ?PostAuthor
{
if ($this->getMatter('author')) {
return PostAuthor::getOrCreate($this->getMatter('author'));
}
return null;
}
protected function makeImage(): ?FeaturedImage
{
if ($this->getMatter('image')) {
return FeaturedImageFactory::make($this->matter, $this->filePath);
}
return null;
}
private function makeDescriptionFromMarkdownBody(): string
{
return Str::limit((new ConvertsMarkdownToPlainText($this->markdown->body()))->execute(), 125);
}
protected function getMatter(string $key): string|null|array
{
/** @var string|null|array $value */
$value = $this->matter->get($key);
return $value;
}
}