Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
remindcollection.php
1<?php
2
4
5
9
11{
15 private ?Date $start = null;
16
20 private bool $single = false;
21
26 public function setEventStart(Date $start): RemindCollection
27 {
28 $this->start = $start;
29
30 return $this;
31 }
32
38 public function getSpecificTimeCollection(string $dateFormat = null): array
39 {
40 if (!$this->start)
41 {
42 throw new PropertyException('You should set start event time. Use setEventStart.');
43 }
44
45 return array_map(function ($remind) use ($dateFormat) {
47 try
48 {
49 return $remind
50 ->setEventStart($this->start)
51 ->getSpecificTime()
52 ->format($dateFormat)
53 ;
54 }
55 catch (PropertyException $exception)
56 {
57 return '';
58 }
60 }
61
65 public function getFilterRemindBeforeEventStart(): array
66 {
67 return array_filter($this->collection, function ($remind) {
68 try
69 {
71 return $remind
72 ->setEventStart($this->start)
73 ->isBeforeEventStart()
74 ;
75 }
76 catch (PropertyException $exception)
77 {
78 return false;
79 }
80 });
81 }
82
88 public function deDuplicate(): self
89 {
90 if (!$this->start)
91 {
92 throw new PropertyException('You should set start event time. Use setEventStart.');
93 }
95 $remindList = [];
96
98 foreach ($this->collection as $remind)
99 {
100 $key = $remind
101 ->setEventStart($this->start)
102 ->getSpecificTime()
103 ->format('U');
104
105 if (empty($remindList[$key]))
106 {
107 $remindList[$key] = $remind;
108 }
109 else
110 {
111 $remindList[$key] = $this->chooseRemindByRank($remind, $remindList[$key]);
112 }
113 }
114
115 $this->collection = array_values($remindList);
116
117 return $this;
118 }
119
124 {
125 usort($this->collection, function (Remind $remind1, Remind $remind2) {
126 if (
127 ($remind1->getEventStart() === null)
128 || ($remind2->getEventStart() === null)
129 )
130 {
131 $remind1->setEventStart($this->start);
132 $remind2->setEventStart($this->start);
133 }
134
135 return $remind1->getTimeBeforeStartInMinutes() <=> $remind2->getTimeBeforeStartInMinutes();
136 });
137
138 return $this;
139 }
140
147 private function chooseRemindByRank(Remind $remind1, Remind $remind2): Remind
148 {
149 return ($remind1->getRank() > $remind2->getRank())
150 ? $remind1
151 : $remind2
152 ;
153 }
154
160 public function setCollection(array $collection): self
161 {
162 $this->collection = $collection;
163
164 return $this;
165 }
166
170 public function isSingle(): bool
171 {
172 return $this->single;
173 }
174
180 public function setSingle(bool $single): self
181 {
182 $this->single = $single;
183
184 return $this;
185 }
186
190 public function getEventStart(): ?Date
191 {
192 return $this->start;
193 }
194}