oliverdavies.uk/src/TalksBundle/TwigExtension/TalksExtension.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2018-05-09 23:24:08 +01:00
<?php
namespace TalksBundle\TwigExtension;
2018-05-09 23:24:08 +01:00
use Illuminate\Support\Collection;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
use Twig_Extension;
use Twig_SimpleFunction;
class TalksExtension extends Twig_Extension
2018-05-09 23:24:08 +01:00
{
/**
* @var string The current date.
*/
private $today;
public function __construct()
{
2018-08-01 00:27:13 +01:00
$this->today = (new \DateTime())
2018-08-31 00:33:34 +01:00
->modify('today')
2018-08-01 00:27:13 +01:00
->setTimezone(new \DateTimeZone('Europe/London'))
2019-02-13 01:10:14 +00:00
->getTimestamp();
2018-05-09 23:24:08 +01:00
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new Twig_SimpleFunction('getAllTalks', [$this, 'getAll']),
new Twig_SimpleFunction('getUpcomingTalks', [$this, 'getUpcoming']),
new Twig_SimpleFunction('getPastTalks', [$this, 'getPast']),
];
}
/**
* Get all upcoming and previous talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
* @param array $eventData Shared event data.
*
* @return Collection A sorted collection of talks.
*/
public function getAll($talks, array $eventData = [])
{
return $this->format($talks, $eventData)->sortBy('event.date');
}
/**
* Get all upcoming talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
* @param array $eventData Shared event data.
*
* @return Collection A sorted collection of talks.
*/
public function getUpcoming($talks, array $eventData = [])
{
return $this->format($talks, $eventData)
->filter(function ($talk) {
return $talk['event']['date'] >= $this->today;
})
->sortBy('event.date');
}
/**
* Get all past talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
* @param array $eventData Shared event data.
*
* @return Collection A sorted collection of talks.
*/
public function getPast($talks, array $eventData = [])
{
return $this->format($talks, $eventData)
->filter(function ($talk) {
return $talk['event']['date'] < $this->today;
})
->sortByDesc('event.date');
}
/**
* Format the talk data into the required format.
*
* @param ProxySourceCollection|array $talks All talk nodes.
* @param array $eventData Shared event data.
*
* @return Collection The combined event and talk data.
*/
public function format($talks, array $eventData)
{
$eventData = collect($eventData);
return collect($talks)->flatMap(function ($talk) use ($eventData) {
// 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, $eventData) {
$event = collect($event);
$event = $event->merge($eventData->get($event->get('event')))->all();
return compact('event', 'talk');
});
});
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'talks';
2018-05-09 23:24:08 +01:00
}
}