2017-10-28 00:31:21 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-23 17:14:19 +00:00
|
|
|
namespace FormatTalksBundle\TwigExtension;
|
2017-10-28 00:31:21 +01:00
|
|
|
|
2017-10-29 20:10:04 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2017-11-14 18:44:58 +00:00
|
|
|
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
|
2017-10-29 20:10:04 +00:00
|
|
|
use Twig_Extension;
|
2017-11-14 18:44:58 +00:00
|
|
|
use Twig_SimpleFunction;
|
2017-10-29 20:10:04 +00:00
|
|
|
|
|
|
|
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 18:44:58 +00:00
|
|
|
new Twig_SimpleFunction('getAllTalks', [$this, 'getAll']),
|
|
|
|
new Twig_SimpleFunction('getUpcomingTalks', [$this, 'getUpcoming']),
|
|
|
|
new Twig_SimpleFunction('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.
|
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @param ProxySourceCollection|array $talks All talk nodes.
|
|
|
|
* @param array $eventData Shared event data.
|
2017-10-29 20:10:04 +00:00
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @return Collection A sorted collection of talks.
|
2017-10-29 20:10:04 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @param ProxySourceCollection|array $talks All talk nodes.
|
|
|
|
* @param array $eventData Shared event data.
|
2017-10-29 20:10:04 +00:00
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @return Collection A sorted collection of talks.
|
2017-10-29 20:10:04 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @param ProxySourceCollection|array $talks All talk nodes.
|
|
|
|
* @param array $eventData Shared event data.
|
2017-10-29 20:10:04 +00:00
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @return Collection A sorted collection of talks.
|
2017-10-29 20:10:04 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @param ProxySourceCollection|array $talks All talk nodes.
|
|
|
|
* @param array $eventData Shared event data.
|
2017-10-29 20:10:04 +00:00
|
|
|
*
|
2017-11-14 18:44:58 +00:00
|
|
|
* @return Collection The combined event and talk data.
|
2017-10-29 20:10:04 +00:00
|
|
|
*/
|
2017-11-14 18:44:58 +00:00
|
|
|
public function format($talks, array $eventData)
|
2017-10-29 20:10:04 +00:00
|
|
|
{
|
2017-11-14 18:44:58 +00:00
|
|
|
$eventData = collect($eventData);
|
2017-10-29 20:10:04 +00:00
|
|
|
|
2017-11-14 18:44:58 +00:00
|
|
|
return collect($talks)->flatMap(function ($talk) use ($eventData) {
|
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'])
|
2017-11-14 18:44:58 +00:00
|
|
|
->map(function ($event) use ($talk, $eventData) {
|
2017-11-14 18:26:01 +00:00
|
|
|
$event = collect($event);
|
2017-11-14 18:44:58 +00:00
|
|
|
$event = $event->merge($eventData->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 09:08:34 +01:00
|
|
|
|
2017-10-28 00:31:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'format_talks';
|
|
|
|
}
|
|
|
|
}
|