ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDateList.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /*
5  +-----------------------------------------------------------------------------+
6  | ILIAS open source |
7  +-----------------------------------------------------------------------------+
8  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9  | |
10  | This program is free software; you can redistribute it and/or |
11  | modify it under the terms of the GNU General Public License |
12  | as published by the Free Software Foundation; either version 2 |
13  | of the License, or (at your option) any later version. |
14  | |
15  | This program is distributed in the hope that it will be useful, |
16  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18  | GNU General Public License for more details. |
19  | |
20  | You should have received a copy of the GNU General Public License |
21  | along with this program; if not, write to the Free Software |
22  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23  +-----------------------------------------------------------------------------+
24 */
25 
33 class ilDateList implements Iterator, Countable
34 {
35  public const TYPE_DATE = 1;
36  public const TYPE_DATETIME = 2;
37 
39  protected array $list_item = [];
40 
41  protected int $type;
42 
43  public function __construct(int $a_type)
44  {
45  $this->type = $a_type;
46  $this->list_item = [];
47  }
48 
49  public function rewind(): void
50  {
51  reset($this->list_item);
52  }
53 
54  public function current(): ilDateTime
55  {
56  return current($this->list_item);
57  }
58 
59  public function key(): string
60  {
61  return key($this->list_item);
62  }
63 
64  public function next(): void
65  {
66  next($this->list_item);
67  }
68 
69  public function valid(): bool
70  {
71  return key($this->list_item) !== null;
72  }
73 
74  public function count(): int
75  {
76  return count($this->list_item);
77  }
78 
80  public function get(): array
81  {
82  return $this->list_item;
83  }
84 
85  public function getAtPosition(int $a_pos): ?ilDateTime
86  {
87  $counter = 1;
88  foreach ($this->get() as $item) {
89  if ($counter++ == $a_pos) {
90  return $item;
91  }
92  }
93 
94  return null;
95  }
96 
97  public function add(ilDateTime $date): void
98  {
99  $this->list_item[(string) $date->get(IL_CAL_UNIX)] = clone $date;
100  }
101 
102  public function merge(ilDateList $other_list): void
103  {
104  foreach ($other_list->get() as $new_date) {
105  $this->add($new_date);
106  }
107  }
108 
109  public function remove(ilDateTime $remove): void
110  {
111  $unix_remove = (string) $remove->get(IL_CAL_UNIX);
112  if (isset($this->list_item[$unix_remove])) {
113  unset($this->list_item[$unix_remove]);
114  }
115  }
116 
117  public function removeByDAY(ilDateTime $remove): void
118  {
119  foreach ($this->list_item as $key => $dt) {
120  if (ilDateTime::_equals($remove, $dt, IL_CAL_DAY, ilTimeZone::UTC)) {
121  unset($this->list_item[$key]);
122  }
123  }
124  }
125 
126  public function sort(): void
127  {
128  ksort($this->list_item, SORT_NUMERIC);
129  }
130 
131  public function __toString(): string
132  {
133  $out = '<br />';
134  foreach ($this->get() as $date) {
135  $out .= $date->get(IL_CAL_DATETIME, '', 'Europe/Berlin') . '<br/>';
136  }
137 
138  return $out;
139  }
140 }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const IL_CAL_DATETIME
merge(ilDateList $other_list)
add(ilDateTime $date)
removeByDAY(ilDateTime $remove)
const IL_CAL_UNIX
getAtPosition(int $a_pos)
const IL_CAL_DAY
List of dates.
$out
Definition: buildRTE.php:24
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(int $a_type)
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.