Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
EntityChat.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
11
12class EntityChat extends GroupChat
13{
14 protected const ENTITY_SEPARATOR = '|';
15
16 protected const ENTITY_MAP_FIELDS = ['entityId', 'entityData1', 'entityData2', 'entityData3'];
17
18 protected $entityMap = [
19 'entityId' => [],
20 'entityData1' => [],
21 'entityData2' => [],
22 'entityData3' => [],
23 ];
24
25 protected $entityData = [];
26
27 public function setEntityMap(array $entityMap): self
28 {
29 foreach ($entityMap as $field => $map)
30 {
31 if (in_array($field, self::ENTITY_MAP_FIELDS, true) && is_array($map))
32 {
33 $this->entityMap[$field] = array_values($map);
34 }
35 }
36
37 return $this;
38 }
39
40 public function getEntityMap(): array
41 {
42 return $this->entityMap;
43 }
44
49 public function getEntityData(bool $force = false): array
50 {
51 if (!count($this->entityData) || $force)
52 {
53 $this->entityData = $this->unmapEntity();
54 }
55
56 return $this->entityData;
57 }
58
59 private function unmapEntity(): array
60 {
61 $result = [];
62 foreach ($this->getEntityMap() as $entityType => $entityFields)
63 {
64 if (!count($entityFields))
65 {
66 continue;
67 }
68
69 if ($this->$entityType)
70 {
71 $data = explode(self::ENTITY_SEPARATOR, $this->$entityType);
72 if (count($entityFields) === count($data))
73 {
74 $result[$entityType] = array_combine($entityFields, $data);
75 }
76 else
77 {
78 $result[$entityType] = $data;
79 }
80 }
81 else
82 {
83 $result[$entityType] = array_fill_keys($entityFields, null);
84 }
85 }
86 return $result;
87 }
88
99 public static function find(array $params = [], ?Context $context = null): Result
100 {
101 $result = new Result;
102
103 if (empty($params['ENTITY_TYPE']) || empty($params['ENTITY_ID']))
104 {
105 return $result->addError(new ChatError(ChatError::WRONG_PARAMETER));
106 }
107
108 $row = ChatTable::query()
109 ->setSelect(['ID', 'TYPE', 'ENTITY_TYPE', 'ENTITY_ID'])
110 ->where('ENTITY_TYPE', $params['ENTITY_TYPE'])
111 ->where('ENTITY_ID', $params['ENTITY_ID'])
112 ->fetch()
113 ;
114
115 if ($row)
116 {
117 $result->setResult([
118 'ID' => (int)$row['ID'],
119 'TYPE' => $row['TYPE'],
120 'ENTITY_TYPE' => $row['ENTITY_TYPE'],
121 'ENTITY_ID' => $row['ENTITY_ID'],
122 ]);
123 }
124
125 return $result;
126 }
127}
static find(array $params=[], ?Context $context=null)
setEntityMap(array $entityMap)
getEntityData(bool $force=false)
addError(Error $error)
Definition result.php:50