Refactor, ensure each event is an array

This commit is contained in:
Oliver Davies 2019-01-18 21:55:37 +00:00
parent fddaa56ed2
commit 2334edf692
2 changed files with 16 additions and 10 deletions

View file

@ -47,7 +47,7 @@ class TalksExtension extends Twig_Extension
public function getAll($talks, array $eventData = []): Collection
{
return collect($talks)->sortBy(function ($talk) {
return collect($talk['events'])->pluck('date')->sort()->last();
return $this->getLastDate($talk);
});
}
@ -59,10 +59,10 @@ class TalksExtension extends Twig_Extension
*
* @return Collection A sorted collection of talks.
*/
public function getUpcoming($talks, array $eventData = [])
public function getUpcoming($talks, array $eventData = []): Collection
{
return $this->getAll($talks)->filter(function ($talk) {
return collect($talk['events'])->pluck('date')->sort()->last() >= $this->today;
return $this->getLastDate($talk) >= $this->today;
})->values();
}
@ -74,10 +74,10 @@ class TalksExtension extends Twig_Extension
*
* @return Collection A sorted collection of talks.
*/
public function getPast($talks, array $eventData = [])
public function getPast($talks, array $eventData = []): Collection
{
return $this->getAll($talks)->filter(function ($talk) {
return collect($talk['events'])->pluck('date')->sort()->last() < $this->today;
return $this->getLastDate($talk) < $this->today;
})->values();
}
@ -99,6 +99,7 @@ class TalksExtension extends Twig_Extension
// event data (e.g. event name and website).
return collect($talk['events'])
->map(function ($event) use ($talk, $eventData) {
$event = collect($event);
$event = $event->merge($eventData->get($event->get('event')))->all();
@ -114,4 +115,9 @@ class TalksExtension extends Twig_Extension
{
return 'talks';
}
private function getLastDate($talk): string
{
return (string) collect($talk['events'])->pluck('date')->sort()->last();
}
}

View file

@ -80,14 +80,14 @@ class TalksExtensionTest extends TestCase
$pastTalk = [
'title' => 'Past talk',
'events' => [
'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT),
['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)],
]
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)],
['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)],
],
];
@ -103,21 +103,21 @@ class TalksExtensionTest extends TestCase
$pastTalk = [
'title' => 'Past talk',
'events' => [
'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT),
['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)],
]
];
$todayTalk = [
'title' => 'A talk that it happening today',
'events' => [
['date' => (new DateTime('now'))->format(TalksExtension::DATE_FORMAT)],
['date' => (new DateTime('now'))->format(TalksExtension::DATE_FORMAT)],
],
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)],
['date' => (new DateTime('+1 day'))->format(TalksExtension::DATE_FORMAT)],
],
];