From 8d1fe01d837f832c8544bf0ae52058eede13a539 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 20 May 2019 00:58:36 +0100 Subject: [PATCH] Get events for talks --- .../src/TwigExtension/TalksExtension.php | 16 ++++++++++++++ .../TwigExtension/TalksExtensionTest.php | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Talks/src/TwigExtension/TalksExtension.php b/src/Talks/src/TwigExtension/TalksExtension.php index 4564ad9b..ddd993be 100644 --- a/src/Talks/src/TwigExtension/TalksExtension.php +++ b/src/Talks/src/TwigExtension/TalksExtension.php @@ -5,6 +5,8 @@ namespace App\Talks\TwigExtension; use Illuminate\Support\Collection; use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection; use Twig_Extension; +use Tightenco\Collect\Support\Collection; +use Twig\TwigFilter; use Twig_SimpleFunction; class TalksExtension extends Twig_Extension @@ -34,6 +36,13 @@ class TalksExtension extends Twig_Extension ]; } + public function getFilters() + { + return [ + new TwigFilter('events', [$this, 'getEvents']), + ]; + } + /** * Get all upcoming and previous talks. * @@ -88,4 +97,11 @@ class TalksExtension extends Twig_Extension { return (string) collect($talk['events'])->pluck('date')->sort()->last(); } + + public function getEvents(Collection $talks): Collection + { + return $talks->flatMap(function ($talk): array { + return $talk['events']; + }); + } } diff --git a/src/Talks/tests/TwigExtension/TalksExtensionTest.php b/src/Talks/tests/TwigExtension/TalksExtensionTest.php index d8b59914..ff212051 100644 --- a/src/Talks/tests/TwigExtension/TalksExtensionTest.php +++ b/src/Talks/tests/TwigExtension/TalksExtensionTest.php @@ -141,4 +141,26 @@ class TalksExtensionTest extends TestCase $this->assertCount(1, $this->extension->getUpcoming([$talk])); $this->assertEmpty($this->extension->getPast([$talk])); } + + /** @test */ + public function get_events_from_talks() + { + $talkA = [ + 'title' => 'Talk A', + 'events' => [ + ['event' => 'event_a', 'date' => (new DateTime('-1 days'))->getTimestamp()], + ['event' => 'event_b', 'date' => (new DateTime('+1 days'))->getTimestamp()], + ], + ]; + + $talkB = [ + 'title' => 'Talk B', + 'events' => [ + ['event' => 'event_a', 'date' => (new DateTime('-3 days'))->getTimestamp()], + ], + ]; + + $talks = collect([$talkA, $talkB]); + $this->assertCount(3, $this->extension->getEvents($talks)); + } }