2021-08-23 12:00:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\TwigExtension;
|
|
|
|
|
2021-08-25 10:48:56 +01:00
|
|
|
use Carbon\Carbon;
|
2021-08-23 12:00:00 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2021-08-25 00:59:26 +01:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFunction;
|
2021-08-23 12:00:00 +01:00
|
|
|
|
2021-08-25 00:59:26 +01:00
|
|
|
final class TalkExtension extends AbstractExtension
|
2021-08-23 12:00:00 +01:00
|
|
|
{
|
2021-08-25 00:59:26 +01:00
|
|
|
public function getFunctions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-08-25 01:11:57 +01:00
|
|
|
public function getPastTalkCount(iterable $talks = []): int
|
|
|
|
{
|
|
|
|
return $this->getEventsFromTalks($talks)->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getEventsFromTalks(iterable $talks): Collection
|
2021-08-23 12:00:00 +01:00
|
|
|
{
|
|
|
|
$talkCollection = new Collection($talks);
|
|
|
|
|
2021-08-25 10:48:56 +01:00
|
|
|
$today = Carbon::today()->format('Y-m-d');
|
|
|
|
|
2021-08-25 01:11:57 +01:00
|
|
|
return $talkCollection
|
2021-08-25 10:48:56 +01:00
|
|
|
->flatMap(fn($talk): array => (array) $talk['events'])
|
|
|
|
->filter(fn(array $event): bool => $event['date'] < $today);
|
2021-08-23 12:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 10:48:56 +01:00
|
|
|
|