From 2334edf69280bc33157f4cc33d21e3982ffa9631 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 18 Jan 2019 21:55:37 +0000 Subject: [PATCH] Refactor, ensure each event is an array --- src/TalksBundle/TwigExtension/TalksExtension.php | 16 +++++++++++----- .../TwigExtension/TalksExtensionTest.php | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/TalksBundle/TwigExtension/TalksExtension.php b/src/TalksBundle/TwigExtension/TalksExtension.php index 0bdee6dd..8d82eee5 100644 --- a/src/TalksBundle/TwigExtension/TalksExtension.php +++ b/src/TalksBundle/TwigExtension/TalksExtension.php @@ -47,7 +47,7 @@ class TalksExtension extends Twig_Extension public function getAll($talks, array $eventData = []): Collection { return collect($talks)->sortBy(function ($talk) { - return collect($talk['events'])->pluck('date')->sort()->last(); + return $this->getLastDate($talk); }); } @@ -59,10 +59,10 @@ class TalksExtension extends Twig_Extension * * @return Collection A sorted collection of talks. */ - public function getUpcoming($talks, array $eventData = []) + public function getUpcoming($talks, array $eventData = []): Collection { return $this->getAll($talks)->filter(function ($talk) { - return collect($talk['events'])->pluck('date')->sort()->last() >= $this->today; + return $this->getLastDate($talk) >= $this->today; })->values(); } @@ -74,10 +74,10 @@ class TalksExtension extends Twig_Extension * * @return Collection A sorted collection of talks. */ - public function getPast($talks, array $eventData = []) + public function getPast($talks, array $eventData = []): Collection { return $this->getAll($talks)->filter(function ($talk) { - return collect($talk['events'])->pluck('date')->sort()->last() < $this->today; + return $this->getLastDate($talk) < $this->today; })->values(); } @@ -99,6 +99,7 @@ class TalksExtension extends Twig_Extension // event data (e.g. event name and website). return collect($talk['events']) ->map(function ($event) use ($talk, $eventData) { + $event = collect($event); $event = $event->merge($eventData->get($event->get('event')))->all(); @@ -114,4 +115,9 @@ class TalksExtension extends Twig_Extension { return 'talks'; } + + private function getLastDate($talk): string + { + return (string) collect($talk['events'])->pluck('date')->sort()->last(); + } } diff --git a/tests/TalksBundle/TwigExtension/TalksExtensionTest.php b/tests/TalksBundle/TwigExtension/TalksExtensionTest.php index a1aece41..36a29708 100644 --- a/tests/TalksBundle/TwigExtension/TalksExtensionTest.php +++ b/tests/TalksBundle/TwigExtension/TalksExtensionTest.php @@ -80,14 +80,14 @@ class TalksExtensionTest extends TestCase $pastTalk = [ 'title' => 'Past talk', 'events' => [ - 'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT), + ['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)], ] ]; $futureTalk = [ 'title' => 'Future talk', 'events' => [ - ['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)], + ['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)], ], ]; @@ -103,21 +103,21 @@ class TalksExtensionTest extends TestCase $pastTalk = [ 'title' => 'Past talk', 'events' => [ - 'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT), + ['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)], ] ]; $todayTalk = [ 'title' => 'A talk that it happening today', 'events' => [ - ['date' => (new DateTime('now'))->format(TalksExtension::DATE_FORMAT)], + ['date' => (new DateTime('now'))->format(TalksExtension::DATE_FORMAT)], ], ]; $futureTalk = [ 'title' => 'Future talk', 'events' => [ - ['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)], + ['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)], ], ];