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;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Collection\TalkCollection;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
|
@ -23,15 +22,13 @@ final class TalkExtension extends AbstractExtension
|
|||
return $this->getEventsFromTalks($talks)->count();
|
||||
}
|
||||
|
||||
private function getEventsFromTalks(iterable $talks): Collection
|
||||
private function getEventsFromTalks(iterable $talks): TalkCollection
|
||||
{
|
||||
$talkCollection = new Collection($talks);
|
||||
|
||||
$today = Carbon::today()->format('Y-m-d');
|
||||
$talkCollection = new TalkCollection($talks);
|
||||
|
||||
return $talkCollection
|
||||
->flatMap(fn($talk): array => (array) $talk['events'])
|
||||
->filter(fn(array $event): bool => $event['date'] < $today);
|
||||
->getEvents()
|
||||
->onlyPastTalks();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue