today = (new \DateTime())->format('Y-m-d'); } /** * {@inheritdoc} */ public function getFilters() { return [ new Twig_SimpleFilter('all_talks', [$this, 'getAll']), new Twig_SimpleFilter('upcoming_talks', [$this, 'getUpcoming']), new Twig_SimpleFilter('past_talks', [$this, 'getPast']), ]; } /** * Get all upcoming and previous talks. * * Used to display the talk table on a specific talk page. * * @param array $data An associative array of talk and event data. * * @return array */ public function getAll(array $data) { return $this->sort($this->format($data)); } /** * Get all upcoming talks. * * Used on the main talks page. * * @param array $data The talk and event data. * * @return array */ public function getUpcoming(array $data) { return $this->sort($this->format($data)->filter(function ($talk) { return $talk['event']['date'] >= $this->today; })); } /** * Get all past talks. * * Used on the main talks page and the talks archive. * * @param array $data The talk and event data. * * @return array */ public function getPast(array $data) { return $this->sort($this->format($data)->filter(function ($talk) { return $talk['event']['date'] < $this->today; })); } /** * Format the talk data into the required format. * * @param array $data The talk and event data. * * @return Collection The event and talk data. */ public function format(array $data) { $event_data = collect($data['event_data']); return collect($data['talks'])->flatMap(function ($talk) use ($event_data) { // Build an associative array with the talk, as well as the // specified event data (e.g. date and time) as well as the shared // event data (e.g. event name and website). return collect($talk['events'])->map(function ($event) use ($talk, $event_data) { $event = collect($event); $event = $event->merge($event_data->get($event->get('event')))->all(); return compact('event', 'talk'); }); }); } /** * Sort and return the talks. * * @param Collection $talks The talk data. * * @return array */ private function sort(Collection $talks) { return $talks->sortByDesc('event.date')->all(); } /** * {@inheritdoc} */ public function getName() { return 'format_talks'; } }