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

View file

@ -80,7 +80,7 @@ class TalksExtensionTest extends TestCase
$pastTalk = [ $pastTalk = [
'title' => 'Past talk', 'title' => 'Past talk',
'events' => [ 'events' => [
'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT), ['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)],
] ]
]; ];
@ -103,7 +103,7 @@ class TalksExtensionTest extends TestCase
$pastTalk = [ $pastTalk = [
'title' => 'Past talk', 'title' => 'Past talk',
'events' => [ 'events' => [
'date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT), ['date' => (new DateTime('-1 day'))->format(TalksExtension::DATE_FORMAT)],
] ]
]; ];