Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ReactionMessage.php
1<?php
2
4
10
12{
13 public const COUNT_DISPLAYED_USERS = 5;
14
15 private int $messageId;
19 private array $reactionCounters = [];
23 private array $reactionUsers = [];
27 private array $ownReactions = [];
28 private bool $haveDisplayedUsers = false;
29 private bool $haveUndisplayedUsers = false;
30
31 public function __construct(int $messageId)
32 {
33 $this->messageId = $messageId;
34 }
35
36 public static function getByMessageId(int $messageId): self
37 {
38 return (new ReactionMessages([$messageId]))->getReactionMessage($messageId);
39 }
40
41 public function addCounter(string $reaction, int $counter): self
42 {
43 $this->reactionCounters[$reaction] = $counter;
44
45 if ($counter <= self::COUNT_DISPLAYED_USERS)
46 {
47 $this->haveDisplayedUsers = true;
48 }
49 else
50 {
51 $this->haveUndisplayedUsers = true;
52 }
53
54 return $this;
55 }
56
57 public function addUsers(string $reaction, array $users): self
58 {
59 if (isset($this->reactionUsers[$reaction]))
60 {
61 $this->reactionUsers[$reaction] = array_merge($this->reactionUsers[$reaction], $users);
62 }
63 else
64 {
65 $this->reactionUsers[$reaction] = $users;
66 }
67
68 return $this;
69 }
70
71 public function addOwnReaction(string $reaction): self
72 {
73 $this->ownReactions[] = $reaction;
74
75 return $this;
76 }
77
78 public function getMessageId(): int
79 {
80 return $this->messageId;
81 }
82
83 public function needToFillOwnReactions(): bool
84 {
85 return empty($this->ownReactions) && $this->haveUndisplayedUsers;
86 }
87
88 public function haveReactions(): bool
89 {
90 return !empty($this->reactionCounters);
91 }
92
93 public function haveDisplayedUsers(): bool
94 {
95 return $this->haveDisplayedUsers;
96 }
97
98 public function toRestFormat(array $option = []): array
99 {
100 $converter = new Converter(Converter::KEYS | Converter::VALUES | Converter::TO_LOWER | Converter::LC_FIRST);
101 $rest = [
102 'messageId' => $this->messageId,
103 'reactionCounters' => $converter->process($this->reactionCounters),
104 'reactionUsers' => $converter->process($this->reactionUsers),
105 ];
106
107 if (!isset($option['WITHOUT_OWN_REACTIONS']) || $option['WITHOUT_OWN_REACTIONS'] === false)
108 {
109 $rest['ownReactions'] = $converter->process($this->ownReactions);
110 }
111
112 return $rest;
113 }
114
115 public static function getRestEntityName(): string
116 {
117 return 'reaction';
118 }
119
120 public function getPopupData(array $excludedList = []): PopupData
121 {
122 return new PopupData([new UserShortPopupItem($this->getUserIds())], $excludedList);
123 }
124
128 public function getUserIds(): array
129 {
130 $userIds = [];
131
132 foreach ($this->reactionUsers as $reaction)
133 {
134 foreach ($reaction as $user)
135 {
136 $userIds[$user] = $user;
137 }
138 }
139
140 return $userIds;
141 }
142}
addUsers(string $reaction, array $users)
addCounter(string $reaction, int $counter)