Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
configuration.php
1
<?php
9
namespace
Bitrix\Sender\Message
;
10
11
use
Bitrix\Main\ArgumentException
;
12
use
Bitrix\Main\Error
;
13
use
Bitrix\Main\Localization\Loc
;
14
use
Bitrix\Main\Result
;
15
16
Loc::getMessage
(__FILE__);
17
18
class
Configuration
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
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Config\Configuration
Definition
configuration.php:7
Bitrix\Main\Config\Configuration\count
count()
Definition
configuration.php:273
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Sender\Message\Configuration\$options
$options
Definition
configuration.php:30
Bitrix\Sender\Message\Configuration\getId
getId()
Definition
configuration.php:50
Bitrix\Sender\Message\Configuration\getOption
getOption($key)
Definition
configuration.php:177
Bitrix\Sender\Message\Configuration\getOptions
getOptions()
Definition
configuration.php:205
Bitrix\Sender\Message\Configuration\hasOptions
hasOptions()
Definition
configuration.php:195
Bitrix\Sender\Message\Configuration\getView
getView()
Definition
configuration.php:70
Bitrix\Sender\Message\Configuration\$data
$data
Definition
configuration.php:24
Bitrix\Sender\Message\Configuration\getReadonlyView
getReadonlyView($key, $defaultValue=null)
Definition
configuration.php:144
Bitrix\Sender\Message\Configuration\addOption
addOption(ConfigurationOption $option, $targetOptionCode=null, $isInsertAfter=true)
Definition
configuration.php:246
Bitrix\Sender\Message\Configuration\setId
setId($id)
Definition
configuration.php:60
Bitrix\Sender\Message\Configuration\__construct
__construct(array $data=[])
Definition
configuration.php:37
Bitrix\Sender\Message\Configuration\setView
setView($view)
Definition
configuration.php:85
Bitrix\Sender\Message\Configuration\convertToArray
static convertToArray(array $options)
Definition
configuration.php:226
Bitrix\Sender\Message\Configuration\getArrayOptions
getArrayOptions()
Definition
configuration.php:215
Bitrix\Sender\Message\Configuration\$view
$view
Definition
configuration.php:27
Bitrix\Sender\Message\Configuration\$id
$id
Definition
configuration.php:21
Bitrix\Sender\Message\ConfigurationOption
Definition
configurationoption.php:12
Bitrix\Sender\Message\ConfigurationOption\isTemplated
isTemplated()
Definition
configurationoption.php:399
Bitrix\Sender\Message\ConfigurationOption\getType
getType()
Definition
configurationoption.php:210
Bitrix\Sender\Message
Definition
adapter.php:9
modules
sender
lib
message
configuration.php
Создано системой
1.10.0