Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
calendar.php
1<?php
2
4
10use DateInterval;
11
13{
14 private $events = [];
15 private $timezones = [];
16 private $name;
17 private $description;
18 private $withTimezone = false;
19 private $refreshInterval;
20 private $productIdentifier;
21 private $method;
22
27 public static function createInstance(string $name = null): Calendar
28 {
29 return new self($name);
30 }
31
36 public function __construct(string $name = null)
37 {
38 $this->name = $name;
39 }
40
44 public function getType(): string
45 {
46 return 'VCALENDAR';
47 }
48
52 public function getProperties(): array
53 {
54 return [
55 'VERSION',
56 'PRODID',
57 ];
58 }
59
64 public function setMethod(string $method): Calendar
65 {
66 $this->method = $method;
67
68 return $this;
69 }
70
75 public function setName(string $name): Calendar
76 {
77 $this->name = $name;
78
79 return $this;
80 }
81
86 public function setDescription(string $description): Calendar
87 {
88 $this->description = $description;
89
90 return $this;
91 }
92
97 public function setIdentifier(string $identifier): Calendar
98 {
99 $this->productIdentifier = $identifier;
100
101 return $this;
102 }
103
108 public function addEvent($event): Calendar
109 {
110 if (is_null($event))
111 {
112 return $this;
113 }
114
115 $events = array_map(function ($eventToResolve) {
116 if (! is_callable($eventToResolve)) {
117 return $eventToResolve;
118 }
119 $newEvent = new Event(Helper::getUniqId());
120
121 $eventToResolve($newEvent);
122
123 return $newEvent;
124 }, is_array($event) ? $event : [$event]);
125
126 $this->events = array_merge($this->events, $events);
127
128 return $this;
129 }
130
135 public function setTimezones($timezone): Calendar
136 {
137 if (is_null($timezone)) {
138 return $this;
139 }
140
141 $timezones = array_map(function ($eventToResolve) {
142 if (! is_callable($eventToResolve)) {
143 return $eventToResolve;
144 }
145
146 $newTimezone = new Timezone();
147
148 $eventToResolve($newTimezone);
149
150 return $newTimezone;
151 }, is_array($timezone) ? $timezone : [$timezone]);
152
153 $this->timezones = array_merge($this->timezones, $timezones);
154
155 return $this;
156 }
157
161 public function setWithTimezone(): Calendar
162 {
163 $this->withTimezone = true;
164
165 return $this;
166 }
167
172 public function refreshInterval(int $min): Calendar
173 {
174 $this->refreshInterval = new DateInterval("PT{$min}M");
175
176 return $this;
177 }
178
182 public function get(): string
183 {
184 return $this->toString();
185 }
186
190 public function setContent(): Content
191 {
192 $events = $this->events;
193 $timezones = $this->timezones;
194
195 if ($this->withTimezone) {
196 array_walk($events, function (Event $event)
197 {
198 $event->setWithTimezone();
199 });
200 }
201
202 $content = Content::getInstance($this->getType())
203 ->textProperty('VERSION', '2.0')
204 ->textProperty('METHOD', $this->method)
205 ->textProperty('CALSCALE', 'GREGORIAN')
206 ->textProperty('PRODID', $this->productIdentifier ?? '-//Bitrix//Bitrix Calendar//EN')
207 ->textProperty(['NAME', 'X-WR-CALNAME'], $this->name)
208 ->textProperty(['DESCRIPTION', 'X-WR-CALDESC'], $this->description)
209 ->subComponent(...$timezones)
210 ->subComponent(...$events);
211
212 if ($this->refreshInterval)
213 {
214 $content->property(
215 LengthPropertyType::getInstance('REFRESH-INTERVAL', $this->refreshInterval)
216 ->addParameter(new Parameter('VALUE', 'DURATION'))
217 )
218 ->property(LengthPropertyType::getInstance('X-PUBLISHED-TTL', $this->refreshInterval));
219 }
220
221 return $content;
222 }
223}
static getInstance($names, \DateInterval $interval)
setIdentifier(string $identifier)
Definition calendar.php:97
setDescription(string $description)
Definition calendar.php:86
static createInstance(string $name=null)
Definition calendar.php:27
setWithTimezone(bool $withTimeZone)
Definition event.php:205