Skip to content

Commit c78f6fe

Browse files
committed
Better typing for TILs
1 parent c01b82f commit c78f6fe

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

resources/views/pages/til/index.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<ul class="ps-2">
1818
{% for til in entries %}
19-
<li class="pb-1"><a href="{{ til.url }}" class="cursor-pointer hover:underline">{{ til.title }}</a></li>
19+
<li class="pb-1"><a href="{{ til.url }}" class="cursor-pointer hover:underline">{{ til.frontMatter.title }}</a></li>
2020
{% endfor %}
2121
</ul>
2222
</div>

resources/views/pages/til/show.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<h2 class="text-2xl font-semibold {{ colors.textLightBg }}">
77
<a href="{{ urls.til.href }}" class="font-black underline">TIL</a>
88
<span class="block pt-2 md:inline md:pt-0">
9-
{{ til.title }}
9+
{{ til.frontMatter.title }}
1010
</span>
1111
</h2>
1212
</div>
@@ -17,11 +17,11 @@
1717
</div>
1818
</div>
1919

20-
{% if til.sources %}
20+
{% if til.frontMatter.sources %}
2121
<div class="mt-4 md:mt-14 mb-5 text-gray-600 dark:text-gray-400">
2222
<div class="mb-2 border-b-2 border-gray-500">
2323
<h4 class="text-sm">
24-
{% if til.sources|length == 1 %}
24+
{% if til.frontMatter.sources|length == 1 %}
2525
Source
2626
{% else %}
2727
Sources
@@ -30,7 +30,7 @@
3030
</div>
3131

3232
<div class="flex flex-col text-sm">
33-
{% for source in til.sources %}
33+
{% for source in til.frontMatter.sources %}
3434
<a href="{{ source }}" class="hover:underline">{{ source }}</a>
3535
{% endfor %}
3636
</div>

src/Til/Entry.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
final readonly class Entry
88
{
99
public function __construct(
10-
public string $category,
11-
public string $title,
10+
public FrontMatter $frontMatter,
1211
public string $content,
1312
public string $slug,
1413
public string $url,
15-
public array $sources,
1614
) {}
1715
}

src/Til/FrontMatter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mopolo\Cv\Til;
6+
7+
final readonly class FrontMatter
8+
{
9+
public function __construct(
10+
public string $category,
11+
public string $title,
12+
/** @var array<string>|null */
13+
public ?array $sources = null,
14+
) {
15+
}
16+
}

src/Til/Renderer.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Mopolo\Cv\Til;
66

7+
use CuyZ\Valinor\Mapper\Source\Source;
8+
use CuyZ\Valinor\MapperBuilder;
79
use Exception;
810
use League\CommonMark\Environment\Environment;
911
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
@@ -49,7 +51,7 @@ public function renderList(): array
4951
foreach ($finder as $file) {
5052
$entry = $this->renderFile($file);
5153

52-
$tils[$entry->category][] = $entry;
54+
$tils[$entry->frontMatter->category][] = $entry;
5355
}
5456

5557
return $tils;
@@ -91,33 +93,19 @@ public function render(string $path, string $content): Entry
9193
throw new Exception('Missing front matter in ' . $path);
9294
}
9395

94-
$frontMatter = $result->getFrontMatter();
96+
$rawFrontMatter = $result->getFrontMatter();
9597

96-
if (!is_array($frontMatter)) {
97-
throw new Exception('Invalid front matter in ' . $path);
98+
if (!is_array($rawFrontMatter)) {
99+
throw new Exception("Front matter is not an array");
98100
}
99101

100-
$category = $frontMatter['category'] ?? throw new Exception('Missing category in ' . $path);
101-
102-
if (!is_string($category)) {
103-
throw new Exception('Invalid category in ' . $path);
104-
}
105-
106-
$title = $frontMatter['title'] ?? throw new Exception('Missing title in ' . $path);
107-
108-
if (!is_string($title)) {
109-
throw new Exception('Invalid title in ' . $path);
110-
}
111-
112-
$sources = $frontMatter['sources'] ?? [];
102+
$frontMatter = (new MapperBuilder())->mapper()->map(FrontMatter::class, Source::array($rawFrontMatter));
113103

114104
return new Entry(
115-
$category,
116-
$title,
105+
$frontMatter,
117106
$result->getContent(),
118107
$slug,
119108
$this->url($slug),
120-
$sources,
121109
);
122110
}
123111

0 commit comments

Comments
 (0)