Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
2
4
11class Event
12{
13 /* @var string $type */
14 private $type;
15 /* @var array $data */
16 private $data = [];
17
22 public function __construct(string $type = '')
23 {
24 $this->type = $type;
25 }
26
31 public function setData(array $data = []): self
32 {
33 $this->data = $this->prepareData($data);
34 return $this;
35 }
36
40 public function getType(): string
41 {
42 return $this->type;
43 }
44
48 public function getData(): array
49 {
50 return $this->data;
51 }
52
56 public function getUserId(): int
57 {
58 $userId = 0;
59
60 switch ($this->type)
61 {
66 $userId = (int)$this->data['USER_ID'];
67 break;
68 }
69
70 return $userId;
71 }
72
76 public function getGroupId(): int
77 {
78 $groupId = 0;
79
80 switch ($this->type)
81 {
86 $groupId = (int)$this->data['GROUP_ID'];
87 break;
88 }
89
90 return $groupId;
91 }
92
96 public function getUsedRoles(): array
97 {
98 $rolesList = [];
99
100 switch ($this->type)
101 {
104 $rolesList[] = $this->data['ROLE'];
105 break;
107 $rolesList[] = $this->data['ROLE_OLD'];
108 $rolesList[] = $this->data['ROLE_NEW'];
109 break;
110 }
111
112 return $rolesList;
113 }
114
118 public function getInitiatedByType(): string
119 {
120 $initiatedByType = null;
121
122 switch ($this->type)
123 {
127 $initiatedByType = $this->data['INITIATED_BY_TYPE'];
128 break;
129 }
130
131 return $initiatedByType;
132 }
133
138 private function prepareData(array $data = []): array
139 {
140 $validFields = [
141 'USER_ID',
142 'GROUP_ID',
143 'ROLE',
144 'ROLE_OLD',
145 'ROLE_NEW',
146 'INITIATED_BY_TYPE',
147 'RELATION_ID',
148 ];
149
150 foreach ($data as $key => $row)
151 {
152 if (!in_array($key, $validFields, true))
153 {
154 unset($data[$key]);
155 }
156 }
157
158 return $data;
159 }
160}