Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseController.php
1<?php
2
4
22
23abstract class BaseController extends Controller
24{
25 protected const MAX_LIMIT = 200;
26 protected const DEFAULT_LIMIT = 50;
27
28 public function getAutoWiredParameters()
29 {
30 return [
33 'chat',
34 function($className, string $dialogId) {
35 $chatId = Dialog::getChatId($dialogId);
36
37 return Chat::getInstance((int)$chatId);
38 }
39 ),
42 'chat',
43 function($className, int $chatId) {
44 return Chat::getInstance($chatId);
45 }
46 ),
49 'message',
50 function ($className, int $messageId) {
51 return $this->getMessageById($messageId);
52 }
53 ),
54 ];
55 }
56
57 protected function getDefaultPreFilters()
58 {
59 return array_merge(
60 [
62 ],
63 parent::getDefaultPreFilters(),
64 [
66 new StartIdFilter(),
67 new CheckChatAccess(),
69 ]
70 );
71 }
72
73 protected function getLimit(int $limit): int
74 {
75 return $limit > 0 && $limit <= static::MAX_LIMIT ? $limit : static::DEFAULT_LIMIT;
76 }
77
78 protected function toRestFormat(RestConvertible ...$entities): array
79 {
80 return (new RestAdapter(...$entities))->toRestFormat();
81 }
82
83 public static function recursiveWhiteList($fields, $whiteList, bool $sanitizeOnly = false)
84 {
85 $data = [];
86 $converter = new Converter(Converter::TO_SNAKE | Converter::TO_UPPER);
87 foreach ($fields as $field => $value)
88 {
89 if (is_array($value))
90 {
91 $data[$converter->process($field)] = self::recursiveWhiteList($value, $whiteList[$field], true);
92 }
93 elseif ((is_array($whiteList) && in_array($field, $whiteList)) || $sanitizeOnly)
94 {
95 $data[$converter->process($field)] = $value;
96 }
97 }
98
99 return $data;
100 }
101
102 //todo: think about recursion in method.
103 protected function checkWhiteList(array $fields, array $whiteList): array
104 {
105 $filteredFields = [];
106
107 foreach ($whiteList as $allowedField)
108 {
109 if (isset($fields[$allowedField]))
110 {
111 $filteredFields[$allowedField] = $fields[$allowedField];
112 }
113 }
114
115 return $filteredFields;
116 }
117
118 protected function getMessageById(int $id): ?Message
119 {
120 $message = new \Bitrix\Im\V2\Message($id);
121
122 if ($message->getMessageId() === null)
123 {
124 $this->addError(new MessageError(MessageError::MESSAGE_NOT_FOUND));
125
126 return null;
127 }
128
129 return $message;
130 }
131
132 protected function convertCharToBool(string $char, bool $default = false): bool
133 {
134 if ($char === 'Y')
135 {
136 return true;
137 }
138 if ($char === 'N')
139 {
140 return false;
141 }
142
143 return $default;
144 }
145
146 protected function getRawValue(string $key)
147 {
148 return $this->prepareRawValue($this->request->getPostList(), $key)
149 ?? $this->prepareRawValue($this->request->getQueryList(), $key)
150 ?? null
151 ;
152 }
153
154 private function prepareRawValue(ParameterDictionary $list, string $key)
155 {
156 $rawData = $list->toArrayRaw();
157 if (isset($rawData[$key]))
158 {
159 if(Application::getInstance()->isUtfMode())
160 {
161 return $rawData[$key];
162 }
163
164 return Encoding::convertEncoding($rawData[$key], 'UTF-8', SITE_CHARSET);
165 }
166
167 $data = $list->toArray();
168
169 return $data[$key] ?? null;
170 }
171}
static getChatId($dialogId, $userId=null)
Definition dialog.php:91
toRestFormat(RestConvertible ... $entities)
convertCharToBool(string $char, bool $default=false)
static recursiveWhiteList($fields, $whiteList, bool $sanitizeOnly=false)
checkWhiteList(array $fields, array $whiteList)