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

114 lines
2.8 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-29 20:10:04 +00:00
use Illuminate\Support\Collection;
2017-11-14 13:43:46 +00:00
use Twig\TwigFunction;
2017-10-29 20:10:04 +00:00
use Twig_Extension;
class FormatTalksExtension extends Twig_Extension
2017-10-28 00:31:21 +01:00
{
2017-10-29 21:55:38 +00:00
/**
* @var string The current date.
*/
private $today;
2017-11-01 17:24:23 +00:00
public function __construct()
{
$this->today = (new \DateTime())->format('Y-m-d');
}
2017-10-28 00:31:21 +01:00
/**
* {@inheritdoc}
*/
2017-11-14 13:43:46 +00:00
public function getFunctions()
2017-10-28 00:31:21 +01:00
{
return [
2017-11-14 13:43:46 +00:00
new TwigFunction('getAllTalks', [$this, 'getAll']),
new TwigFunction('getUpcomingTalks', [$this, 'getUpcoming']),
new TwigFunction('getPastTalks', [$this, 'getPast']),
2017-10-28 00:31:21 +01:00
];
}
2017-10-29 20:10:04 +00:00
/**
* Get all upcoming and previous talks.
*
* Used to display the talk table on a specific talk page.
*
* @param array $data An associative array of talk and event data.
*
* @return array
*/
2017-11-14 13:43:46 +00:00
public function getAll($talks, array $eventData = [])
2017-11-02 07:40:34 +00:00
{
2017-11-14 13:43:46 +00:00
return $this->format($talks, $eventData)->sortBy('event.date');
2017-10-29 20:10:04 +00:00
}
/**
* Get all upcoming talks.
*
* Used on the main talks page.
*
* @return array
*/
2017-11-14 13:43:46 +00:00
public function getUpcoming($talks, array $eventData = [])
2017-11-02 07:40:34 +00:00
{
2017-11-14 18:20:27 +00:00
return $this->format($talks, $eventData)
2017-11-14 18:26:01 +00:00
->filter(function ($talk) {
return $talk['event']['date'] >= $this->today;
})
->sortBy('event.date');
2017-10-29 20:10:04 +00:00
}
/**
* Get all past talks.
*
* Used on the main talks page and the talks archive.
*
* @param array $data The talk and event data.
*
* @return array
*/
2017-11-14 13:43:46 +00:00
public function getPast($talks, array $eventData = [])
2017-11-02 07:40:34 +00:00
{
2017-11-14 13:43:46 +00:00
return $this->format($talks, $eventData)
2017-11-14 18:26:01 +00:00
->filter(function ($talk) {
return $talk['event']['date'] < $this->today;
})
->sortByDesc('event.date');
2017-10-29 20:10:04 +00:00
}
/**
* Format the talk data into the required format.
*
* @param array $data The talk and event data.
*
* @return Collection The event and talk data.
*/
2017-11-14 13:43:46 +00:00
public function format($talks, array $event_data)
2017-10-29 20:10:04 +00:00
{
2017-11-14 13:43:46 +00:00
$event_data = collect($event_data);
2017-10-29 20:10:04 +00:00
2017-11-14 13:43:46 +00:00
return collect($talks)->flatMap(function ($talk) use ($event_data) {
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).
2017-11-14 18:26:01 +00:00
return collect($talk['events'])
->map(function ($event) use ($talk, $event_data) {
$event = collect($event);
$event = $event->merge($event_data->get($event->get('event')))->all();
2017-10-28 13:16:35 +01:00
2017-11-14 18:26:01 +00:00
return compact('event', 'talk');
});
2017-10-29 20:10:04 +00:00
});
}
2017-10-28 00:31:21 +01:00
/**
* {@inheritdoc}
*/
public function getName()
{
return 'format_talks';
}
}