Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
recurringeventrules.php
1<?php
2
4
5
9
11{
12 public const FREQUENCY = [
13 'daily' => 'DAILY',
14 'weekly' => 'WEEKLY',
15 'monthly' => 'MONTHLY',
16 'yearly' => 'YEARLY',
17 ];
18 public const FREQUENCY_WEEKLY = 'WEEKLY';
19 public const FREQUENCY_DAILY = 'DAILY';
20 public const FREQUENCY_MONTHLY = 'MONTHLY';
21 public const FREQUENCY_YEARLY = 'YEARLY';
22
26 private string $frequency;
30 private ?int $count = null;
34 private ?Date $until = null;
38 private $dateFormat;
42 private int $interval;
46 private ?array $byDay = [];
47
52 public function __construct(string $frequency, int $interval = 1)
53 {
54 $this->frequency = $frequency;
55 $this->interval = $interval;
56 }
57
61 public function getFields(): array
62 {
63 $fields = [];
64
65 $fields['frequency'] = $this->frequency;
66
67 if ($this->count > 0)
68 {
69 $fields['count'] = $this->count;
70 }
71
72 if (isset($this->until))
73 {
74 $fields['until'] = $this->until->format($this->dateFormat);
75 }
76
77 if (isset($this->interval))
78 {
79 $fields['interval'] = $this->interval;
80 }
81
82 if (isset($this->byDay))
83 {
84 $fields['byday'] = $this->byDay;
85 }
86
87 return $fields;
88 }
89
95 public function setUntil(Date $until, string $dateFormat = null): RecurringEventRules
96 {
97 $this->until = $until;
98 $this->dateFormat = $dateFormat;
99
100 return $this;
101 }
102
107 public function setCount(int $count): RecurringEventRules
108 {
109 $this->count = $count;
110
111 return $this;
112 }
113
117 public function toString(): string
118 {
119 $result = "FREQ={$this->frequency}";
120
121 if ($this->count > 0)
122 {
123 $result .= ",COUNT={$this->count}";
124 }
125
126 if (isset($this->until))
127 {
128 $result .= ",UNTIL={$this->until}";
129 }
130
131 if (isset($this->interval))
132 {
133 $result .= ",INTERVAL={$this->interval}";
134 }
135
136 if (isset($this->byDay) && $this->frequency === self::FREQUENCY_WEEKLY)
137 {
138 $byDayString = implode(',',$this->byDay);
139 $result .= ",BYDAY={$byDayString}";
140 }
141
142 return $result;
143 }
144
148 public function toArray(): array
149 {
150 $result = ['FREQ' => $this->frequency];
151 if ($this->count > 0)
152 {
153 $result['COUNT'] = $this->count;
154 }
155
156 if (isset($this->until))
157 {
158 $result['UNTIL'] = $this->until->toString();
159 }
160
161 if (isset($this->interval))
162 {
163 $result ['INTERVAL'] = $this->interval;
164 }
165
166 if (isset($this->byDay) && $this->frequency === self::FREQUENCY_WEEKLY)
167 {
168 $result['BYDAY'] = $this->byDay;
169 }
170
171 return $result;
172 }
173
178 public function setInterval(int $interval): RecurringEventRules
179 {
180 $this->interval = $interval;
181
182 return $this;
183 }
184
190 public function setByDay(array $byDay): RecurringEventRules
191 {
192 $this->byDay = $byDay;
193
194 return $this;
195 }
196
200 public function getFrequency(): string
201 {
202 return $this->frequency;
203 }
204
208 public function getCount(): ?int
209 {
210 return $this->count;
211 }
212
216 public function getUntil(): ?Date
217 {
218 return $this->until;
219 }
220
224 public function getInterval(): int
225 {
226 return $this->interval;
227 }
228
232 public function getByday(): array
233 {
234 return $this->byDay;
235 }
236
240 public function hasDay(): bool
241 {
242 return ($this->frequency === self::FREQUENCY_WEEKLY)
243 && $this->byDay;
244 }
245
249 public function hasCount(): bool
250 {
251 return (bool)$this->count;
252 }
253
257 public function hasUntil(): bool
258 {
259 return $this->until !== null;
260 }
261
265 public function isUntilEndOfTime(): bool
266 {
267 if ($this->until === null)
268 {
269 return false;
270 }
271
272 return $this->until->format('d.m.Y') === Helper::END_OF_TIME;
273 }
274
279 public function setFrequency(string $frequency): RecurringEventRules
280 {
281 $this->frequency = $frequency;
282
283 return $this;
284 }
285}