Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
providerbase.php
1<?php
2
4
9
14abstract class ProviderBase
15{
16 public const ERROR_DECODE_DATA = 'ERROR_DECODE_DATA';
17 public const ERROR_DATA_NOT_FOUND = 'ERROR_DATA_NOT_FOUND';
18 private $errorList = [];
19 private $contextUser;
20 private $context;
21
27 public function __construct(array $setting)
28 {
29 if (!empty($setting['CONTEXT']))
30 {
31 $this->context = $setting['CONTEXT'];
32 }
33
34 if (!empty($setting['CONTEXT_USER']))
35 {
36 $this->contextUser = $setting['CONTEXT_USER'];
37 }
38 }
39
45 protected function getContext()
46 {
47 return $this->context ?? Helper::getInstance()->getContextAction();
48 }
49
55 protected function getUserContext()
56 {
57 return $this->contextUser ?? Helper::getInstance()->getContextUser('');
58 }
59
69 abstract public function addContent(string $code, $content, $type = false): bool;
70
77 abstract public function addFiles(array $files): array;
78
87 abstract public function get(string $path, int $step): ?array;
88
95 public function getContent(string $path, int $step): array
96 {
97 $result = [
98 'DATA' => null,
99 '~DATA' => null,
100 'FILE_NAME' => null,
101 'SANITIZE' => false,
102 'COUNT' => 0,
103 ];
104
105 try
106 {
107 $content = $this->get($path, $step);
108 if (!is_null($content['DATA']))
109 {
110 $result['~DATA'] = Json::decode($content['DATA']);
111 if (!empty($result['~DATA']))
112 {
113 $result['DATA'] = Helper::getInstance()->sanitize($result['~DATA'], $result['SANITIZE']);
114 }
115 }
116 else
117 {
118 $result['ERROR_CODE'] = self::ERROR_DATA_NOT_FOUND;
119 }
120
121 if ($content['FILE_NAME'])
122 {
123 $result['FILE_NAME'] = preg_replace(
125 '',
126 $content['FILE_NAME']
127 );
128 }
129
130 if (!empty($content['COUNT']) && (int)$content['COUNT'] > 0)
131 {
132 $result['COUNT'] = (int)$content['COUNT'];
133 }
134 }
135 catch (ArgumentException $exception)
136 {
137 $result['ERROR_CODE'] = self::ERROR_DECODE_DATA;
138 }
139 catch (SystemException $exception)
140 {
141 $result['ERROR_CODE'] = self::ERROR_DATA_NOT_FOUND;
142 }
143
144 return $result;
145 }
146
153 protected function packageContent($content): ?string
154 {
155 $result = null;
156 if (is_array($content))
157 {
158 try
159 {
160 $result = Json::encode($content);
161 }
162 catch (ArgumentException $e)
163 {
164 $result = null;
165 $this->addError('ERROR_JSON_ENCODE', '');
166
167 }
168 }
169 elseif (is_string($content))
170 {
171 $result = $content;
172 }
173
174 return $result;
175 }
176
185 protected function addError($code, $message): bool
186 {
187 $this->errorList[$code] = $message;
188
189 return true;
190 }
191
197 public function listError(): array
198 {
199 return $this->errorList;
200 }
201
207 public function resetErrors(): bool
208 {
209 $this->errorList = [];
210
211 return true;
212 }
213}
addContent(string $code, $content, $type=false)