2018-05-09 23:24:08 +01:00
|
|
|
<?php
|
|
|
|
|
2019-01-16 21:52:54 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-16 21:52:54 +00:00
|
|
|
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']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:27:50 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2019-01-18 20:38:58 +00:00
|
|
|
return collect($talks)->sortBy(function ($talk) {
|
2019-01-18 21:55:37 +00:00
|
|
|
return $this->getLastDate($talk);
|
2019-01-18 20:38:58 +00:00
|
|
|
});
|
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
|
|
|
{
|
2019-01-18 21:44:21 +00:00
|
|
|
return $this->getAll($talks)->filter(function ($talk) {
|
2019-01-18 21:55:37 +00:00
|
|
|
return $this->getLastDate($talk) >= $this->today;
|
2019-01-18 21:44:21 +00:00
|
|
|
})->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
|
|
|
{
|
2019-01-18 21:27:03 +00:00
|
|
|
return $this->getAll($talks)->filter(function ($talk) {
|
2019-01-18 21:55:37 +00:00
|
|
|
return $this->getLastDate($talk) < $this->today;
|
2019-01-18 21:44:21 +00:00
|
|
|
})->values();
|
2018-05-09 23:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
2019-01-16 21:52:54 +00:00
|
|
|
return 'talks';
|
2018-05-09 23:24:08 +01:00
|
|
|
}
|
2019-01-18 21:55:37 +00:00
|
|
|
|
|
|
|
private function getLastDate($talk): string
|
|
|
|
{
|
|
|
|
return (string) collect($talk['events'])->pluck('date')->sort()->last();
|
|
|
|
}
|
2018-05-09 23:24:08 +01:00
|
|
|
}
|