Get events for talks
This commit is contained in:
parent
478d78add6
commit
8d1fe01d83
|
@ -5,6 +5,8 @@ namespace App\Talks\TwigExtension;
|
|||
use Illuminate\Support\Collection;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
|
||||
use Twig_Extension;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
use Twig\TwigFilter;
|
||||
use Twig_SimpleFunction;
|
||||
|
||||
class TalksExtension extends Twig_Extension
|
||||
|
@ -34,6 +36,13 @@ class TalksExtension extends Twig_Extension
|
|||
];
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('events', [$this, 'getEvents']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all upcoming and previous talks.
|
||||
*
|
||||
|
@ -88,4 +97,11 @@ class TalksExtension extends Twig_Extension
|
|||
{
|
||||
return (string) collect($talk['events'])->pluck('date')->sort()->last();
|
||||
}
|
||||
|
||||
public function getEvents(Collection $talks): Collection
|
||||
{
|
||||
return $talks->flatMap(function ($talk): array {
|
||||
return $talk['events'];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,4 +141,26 @@ class TalksExtensionTest extends TestCase
|
|||
$this->assertCount(1, $this->extension->getUpcoming([$talk]));
|
||||
$this->assertEmpty($this->extension->getPast([$talk]));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_events_from_talks()
|
||||
{
|
||||
$talkA = [
|
||||
'title' => 'Talk A',
|
||||
'events' => [
|
||||
['event' => 'event_a', 'date' => (new DateTime('-1 days'))->getTimestamp()],
|
||||
['event' => 'event_b', 'date' => (new DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
];
|
||||
|
||||
$talkB = [
|
||||
'title' => 'Talk B',
|
||||
'events' => [
|
||||
['event' => 'event_a', 'date' => (new DateTime('-3 days'))->getTimestamp()],
|
||||
],
|
||||
];
|
||||
|
||||
$talks = collect([$talkA, $talkB]);
|
||||
$this->assertCount(3, $this->extension->getEvents($talks));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue