Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseLinkItem.php
1<?php
2
3namespace Bitrix\Im\V2\Link;
4
7use Bitrix\Im\V2\Common\ActiveRecordImplementation;
8use Bitrix\Im\V2\Common\FieldAccessImplementation;
9use Bitrix\Im\V2\Common\RegistryEntryImplementation;
16
17abstract class BaseLinkItem implements LinkItem, \ArrayAccess, ActiveRecord, RegistryEntry
18{
19 use FieldAccessImplementation;
20 use ActiveRecordImplementation;
21 use RegistryEntryImplementation;
22
23 protected ?int $id = null;
24 protected int $authorId;
25 protected ?int $messageId = null;
26 protected int $chatId;
27 protected ?int $entityId;
30
34 abstract public static function getEntityClassName(): string;
35 abstract protected static function getEntityIdFieldName(): string;
36
42 public static function linkEntityToMessage(RestEntity $entity, Message $message): self
43 {
44 $link = new static();
45
46 return $link->setEntity($entity)->setMessageInfo($message);
47 }
48
49 public function toRestFormatIdOnly(): array
50 {
51 return [
52 'linkId' => $this->getPrimaryId(),
53 'chatId' => $this->getChatId(),
54 static::getEntityClassName()::getRestEntityName() . 'Id' => $this->getEntityId()
55 ];
56 }
57
58 public function getPopupData(array $excludedList = []): PopupData
59 {
60 $data = new PopupData([new UserPopupItem([$this->getAuthorId()])], $excludedList);
61
62 return $data->mergeFromEntity($this->getEntity(), $excludedList);
63 }
64
65 //region Getters & setters
66
67 public function getId(): ?int
68 {
69 return $this->id;
70 }
71
72 public function setId(int $id): BaseLinkItem
73 {
74 $this->id = $id;
75 return $this;
76 }
77
78 public function getPrimaryId(): ?int
79 {
80 return $this->getId();
81 }
82
83 public function setPrimaryId(int $primaryId): self
84 {
85 $this->setId($primaryId);
86 return $this;
87 }
88
89 public function getAuthorId(): int
90 {
91 return $this->authorId;
92 }
93
94 public function setAuthorId(int $authorId): BaseLinkItem
95 {
96 $this->authorId = $authorId;
97 return $this;
98 }
99
100 public function getMessageId(): ?int
101 {
102 return $this->messageId;
103 }
104
105 public function setMessageId(?int $messageId): BaseLinkItem
106 {
107 $this->messageId = $messageId;
108 return $this;
109 }
110
111 public function getChatId(): int
112 {
113 return $this->chatId;
114 }
115
116 public function setChatId(int $chatId): BaseLinkItem
117 {
118 $this->chatId = $chatId;
119 return $this;
120 }
121
122 public function getEntityId(): ?int
123 {
124 return $this->entityId;
125 }
126
127 public function setEntityId(?int $entityId): BaseLinkItem
128 {
129 $this->entityId = $entityId;
130 return $this;
131 }
132
133 public function getDateCreate(): DateTime
134 {
135 return $this->dateCreate;
136 }
137
139 {
140 $this->dateCreate = $dateCreate;
141 return $this;
142 }
143
147 public function getEntity(): RestEntity
148 {
149 return $this->entity;
150 }
151
156 public function setEntity(RestEntity $entity): self
157 {
158 $this->entity = $entity;
159 $this->setEntityId($entity->getId());
160
161 return $this;
162 }
163
164 public function setMessageInfo(Message $message): self
165 {
166 $this->setMessageId($message->getMessageId());
167 $this->setChatId($message->getChatId());
168 $this->setAuthorId($message->getAuthorId());
169
170 return $this;
171 }
172
173 //endregion
174
175 protected static function mirrorDataEntityFields(): array
176 {
177 return [
178 'ID' => [
179 'primary' => true,
180 'field' => 'id',
181 'set' => 'setId',
182 'get' => 'getId',
183 ],
184 'MESSAGE_ID' => [
185 'field' => 'messageId',
186 'set' => 'setMessageId',
187 'get' => 'getMessageId',
188 ],
189 'CHAT_ID' => [
190 'field' => 'chatId',
191 'set' => 'setChatId',
192 'get' => 'getChatId',
193 ],
194 'DATE_CREATE' => [
195 'field' => 'dateCreate',
196 'set' => 'setDateCreate',
197 'get' => 'getDateCreate',
198 ],
199 'AUTHOR_ID' => [
200 'field' => 'authorId',
201 'set' => 'setAuthorId',
202 'get' => 'getAuthorId',
203 ],
204 static::getEntityIdFieldName() => [
205 'field' => 'entityId',
206 'set' => 'setEntityId',
207 'get' => 'getEntityId',
208 ]
209 ];
210 }
211
212 public function toRestFormat(array $option = []): array
213 {
214 $startId = Chat::getInstance($this->getChatId())->getStartId();
215
216 return [
217 'id' => $this->getPrimaryId(),
218 'messageId' => ($this->getMessageId() >= $startId) ? $this->getMessageId() : 0,
219 'chatId' => $this->getChatId(),
220 'authorId' => $this->getAuthorId(),
221 'dateCreate' => $this->getDateCreate()->format('c'),
222 static::getEntityClassName()::getRestEntityName() => $this->getEntity()->toRestFormat($option),
223 ];
224 }
225}