Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
configuration.php
1<?php
10
15
16Loc::getMessage(__FILE__);
17
19{
21 protected $id;
22
24 protected $data = [];
25
27 protected $view;
28
30 protected $options = [];
31
37 public function __construct(array $data = [])
38 {
39 if ($data)
40 {
41 $this->data = $data;
42 }
43 }
44
50 public function getId()
51 {
52 return $this->id;
53 }
54
60 public function setId($id)
61 {
62 $this->id = $id;
63 }
64
70 public function getView()
71 {
72 if (!is_callable($this->view))
73 {
74 return null;
75 }
76
77 return call_user_func_array($this->view, []);
78 }
79
85 public function setView($view)
86 {
87 $this->view = $view;
88 }
89
97 public function set($key, $value)
98 {
99 $this->data[$key] = $value;
100 $option = $this->getOption($key);
101 if ($option)
102 {
103 $option->setValue($value);
104 }
105
106 return $value;
107 }
108
116 public function get($key, $defaultValue = null)
117 {
118 if (isset ($this->data[$key]))
119 {
120 if ($this->data[$key] instanceof \Closure)
121 {
122 return $this->data[$key]();
123 }
124
125 return $this->data[$key];
126 }
127
128 $option = $this->getOption($key);
129 if ($option)
130 {
131 return $option->getValue();
132 }
133
134 return $defaultValue;
135 }
136
144 public function getReadonlyView($key, $defaultValue = null)
145 {
146 $value = $this->get($key, $defaultValue);
147 $option = $this->getOption($key);
148
152 if (!empty($option->getItems()))
153 {
154 foreach ($option->getItems() as $item)
155 {
156 if (!empty($value) && isset($item['code']) && $item['code'] == $value)
157 {
158 return $item['value'];
159 }
160 }
161 }
162
163 if ($option)
164 {
165 return $option->getReadonlyView($value);
166 }
167
168 return $value;
169 }
170
177 public function getOption($key)
178 {
179 foreach ($this->options as $option)
180 {
181 if ($option->getCode() == $key)
182 {
183 return $option;
184 }
185 }
186
187 return null;
188 }
189
195 public function hasOptions()
196 {
197 return count($this->options) > 0;
198 }
199
205 public function getOptions()
206 {
207 return $this->options;
208 }
209
215 public function getArrayOptions()
216 {
217 return self::convertToArray($this->options);
218 }
219
226 public static function convertToArray(array $options)
227 {
228 $list = [];
229 foreach ($options as $option)
230 {
231 $list[] = $option->getArray();
232 }
233
234 return $list;
235 }
236
246 public function addOption(ConfigurationOption $option, $targetOptionCode = null, $isInsertAfter = true)
247 {
248 if ($option->isTemplated() && $this->hasTemplatedOption())
249 {
250 throw new ArgumentException('Templated option already exists.');
251 }
252
253 $uniqueTypes = [
254 ConfigurationOption::TYPE_TEMPLATE_TYPE,
255 ConfigurationOption::TYPE_TEMPLATE_ID,
256 ];
257 if (in_array($option->getType(), $uniqueTypes, true) && $this->hasOptionsOfType($option->getType()))
258 {
259 throw new ArgumentException('Option with type \'' . $option->getType() . '\' already exists.');
260 }
261
262 if ($targetOptionCode)
263 {
264 $index = array_search($this->getOption($targetOptionCode), $this->options);
265 if ($isInsertAfter)
266 {
267 $index++;
268 }
269 $this->options = array_merge(
270 array_slice($this->options, 0, $index),
271 [$option],
272 array_slice($this->options, $index)
273 );
274 }
275 else
276 {
277 $this->options[] = $option;
278 }
279 }
280
287 public function setArrayOptions(array $options)
288 {
289 foreach ($options as $option)
290 {
291 $this->addOption(new ConfigurationOption($option));
292 }
293 }
294
300 public function getTemplatedOption()
301 {
302 foreach ($this->options as $option)
303 {
304 if ($option->isTemplated())
305 {
306 return $option;
307 }
308 }
309
310 return null;
311 }
312
318 public function hasTemplatedOption()
319 {
320 return $this->getTemplatedOption() !== null;
321 }
322
329 public function getOptionsByGroup($group)
330 {
331 $result = [];
332 foreach ($this->options as $option)
333 {
334 if ($option->getGroup() == $group)
335 {
336 $result[] = $option;
337 }
338 }
339
340 return $result;
341 }
342
349 public function getOptionsByType($type)
350 {
351 $result = [];
352 foreach ($this->options as $option)
353 {
354 if ($option->getType() == $type)
355 {
356 $result[] = $option;
357 }
358 }
359
360 return $result;
361 }
362
369 public function getOptionByType($type)
370 {
371 return current($this->getOptionsByType($type));
372 }
373
380 public function hasOptionsOfType($type)
381 {
382 return count($this->getOptionsByType($type)) > 0;
383 }
384
390 public function checkOptions()
391 {
392 $result = new Result();
393 $this->checkRequiredOptions($result);
394
395 return $result;
396 }
397
404 protected function checkRequiredOptions(Result $result = null)
405 {
406 if (is_null($result))
407 {
408 $result = new Result;
409 }
410
411 foreach ($this->getOptions() as $option)
412 {
413 if (!$option->isRequired())
414 {
415 continue;
416 }
417
418 if ($option->hasValue())
419 {
420 continue;
421 }
422
423 $result->addError(new Error(
424 Loc::getMessage(
425 'SENDER_MESSAGE_CONFIG_ERROR_EMPTY_REQUIRED_FIELD',
426 ['%name%' => $option->getName()]
427 )
428 ));
429 }
430
431 return $result;
432 }
433}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getReadonlyView($key, $defaultValue=null)
addOption(ConfigurationOption $option, $targetOptionCode=null, $isInsertAfter=true)
static convertToArray(array $options)