Add filters for upcoming and past events

This commit is contained in:
Oliver Davies 2019-05-20 12:55:18 +01:00
parent e94ff3addd
commit 394be76029
2 changed files with 100 additions and 10 deletions

View file

@ -37,8 +37,11 @@ class TalksExtension extends AbstractExtension
public function getFilters() public function getFilters()
{ {
return [ return [
new TwigFilter('past', [$this, 'filterPast']), new TwigFilter('events', [$this, 'getEvents']),
new TwigFilter('upcoming', [$this, 'filterUpcoming']), 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 public function getEvents($talks): Collection
{ {
return collect($talks)->flatMap(function ($talk): array { 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; 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; return $this->getLastDate($talk) < $this->today;
})->values(); })->values()->toArray();
} }
private function getLastDate($talk): string private function getLastDate($talk): string
@ -91,4 +93,18 @@ class TalksExtension extends AbstractExtension
return $this->getEvents(collect([$talk])) return $this->getEvents(collect([$talk]))
->pluck('date')->max(); ->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();
}
} }

View file

@ -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));
} }
} }