Blame | Letzte Änderung | Log anzeigen | RSS feed
<?php namespace Clockwork\Request\Timeline;// Data structure representing collection of time-based eventsclass Timeline{// Timeline eventspublic $events = [];// Create a new timeline, optionally with existing eventspublic function __construct($events = []){foreach ($events as $event) {$this->create($event['description'], $event);}}// Find or create a new event, takes description and optional data - name, start, end, duration, color, datapublic function event($description, $data = []){$name = isset($data['name']) ? $data['name'] : $description;if ($event = $this->find($name)) return $event;return $this->create($description, $data);}// Create a new event, takes description and optional data - name, start, end, duration, color, datapublic function create($description, $data = []){return $this->events[] = new Event($description, $data);}// Find event by namepublic function find($name){foreach ($this->events as $event) {if ($event->name == $name) return $event;}}// Merge another timeline instance into the current timelinepublic function merge(Timeline $timeline){$this->events = array_merge($this->events, $timeline->events);return $this;}// Finalize timeline, ends all events, sorts them and returns as an arraypublic function finalize($start = null, $end = null){foreach ($this->events as $event) {$event->finalize($start, $end);}$this->sort();return $this->toArray();}// Sort the timeline events by start timepublic function sort(){usort($this->events, function ($a, $b) { return $a->start * 1000 - $b->start * 1000; });}// Return events as an arraypublic function toArray(){return array_map(function ($event) { return $event->toArray(); }, $this->events);}}