wip
This commit is contained in:
parent
66667bcb5b
commit
4fa5cd767d
38 changed files with 996 additions and 76 deletions
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace FormatTalksBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
|
||||
class SculpinFormatTalksExtension extends Extension
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
|
||||
$loader->load('services.yml');
|
||||
}
|
||||
}
|
5
src/FormatTalksBundle/Resources/config/services.yml
Normal file
5
src/FormatTalksBundle/Resources/config/services.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
services:
|
||||
twig.format_talks:
|
||||
class: 'FormatTalksBundle\TwigExtension\FormatTalksExtension'
|
||||
tags:
|
||||
- { name: twig.extension }
|
9
src/FormatTalksBundle/SculpinFormatTalksBundle.php
Normal file
9
src/FormatTalksBundle/SculpinFormatTalksBundle.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace FormatTalksBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class SculpinFormatTalksBundle extends Bundle
|
||||
{
|
||||
}
|
114
src/FormatTalksBundle/TwigExtension/FormatTalksExtension.php
Normal file
114
src/FormatTalksBundle/TwigExtension/FormatTalksExtension.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
namespace FormatTalksBundle\TwigExtension;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
|
||||
use Twig_Extension;
|
||||
use Twig_SimpleFunction;
|
||||
|
||||
class FormatTalksExtension extends Twig_Extension
|
||||
{
|
||||
/**
|
||||
* @var string The current date.
|
||||
*/
|
||||
private $today;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->today = (new \DateTime())->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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 'format_talks';
|
||||
}
|
||||
}
|
5
src/FormatTalksBundle/composer.json
Normal file
5
src/FormatTalksBundle/composer.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"require": {
|
||||
"tightenco/collect": "^5.4"
|
||||
}
|
||||
}
|
8
src/helpers.php
Normal file
8
src/helpers.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
function tap($value, $callback)
|
||||
{
|
||||
$callback($value);
|
||||
|
||||
return $value;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue