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

114 lines
2.9 KiB
PHP
Raw Normal View History

2018-05-09 23:24:08 +01:00
<?php
namespace App\TwigExtension\Talk;
2018-05-09 23:24:08 +01:00
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
2019-05-20 00:58:36 +01:00
use Tightenco\Collect\Support\Collection;
use Twig\Extension\AbstractExtension;
2019-05-20 00:58:36 +01:00
use Twig\TwigFilter;
use Twig\TwigFunction;
2018-05-09 23:24:08 +01:00
class TalksExtension extends AbstractExtension
2018-05-09 23:24:08 +01:00
{
/**
2019-06-26 01:03:00 +01:00
* @var int The current date.
2018-05-09 23:24:08 +01:00
*/
private $today;
public function __construct()
{
2018-08-01 00:27:13 +01:00
$this->today = (new \DateTime())
2019-06-28 21:45:40 +01:00
->modify('today')
->setTimezone(new \DateTimeZone('Europe/London'))
->getTimestamp();
2018-05-09 23:24:08 +01:00
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
2019-05-20 02:00:00 +01:00
new TwigFunction('getTalks', [$this, 'getTalks']),
2018-05-09 23:24:08 +01:00
];
}
2019-05-20 00:58:36 +01:00
public function getFilters()
{
return [
new TwigFilter('pastEvents', [$this, 'filterPastEvents']),
new TwigFilter('pastTalks', [$this, 'filterPastTalks']),
new TwigFilter('upcomingEvents', [$this, 'filterUpcomingEvents']),
new TwigFilter('upcomingTalks', [$this, 'filterUpcomingTalks']),
2019-05-20 00:58:36 +01:00
];
}
/**
2019-05-20 02:00:00 +01:00
* {@inheritdoc}
2019-03-26 21:58:35 +00:00
*/
2019-05-20 02:00:00 +01:00
public function getName()
2018-05-09 23:24:08 +01:00
{
2019-05-20 02:00:00 +01:00
return 'app.talks';
2018-05-09 23:24:08 +01:00
}
/**
2019-05-20 02:00:00 +01:00
* Get all upcoming and previous talks.
2018-05-09 23:24:08 +01:00
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
2019-05-20 02:00:00 +01:00
public function getTalks($talks): Collection
2018-05-09 23:24:08 +01:00
{
return (new Collection($talks))->sortBy(function ($talk) {
2019-05-20 02:00:00 +01:00
return $this->getLastDate($talk);
});
2018-05-09 23:24:08 +01:00
}
2019-09-27 01:51:32 +01:00
public function filterUpcomingTalks(Collection $talks): Collection
2019-05-20 01:58:36 +01:00
{
return $talks->filter(function ($talk): bool {
2019-05-20 01:58:36 +01:00
return $this->getLastDate($talk) >= $this->today;
2019-09-27 01:51:32 +01:00
})->values();
2019-05-20 01:58:36 +01:00
}
2019-09-27 01:51:32 +01:00
public function filterPastTalks(Collection $talks): Collection
2019-05-20 01:58:36 +01:00
{
return $talks->filter(function ($talk): bool {
2019-05-20 01:58:36 +01:00
return $this->getLastDate($talk) < $this->today;
2019-09-27 01:51:32 +01:00
})->values();
2019-05-20 01:58:36 +01:00
}
private function getLastDate($talk): string
{
return $this->eventsFromTalks(new Collection([$talk]))
2019-05-20 01:58:36 +01:00
->pluck('date')->max();
}
2019-09-27 01:51:32 +01:00
public function filterUpcomingEvents($talks): Collection
{
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
return $event['date'] >= $this->today;
2019-09-27 01:51:32 +01:00
})->sortBy('date');
}
2019-09-27 01:51:32 +01:00
public function filterPastEvents($talks): Collection
{
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
return $event['date'] < $this->today;
2019-09-27 01:51:32 +01:00
})->sortBy('date');
}
private function eventsFromTalks($talks): Collection
{
return (new Collection($talks))->flatMap(function ($talk): array {
return $talk['events'];
});
}
2019-05-20 20:36:23 +01:00
2019-09-27 01:51:32 +01:00
public function getAllEvents($talks): Collection
2019-05-20 20:36:23 +01:00
{
2019-09-27 01:51:32 +01:00
return $this->eventsFromTalks($talks);
2019-05-20 20:36:23 +01:00
}
2018-05-09 23:24:08 +01:00
}