From 394be76029ffbb6e505cb5b27bb536776fc1abcf Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 20 May 2019 12:55:18 +0100 Subject: [PATCH] Add filters for upcoming and past events --- .../src/TwigExtension/TalksExtension.php | 34 ++++++--- src/Talks/tests/RetrievingEventsTest.php | 76 ++++++++++++++++++- 2 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/Talks/src/TwigExtension/TalksExtension.php b/src/Talks/src/TwigExtension/TalksExtension.php index 7b418c68..e00d8f2b 100644 --- a/src/Talks/src/TwigExtension/TalksExtension.php +++ b/src/Talks/src/TwigExtension/TalksExtension.php @@ -37,8 +37,11 @@ class TalksExtension extends AbstractExtension public function getFilters() { return [ - new TwigFilter('past', [$this, 'filterPast']), - new TwigFilter('upcoming', [$this, 'filterUpcoming']), + new TwigFilter('events', [$this, 'getEvents']), + new TwigFilter('pastEvents', [$this, 'filterPastEvents']), + new TwigFilter('pastTalks', [$this, 'filterPastTalks']), + new TwigFilter('upcomingEvents', [$this, 'filterUpcomingEvents']), + new TwigFilter('upcomingTalks', [$this, 'filterUpcomingTalks']), ]; } @@ -64,7 +67,6 @@ class TalksExtension extends AbstractExtension }); } - public function getEvents($talks): Collection { return collect($talks)->flatMap(function ($talk): array { @@ -72,18 +74,18 @@ class TalksExtension extends AbstractExtension }); } - public function filterUpcoming(Collection $talks) + public function filterUpcomingTalks(Collection $talks): array { - return $talks->filter(function ($talk) { + return $talks->filter(function ($talk): bool { return $this->getLastDate($talk) >= $this->today; - })->values(); + })->values()->toArray(); } - public function filterPast(Collection $talks) + public function filterPastTalks(Collection $talks): array { - return $talks->filter(function ($talk) { + return $talks->filter(function ($talk): bool { return $this->getLastDate($talk) < $this->today; - })->values(); + })->values()->toArray(); } private function getLastDate($talk): string @@ -91,4 +93,18 @@ class TalksExtension extends AbstractExtension return $this->getEvents(collect([$talk])) ->pluck('date')->max(); } + + public function filterUpcomingEvents(Collection $events): array + { + return $events->filter(function ($event): bool { + return $event['date'] >= $this->today; + })->sortBy('date')->toArray(); + } + + public function filterPastEvents(Collection $events): array + { + return $events->filter(function ($event): bool { + return $event['date'] < $this->today; + })->sortBy('date')->toArray(); + } } diff --git a/src/Talks/tests/RetrievingEventsTest.php b/src/Talks/tests/RetrievingEventsTest.php index a51ec056..2e466e67 100644 --- a/src/Talks/tests/RetrievingEventsTest.php +++ b/src/Talks/tests/RetrievingEventsTest.php @@ -49,6 +49,80 @@ class RetrievingEventsTest extends TestCase ], ]; - $this->assertCount(3, $this->extension->getEvents([$talkA, $talkB])); + $talks = $this->extension->getTalks([$talkA, $talkB]); + $this->assertInstanceOf(Collection::class, $talks); + $this->assertCount(3, $this->extension->getEvents($talks)); + } + + /** @test */ + public function get_past_events() + { + $talkA = [ + 'title' => 'Test Driven Drupal', + 'events' => [ + [ + 'event' => 'php_south_wales', + 'date' => (new DateTime('+1 days'))->getTimestamp(), + ], + [ + 'event' => 'drupalcamp_london', + 'date' => (new DateTime('-1 days'))->getTimestamp(), + ], + ], + ]; + + $talkB = [ + 'title' => 'Taking Flight with Tailwind CSS', + 'events' => [ + [ + 'event' => 'blue_conf_2019', + 'date' => (new DateTime('-2 days'))->getTimestamp(), + ], + ], + ]; + + $talks = $this->extension->getTalks([$talkA, $talkB]); + $events = $this->extension->getEvents($talks); + + $this->assertInstanceOf(Collection::class, $talks); + $this->assertInstanceOf(Collection::class, $events); + + $this->assertCount(2, $this->extension->filterPastEvents($events)); + } + /** @test */ + public function get_current_or_upcoming_events() + { + $talkA = [ + 'title' => 'Test Driven Drupal', + 'events' => [ + [ + 'event' => 'php_south_wales', + 'date' => (new DateTime('+0 days'))->getTimestamp(), + ], + [ + 'event' => 'drupalcamp_london', + 'date' => (new DateTime('-1 days'))->getTimestamp(), + ], + ], + ]; + + $talkB = [ + 'title' => 'Taking Flight with Tailwind CSS', + 'events' => [ + [ + 'event' => 'blue_conf_2019', + 'date' => (new DateTime('+2 days'))->getTimestamp(), + ], + ], + ]; + + + $talks = $this->extension->getTalks([$talkA, $talkB]); + $events = $this->extension->getEvents($talks); + + $this->assertInstanceOf(Collection::class, $talks); + $this->assertInstanceOf(Collection::class, $events); + + $this->assertCount(2, $this->extension->filterUpcomingEvents($events)); } }