Ensure a talk has a title

This commit is contained in:
Oliver Davies 2021-08-25 01:40:42 +01:00
parent 46d1499e9d
commit 3ea01ec811
2 changed files with 13 additions and 2 deletions

View file

@ -7,6 +7,7 @@ namespace App\TwigExtension;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFunction; use Twig\TwigFunction;
use Webmozart\Assert\Assert;
final class TalkExtension extends AbstractExtension final class TalkExtension extends AbstractExtension
{ {
@ -27,6 +28,16 @@ final class TalkExtension extends AbstractExtension
$talkCollection = new Collection($talks); $talkCollection = new Collection($talks);
return $talkCollection return $talkCollection
->flatMap(fn($talk): array => (array) $talk['events']); ->map(fn($talk): array => (array) $talk['events'])
->filter(function (array $event): bool {
try {
Assert::keyExists($event, 'title');
return true;
}
catch (\RuntimeException $e) {
return false;
}
});
} }
} }

View file

@ -27,7 +27,7 @@ final class TalkExtensionTest extends TestCase
[ [
'title' => 'Building static sites with Sculpin', 'title' => 'Building static sites with Sculpin',
'events' => [ 'events' => [
'', 'title' => 'Building static websites with Sculpin',
] ]
], ],
]; ];