summaryrefslogtreecommitdiff
path: root/includes/ical/src/EventsList.php
blob: 33253685e9004e68123c7901090070709cc60620 (plain)
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
<?php

namespace om;

/**
 * Copyright (c) 2004-2022 Roman Ožana (https://ozana.cz)
 *
 * @license BSD-3-Clause
 * @author Roman Ožana <roman@ozana.cz>
 */
class EventsList extends \ArrayObject {

	/**
	 * Return array of Events
	 *
	 * @return array
	 */
	public function getArrayCopy(): array {
		return array_values(parent::getArrayCopy());
	}

	/**
	 * Return sorted EventList (the newest dates are first)
	 *
	 * @return $this
	 */
	public function sorted(): EventsList {
		$this->uasort(static function ($a, $b): int {
			if ($a['DTSTART'] === $b['DTSTART']) {
				return 0;
			}
			return ($a['DTSTART'] < $b['DTSTART']) ? -1 : 1;
		});

		return $this;
	}

	/**
	 * Return reversed sorted EventList (the oldest dates are first)
	 *
	 * @return $this
	 */
	public function reversed(): EventsList {
		$this->uasort(static function ($a, $b): int {
			if ($a['DTSTART'] === $b['DTSTART']) {
				return 0;
			}
			return ($a['DTSTART'] > $b['DTSTART']) ? -1 : 1;
		});

		return $this;
	}

}