Move custom app code
Move custom app code (the `src` and `tests` directories) into `app`.
This commit is contained in:
parent
b9830f8386
commit
10f0f7fd11
4 changed files with 2 additions and 2 deletions
113
app/src/TwigExtension/Talk/TalksExtension.php
Normal file
113
app/src/TwigExtension/Talk/TalksExtension.php
Normal file
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
|
||||
namespace App\TwigExtension\Talk;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class TalksExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var int The current date.
|
||||
*/
|
||||
private $today;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->today = (new \DateTime())
|
||||
->modify('today')
|
||||
->setTimezone(new \DateTimeZone('Europe/London'))
|
||||
->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('getTalks', [$this, 'getTalks']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('pastEvents', [$this, 'filterPastEvents']),
|
||||
new TwigFilter('pastTalks', [$this, 'filterPastTalks']),
|
||||
new TwigFilter('upcomingEvents', [$this, 'filterUpcomingEvents']),
|
||||
new TwigFilter('upcomingTalks', [$this, 'filterUpcomingTalks']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'app.talks';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all upcoming and previous talks.
|
||||
*
|
||||
* @param ProxySourceCollection|array $talks All talk nodes.
|
||||
*
|
||||
* @return Collection A sorted collection of talks.
|
||||
*/
|
||||
public function getTalks($talks): Collection
|
||||
{
|
||||
return (new Collection($talks))->sortBy(function ($talk) {
|
||||
return $this->getLastDate($talk);
|
||||
});
|
||||
}
|
||||
|
||||
public function filterUpcomingTalks(Collection $talks): Collection
|
||||
{
|
||||
return $talks->filter(function ($talk): bool {
|
||||
return $this->getLastDate($talk) >= $this->today;
|
||||
})->values();
|
||||
}
|
||||
|
||||
public function filterPastTalks(Collection $talks): Collection
|
||||
{
|
||||
return $talks->filter(function ($talk): bool {
|
||||
return $this->getLastDate($talk) < $this->today;
|
||||
})->values();
|
||||
}
|
||||
|
||||
private function getLastDate($talk): string
|
||||
{
|
||||
return $this->eventsFromTalks(new Collection([$talk]))
|
||||
->pluck('date')->max();
|
||||
}
|
||||
|
||||
public function filterUpcomingEvents($talks): Collection
|
||||
{
|
||||
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
|
||||
return $event['date'] >= $this->today;
|
||||
})->sortBy('date');
|
||||
}
|
||||
|
||||
public function filterPastEvents($talks): Collection
|
||||
{
|
||||
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
|
||||
return $event['date'] < $this->today;
|
||||
})->sortBy('date');
|
||||
}
|
||||
|
||||
private function eventsFromTalks($talks): Collection
|
||||
{
|
||||
return (new Collection($talks))->flatMap(function ($talk): array {
|
||||
return $talk['events'];
|
||||
});
|
||||
}
|
||||
|
||||
public function getAllEvents($talks): Collection
|
||||
{
|
||||
return $this->eventsFromTalks($talks);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue