oliverdavies.uk/src/FormatTalksBundle/Twig/FormatTalksExtension.php

54 lines
1.5 KiB
PHP
Raw Normal View History

2017-10-28 00:31:21 +01:00
<?php
2017-10-28 12:17:09 +01:00
namespace FormatTalksBundle\Twig;
2017-10-28 00:31:21 +01:00
2017-10-28 12:17:09 +01:00
class FormatTalksExtension extends \Twig_Extension
2017-10-28 00:31:21 +01:00
{
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
2017-10-28 12:17:09 +01:00
new \Twig_SimpleFilter('format_talks', [$this, 'formatTalks']),
2017-10-28 00:31:21 +01:00
];
}
public function formatTalks($data, $onlyUpcoming = false, $onlyPrevious = false)
2017-10-28 00:31:21 +01:00
{
2017-10-28 13:16:35 +01:00
$events = collect($data['events']);
2017-10-28 02:28:44 +01:00
$today = (new \DateTime())->format('Y-m-d');
2017-10-28 14:03:29 +01:00
return collect($data['talks'])->flatMap(function ($talk) use ($events) {
2017-10-28 13:16:35 +01:00
// 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).
return collect($talk['events'])->map(function ($event) use ($talk, $events) {
$event = collect($event);
$event = $event->merge($events->get($event->get('event')));
return compact('event', 'talk');
});
2017-10-28 14:03:29 +01:00
})->filter(function ($talk) use ($today, $onlyPrevious, $onlyUpcoming) {
2017-10-28 13:16:35 +01:00
if ($onlyUpcoming) {
return $talk['event']['date'] > $today;
}
2017-10-28 13:16:35 +01:00
if ($onlyPrevious) {
return $talk['event']['date'] < $today;
}
2017-10-28 13:16:35 +01:00
return true;
})->sortByDesc('event.date')->all();
2017-10-28 00:31:21 +01:00
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'format_talks';
}
}