oliverdavies.uk/src/TwigExtension/TalkExtension.php

60 lines
1.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\TwigExtension;
2021-08-28 00:59:44 +01:00
use App\Collection\TalkCollection;
use Illuminate\Support\Collection;
2021-08-25 00:59:26 +01:00
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* @template TKey of array-key
* @template TValue
*/
2021-08-25 00:59:26 +01:00
final class TalkExtension extends AbstractExtension
{
2021-08-25 00:59:26 +01:00
public function getFunctions()
{
return [
new TwigFunction('get_last_event_date_for_talk', [$this, 'getLastEventDate']),
2021-08-25 00:59:26 +01:00
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
];
}
/**
* @param TValue $talk
*/
public function getLastEventDate($talk): ?string
{
return Collection::make($talk['events'])
->pluck('date')
->sort()
->last();
}
/**
* @param iterable<int, TValue> $talks
*/
2021-08-25 01:11:57 +01:00
public function getPastTalkCount(iterable $talks = []): int
{
return $this->getEventsFromTalks($talks)->count();
}
/**
* @param iterable<int, TValue> $talks
*
* @return TalkCollection<int, TValue>
*/
2021-08-28 00:59:44 +01:00
private function getEventsFromTalks(iterable $talks): TalkCollection
{
2021-08-28 00:59:44 +01:00
$talkCollection = new TalkCollection($talks);
2021-08-25 10:48:56 +01:00
2021-08-25 01:11:57 +01:00
return $talkCollection
2021-08-28 00:59:44 +01:00
->getEvents()
->onlyPastTalks();
}
}
2021-08-25 10:48:56 +01:00