Use a Twig extension for counting past talks
This commit is contained in:
parent
9b19d7a036
commit
6888675252
5 changed files with 270 additions and 5 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Opdavies\TwigExtension;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
|
@ -10,6 +11,7 @@ class OpdaviesTwigExtension extends AbstractExtension
|
|||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
|
||||
new TwigFunction('get_years_of_experience', [$this, 'getYearsOfExperience']),
|
||||
];
|
||||
}
|
||||
|
@ -19,6 +21,22 @@ class OpdaviesTwigExtension extends AbstractExtension
|
|||
return 'app.opdavies_twig_extension';
|
||||
}
|
||||
|
||||
public function getPastTalkCount(array $talks): int
|
||||
{
|
||||
$today = (new \DateTime())->getTimestamp();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public function getYearsOfExperience(): int
|
||||
{
|
||||
return (new \DateTimeImmutable())->format('Y') - 2007;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue