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

92 lines
2.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.
*
* @return Collection A sorted collection of talks.
*/
2019-01-18 23:11:08 +00:00
public function getAll($talks): Collection
2018-05-09 23:24:08 +01:00
{
return collect($talks)->sortBy(function ($talk) {
return $this->getLastDate($talk);
});
2018-05-09 23:24:08 +01:00
}
/**
* Get all upcoming talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
2019-01-18 23:11:08 +00:00
public function getUpcoming($talks): Collection
2018-05-09 23:24:08 +01:00
{
return $this->getAll($talks)->filter(function ($talk) {
return $this->getLastDate($talk) >= $this->today;
})->values();
2018-05-09 23:24:08 +01:00
}
/**
* Get all past talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
2019-01-18 23:11:08 +00:00
public function getPast($talks): Collection
2018-05-09 23:24:08 +01:00
{
return $this->getAll($talks)->filter(function ($talk) {
return $this->getLastDate($talk) < $this->today;
})->values();
2018-05-09 23:24:08 +01:00
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'talks';
2018-05-09 23:24:08 +01:00
}
private function getLastDate($talk): string
{
return (string) collect($talk['events'])->pluck('date')->sort()->last();
}
2018-05-09 23:24:08 +01:00
}