-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Calendar.php
71 lines (58 loc) · 1.62 KB
/
Calendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Mzur\KirbyCalendar;
use Kirby\Toolkit\A;
/**
* A simple calendar object that mainly consists of a list of Event objects.
*/
class Calendar {
/**
* Array of all events of this calendar.
*/
private $events;
/**
* Array of the aggregation of all present fields of the events. Not every
* event may contain every field.
*/
private $eventFields;
/**
* @param array $events An array of 'raw' events. A raw event is an array
* of field => value pairs.
*/
function __construct($events = []) {
// intantiate all the given events to Event objects
$this->events = array_map('Mzur\\KirbyCalendar\\Event::instantiate', $events);
// sort the events from old to new
usort($this->events, 'Mzur\\KirbyCalendar\\Event::compare');
$this->eventFields = self::findEventFields($this->events);
}
/**
* @return all events, even the ones that are already past.
*/
public function getAllEvents() {
return $this->events;
}
/**
* @return all future events.
*/
public function getEvents() {
return array_filter($this->events, 'Mzur\\KirbyCalendar\\Event::filterPast');
}
/**
* @return all present fields of the events of this calendar.
*/
public function getEventFields() {
return $this->eventFields;
}
/**
* Aggregates all the fields present in the given event object.
* @param array $events An array of Event objects.
*/
private static function findEventFields($events) {
$fields = [];
foreach ($events as $event) {
$fields = A::merge($fields, $event->getFieldKeys());
}
// make an associative array with the same keys as values
return array_combine($fields, $fields);
}
}