Change filters into functions
This commit is contained in:
parent
7955042ec0
commit
1c3d7f820f
|
@ -30,16 +30,9 @@ class TalksExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('get_all_talks', [$this, 'getAllTalks']),
|
new TwigFunction('get_all_talks', [$this, 'getAllTalks']),
|
||||||
];
|
new TwigFunction('get_upcoming_talks', [$this, 'getUpcomingTalks']),
|
||||||
}
|
new TwigFunction('get_past_talks', [$this, 'getPastTalks']),
|
||||||
|
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
|
||||||
public function getFilters()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
new TwigFilter('pastEvents', [$this, 'filterPastEvents']),
|
|
||||||
new TwigFilter('pastTalks', [$this, 'filterPastTalks']),
|
|
||||||
new TwigFilter('upcomingEvents', [$this, 'filterUpcomingEvents']),
|
|
||||||
new TwigFilter('upcomingTalks', [$this, 'filterUpcomingTalks']),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,49 +58,47 @@ class TalksExtension extends AbstractExtension
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filterUpcomingTalks(Collection $talks): Collection
|
public function getUpcomingTalks(array $talks): Collection
|
||||||
{
|
{
|
||||||
return $talks->filter(function ($talk): bool {
|
return $this->getAllTalks($talks)->filter(function ($talk): bool {
|
||||||
return $this->getLastDate($talk) >= $this->today;
|
return $this->getLastDate($talk) >= $this->today;
|
||||||
})->values();
|
})->values();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filterPastTalks(Collection $talks): Collection
|
public function getPastTalks(array $talks): Collection
|
||||||
{
|
{
|
||||||
return $talks->filter(function ($talk): bool {
|
return $this->getAllTalks($talks)->filter(function ($talk): bool {
|
||||||
return $this->getLastDate($talk) < $this->today;
|
return $this->getLastDate($talk) < $this->today;
|
||||||
})->values();
|
})->values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAllEvents($talks): Collection
|
||||||
|
{
|
||||||
|
return $this->eventsFromTalks($talks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPastEvents($talks): Collection
|
||||||
|
{
|
||||||
|
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
|
||||||
|
return $event['date'] < $this->today;
|
||||||
|
})->sortBy('date');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPastTalkCount($talks): int
|
||||||
|
{
|
||||||
|
return $this->getPastEvents($talks)->count();
|
||||||
|
}
|
||||||
|
|
||||||
private function getLastDate($talk): string
|
private function getLastDate($talk): string
|
||||||
{
|
{
|
||||||
return $this->eventsFromTalks(new Collection([$talk]))
|
return $this->eventsFromTalks(new Collection([$talk]))
|
||||||
->pluck('date')->max();
|
->pluck('date')->max();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filterUpcomingEvents($talks): Collection
|
|
||||||
{
|
|
||||||
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
|
|
||||||
return $event['date'] >= $this->today;
|
|
||||||
})->sortBy('date');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function filterPastEvents($talks): Collection
|
|
||||||
{
|
|
||||||
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
|
|
||||||
return $event['date'] < $this->today;
|
|
||||||
})->sortBy('date');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function eventsFromTalks($talks): Collection
|
private function eventsFromTalks($talks): Collection
|
||||||
{
|
{
|
||||||
return (new Collection($talks))->flatMap(function ($talk): array {
|
return (new Collection($talks))->flatMap(function ($talk): array {
|
||||||
return $talk['events'];
|
return $talk['events'];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllEvents($talks): Collection
|
|
||||||
{
|
|
||||||
return $this->eventsFromTalks($talks);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,43 +50,7 @@ class RetrievingEventsTest extends TestCase
|
||||||
];
|
];
|
||||||
|
|
||||||
$talks = $this->extension->getAllTalks([$talkA, $talkB]);
|
$talks = $this->extension->getAllTalks([$talkA, $talkB]);
|
||||||
$events = $this->extension->filterPastEvents($talks);
|
$events = $this->extension->getPastEvents($talks);
|
||||||
|
|
||||||
$this->assertInstanceOf(Collection::class, $talks);
|
|
||||||
$this->assertInstanceOf(Collection::class, $events);
|
|
||||||
|
|
||||||
$this->assertCount(2, $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->getAllTalks([$talkA, $talkB]);
|
|
||||||
$events = $this->extension->filterUpcomingEvents($talks);
|
|
||||||
|
|
||||||
$this->assertInstanceOf(Collection::class, $talks);
|
$this->assertInstanceOf(Collection::class, $talks);
|
||||||
$this->assertInstanceOf(Collection::class, $events);
|
$this->assertInstanceOf(Collection::class, $events);
|
||||||
|
|
|
@ -91,11 +91,9 @@ class RetrievingTalksTest extends TestCase
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$talks = $this->extension->getAllTalks([$pastTalk, $futureTalk]);
|
$talks = $this->extension->getPastTalks([$pastTalk, $futureTalk]);
|
||||||
$filtered = $this->extension->filterPastTalks($talks);
|
$this->assertCount(1, $talks);
|
||||||
|
$this->assertSame($pastTalk, $talks->first());
|
||||||
$this->assertCount(1, $filtered);
|
|
||||||
$this->assertSame($pastTalk, $filtered->first());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
@ -122,11 +120,9 @@ class RetrievingTalksTest extends TestCase
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$talks = $this->extension->getAllTalks([$pastTalk, $todayTalk, $futureTalk]);
|
$talks = $this->extension->getUpcomingTalks([$pastTalk, $todayTalk, $futureTalk]);
|
||||||
$filtered = $this->extension->filterUpcomingTalks($talks);
|
$this->assertSame(2, $talks->count());
|
||||||
|
$this->assertSame([$todayTalk, $futureTalk], $talks->toArray());
|
||||||
$this->assertSame(2, $filtered->count());
|
|
||||||
$this->assertSame([$todayTalk, $futureTalk], $filtered->toArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
@ -140,10 +136,8 @@ class RetrievingTalksTest extends TestCase
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$talks = $this->extension->getAllTalks([$talk]);
|
$this->assertCount(1, $this->extension->getUpcomingTalks([$talk]));
|
||||||
|
$this->assertEmpty($this->extension->getPastTalks([$talk]));
|
||||||
$this->assertCount(1, $this->extension->filterUpcomingTalks($talks));
|
|
||||||
$this->assertEmpty($this->extension->filterPastTalks($talks));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
|
@ -24,22 +24,22 @@ talks:
|
||||||
date: 2018-06-27
|
date: 2018-06-27
|
||||||
---
|
---
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% set talks = get_all_talks(page.talks|merge(data.talks)) %}
|
{% set talks = page.talks|merge(data.talks) %}
|
||||||
|
|
||||||
<header>
|
<header>
|
||||||
<p class="lead">
|
<p class="lead">
|
||||||
After giving my first talk in September 2012, I have now given {{ talks|pastEvents|length }} presentations at various conferences and meetups,
|
After giving my first talk in September 2012, I have now given {{ get_past_talk_count(talks) }} presentations at various conferences and meetups,
|
||||||
on topics including PHP, Drupal, Git, CSS and systems administration.
|
on topics including PHP, Drupal, Git, CSS and systems administration.
|
||||||
</p>
|
</p>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div class="spaced-y-10 mt-6">
|
<div class="spaced-y-10 mt-6">
|
||||||
{% include 'talks/upcoming' with {
|
{% include 'talks/upcoming' with {
|
||||||
talks: talks|upcomingTalks,
|
talks: get_upcoming_talks(talks),
|
||||||
} %}
|
} only %}
|
||||||
|
|
||||||
{% include 'talks/past' with {
|
{% include 'talks/past' with {
|
||||||
talks: talks|pastTalks,
|
talks: get_past_talks(talks),
|
||||||
} %}
|
} only %}
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Reference in a new issue