Merge branch 'use-twig-functions'

This commit is contained in:
Oliver Davies 2017-11-14 13:44:06 +00:00
commit 518fe175b8
5 changed files with 44 additions and 56 deletions

View file

@ -2,7 +2,7 @@
<h2>Events</h2>
{% include "talks-table" with {
talks: { talks: [page], event_data: site.events }|all_talks,
talks: getAllTalks([page], site.events),
talk_page: true
} %}
</div>

View file

@ -13,8 +13,6 @@ talks:
---
<p>Here are a list of my previous conference and user group talks:</p>
{% include "talks-table" with {
talks: { talks: data.talks|merge(page.talks), event_data: site.events }|past_talks
} %}
{% include "talks-table" with { talks: getPastTalks(data.talks, site.events) } %}
<p>Upcoming talks can be found on the <a href="{{ site.url }}/talks">talks page</a>.</p>

View file

@ -10,7 +10,7 @@ use: [talks]
<h2>Upcoming Talks</h2>
{% set upcoming_talks = { talks: data.talks, event_data: site.events }|upcoming_talks %}
{% set upcoming_talks = getUpcomingTalks(data.talks, site.events) %}
{% if upcoming_talks %}
{% include "talks-table" with { talks: upcoming_talks, upcoming: true } %}
{% else %}
@ -19,9 +19,7 @@ use: [talks]
<h2>Last 5 Talks</h2>
{% include "talks-table" with {
talks: { talks: data.talks, event_data: site.events }|past_talks|slice(0,5)
} %}
{% include "talks-table" with { talks: getPastTalks(data.talks, site.events)|slice(0,5) } %}
<p>All previous talks can be found in the <a href="{{ site.url }}/talks/archive">talks archive</a>.</p>
{% endblock %}

View file

@ -3,8 +3,8 @@
namespace FormatTalksBundle\Twig;
use Illuminate\Support\Collection;
use Twig\TwigFunction;
use Twig_Extension;
use Twig_SimpleFilter;
class FormatTalksExtension extends Twig_Extension
{
@ -21,12 +21,12 @@ class FormatTalksExtension extends Twig_Extension
/**
* {@inheritdoc}
*/
public function getFilters()
public function getFunctions()
{
return [
new Twig_SimpleFilter('all_talks', [$this, 'getAll']),
new Twig_SimpleFilter('upcoming_talks', [$this, 'getUpcoming']),
new Twig_SimpleFilter('past_talks', [$this, 'getPast']),
new TwigFunction('getAllTalks', [$this, 'getAll']),
new TwigFunction('getUpcomingTalks', [$this, 'getUpcoming']),
new TwigFunction('getPastTalks', [$this, 'getPast']),
];
}
@ -39,9 +39,9 @@ class FormatTalksExtension extends Twig_Extension
*
* @return array
*/
public function getAll(array $data)
public function getAll($talks, array $eventData = [])
{
return $this->format($data)->sortBy('event.date');
return $this->format($talks, $eventData)->sortBy('event.date');
}
/**
@ -49,15 +49,15 @@ class FormatTalksExtension extends Twig_Extension
*
* Used on the main talks page.
*
* @param array $data The talk and event data.
*
* @return array
*/
public function getUpcoming(array $data)
public function getUpcoming($talks, array $eventData = [])
{
return $this->format($data)->filter(function ($talk) {
return $talk['event']['date'] >= $this->today;
})->sortBy('event.date');
return $this->format($talks, $eventData)
->filter(function ($talk) {
return $talk['event']['date'] >= $this->today;
})
->sortBy('event.date');
}
/**
@ -69,11 +69,13 @@ class FormatTalksExtension extends Twig_Extension
*
* @return array
*/
public function getPast(array $data)
public function getPast($talks, array $eventData = [])
{
return $this->format($data)->filter(function ($talk) {
return $talk['event']['date'] < $this->today;
})->sortByDesc('event.date');
return $this->format($talks, $eventData)
->filter(function ($talk) {
return $talk['event']['date'] < $this->today;
})
->sortByDesc('event.date');
}
/**
@ -83,11 +85,11 @@ class FormatTalksExtension extends Twig_Extension
*
* @return Collection The event and talk data.
*/
public function format(array $data)
public function format($talks, array $event_data)
{
$event_data = collect($data['event_data']);
$event_data = collect($event_data);
return collect($data['talks'])->flatMap(function ($talk) use ($event_data) {
return collect($talks)->flatMap(function ($talk) use ($event_data) {
// Build an associative array with the talk, as well as the
// specified event data (e.g. date and time) as well as the shared
// event data (e.g. event name and website).

View file

@ -27,8 +27,7 @@ class FormatTalksTest extends TestCase
*/
public function testFormat()
{
$data = [
'event_data' => [
$event_data = [
'event-a' => [
'name' => 'Event A',
'location' => 'Somewhere',
@ -39,8 +38,9 @@ class FormatTalksTest extends TestCase
'location' => 'Somewhere else',
'website' => 'http://event-b.com',
],
],
'talks' => [
];
$talks = [
[
'title' => 'Talk A',
'events' => [
@ -54,10 +54,9 @@ class FormatTalksTest extends TestCase
['event' => 'event-b', 'date' => '2018-01-31', 'time' => '17:00'],
],
],
],
];
$results = $this->extension->format($data)->all();
$results = $this->extension->format($talks, $event_data)->all();
$this->assertCount(3, $results);
@ -107,15 +106,12 @@ class FormatTalksTest extends TestCase
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
$data = [
'event_data' => [],
'talks' => [
['events' => [$eventA, $eventB]],
['events' => [$eventC]],
],
$talks = [
['events' => [$eventA, $eventB]],
['events' => [$eventC]],
];
$results = $this->extension->getAll($data);
$results = $this->extension->getAll($talks);
$this->assertCount(3, $results);
@ -137,15 +133,12 @@ class FormatTalksTest extends TestCase
$eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')];
$eventE = ['date' => (new DateTime('+2 weeks'))->format('Y-m-d')];
$data = [
'event_data' => [],
'talks' => [
['events' => [$eventA, $eventC]],
['events' => [$eventB, $eventE]],
],
$talks = [
['events' => [$eventA, $eventC]],
['events' => [$eventB, $eventE]],
];
$results = $this->extension->getUpcoming($data);
$results = $this->extension->getUpcoming($talks);
$this->assertCount(3, $results);
@ -168,16 +161,13 @@ class FormatTalksTest extends TestCase
$eventE = ['date' => (new DateTime('-2 days'))->format('Y-m-d')];
$eventF = ['date' => (new DateTime('-2 months'))->format('Y-m-d')];
$data = [
'event_data' => [],
'talks' => [
['events' => [$eventD]],
['events' => [$eventA, $eventB, $eventC]],
['events' => [$eventF]],
],
$talks = [
['events' => [$eventD]],
['events' => [$eventA, $eventB, $eventC]],
['events' => [$eventF]],
];
$results = $this->extension->getPast($data);
$results = $this->extension->getPast($talks);
$this->assertCount(2, $results);