2024-05-03 11:30:24 +00:00
|
|
|
<?php
|
|
|
|
|
2024-08-02 17:35:36 +00:00
|
|
|
namespace Modules\Talk\TwigExtension;
|
2024-05-03 11:30:24 +00:00
|
|
|
|
2024-05-03 11:53:05 +00:00
|
|
|
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
2024-05-03 11:30:24 +00:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFunction;
|
|
|
|
|
2024-08-02 17:35:36 +00:00
|
|
|
class TalkTwigExtension extends AbstractExtension
|
2024-05-03 11:30:24 +00:00
|
|
|
{
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
return [
|
2024-05-03 11:53:05 +00:00
|
|
|
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
|
2024-05-03 11:30:24 +00:00
|
|
|
new TwigFunction('get_years_of_experience', [$this, 'getYearsOfExperience']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return 'app.opdavies_twig_extension';
|
|
|
|
}
|
|
|
|
|
2024-05-03 11:53:05 +00:00
|
|
|
public function getPastTalkCount(array $talks): int
|
|
|
|
{
|
2024-08-01 20:08:20 +00:00
|
|
|
$today = (new \DateTime('today'))->getTimestamp();
|
2024-05-03 11:53:05 +00:00
|
|
|
|
|
|
|
return collect($talks)
|
|
|
|
->flatMap(fn (ProxySourceItem $talk) => $talk->data()->get('events'))
|
|
|
|
->filter(
|
|
|
|
function (array $event) use ($today): bool {
|
|
|
|
assert(array_key_exists(array: $event, key: 'date'));
|
|
|
|
|
|
|
|
return $event['date'] < $today;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
2024-05-03 11:30:24 +00:00
|
|
|
public function getYearsOfExperience(): int
|
|
|
|
{
|
|
|
|
return (new \DateTimeImmutable())->format('Y') - 2007;
|
|
|
|
}
|
|
|
|
}
|