refactor: Extract a TalkCollection
This commit is contained in:
parent
ac552fb05b
commit
be71d9f255
27
src/Collection/TalkCollection.php
Normal file
27
src/Collection/TalkCollection.php
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Collection;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
final class TalkCollection extends Collection
|
||||||
|
{
|
||||||
|
private const DATE_FORMAT = 'Y-m-d';
|
||||||
|
private const KEY_EVENTS = 'events';
|
||||||
|
private const KEY_EVENT_DATE = 'date';
|
||||||
|
|
||||||
|
public function getEvents(): self
|
||||||
|
{
|
||||||
|
return $this->flatMap(fn($talk): array => (array) $talk[self::KEY_EVENTS]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onlyPastTalks(): self
|
||||||
|
{
|
||||||
|
$today = Carbon::today()->format(self::DATE_FORMAT);
|
||||||
|
|
||||||
|
return $this->filter(fn(array $event): bool => $event[self::KEY_EVENT_DATE] < $today);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\TwigExtension;
|
namespace App\TwigExtension;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use App\Collection\TalkCollection;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
|
@ -23,15 +22,13 @@ final class TalkExtension extends AbstractExtension
|
||||||
return $this->getEventsFromTalks($talks)->count();
|
return $this->getEventsFromTalks($talks)->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getEventsFromTalks(iterable $talks): Collection
|
private function getEventsFromTalks(iterable $talks): TalkCollection
|
||||||
{
|
{
|
||||||
$talkCollection = new Collection($talks);
|
$talkCollection = new TalkCollection($talks);
|
||||||
|
|
||||||
$today = Carbon::today()->format('Y-m-d');
|
|
||||||
|
|
||||||
return $talkCollection
|
return $talkCollection
|
||||||
->flatMap(fn($talk): array => (array) $talk['events'])
|
->getEvents()
|
||||||
->filter(fn(array $event): bool => $event['date'] < $today);
|
->onlyPastTalks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue