Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Param.php
1<?php
2
4
7use Bitrix\Im\Model\EO_MessageParam;
12use Bitrix\Im\V2\Common\ActiveRecordImplementation;
13use Bitrix\Im\V2\Common\RegistryEntryImplementation;
14
19{
20 use ActiveRecordImplementation
21 {
22 load as defaultLoad;
23 }
24 use RegistryEntryImplementation;
25
26 public const
27 TYPE_STRING = 'String',
28 TYPE_INT = 'Integer',
29 TYPE_BOOL = 'Boolean',
30 TYPE_STRING_ARRAY = 'ArrayString',
31 TYPE_INT_ARRAY = 'ArrayInteger',
32 TYPE_DATE_TIME = 'DateTime',
33 TYPE_JSON = 'Json'
34 ;
35
36 protected ?string $type = null;
37
38 protected ?int $paramId = null;
39
40 protected ?int $messageId = null;
41
42 protected ?string $name = null;
43
45 protected $value = null;
46
48 protected ?string $jsonValue = null;
49
51 protected $defaultValue = null;
52
56 public function __construct($source = null)
57 {
58 if (!empty($source))
59 {
60 $this->load($source);
61 }
62 }
63
67 public function load($source): Result
68 {
69 $result = $this->defaultLoad($source);
70 if ($result->isSuccess())
71 {
72 $checkType = $this->getType();
73 $this->detectType();
74 if ($this->type !== $checkType)
75 {
76 $this->setValue($this->value);
77 }
78
79 $type = Params::getType($this->name);
80 if (isset($type['loadValueFilter']) && is_callable($type['loadValueFilter']))
81 {
82 $this->value = \call_user_func($type['loadValueFilter'], $this->value);
83 }
84 }
85
86 return $result;
87 }
88
89 //region Param value
90
95 public function setValue($value): self
96 {
97 if ($value === null)
98 {
99 return $this->unsetValue();
100 }
101
102 switch ($this->type)
103 {
104 case self::TYPE_INT:
105 $this->value = (int)$value;
106 break;
107
108 case self::TYPE_BOOL:
109 if (is_string($value))
110 {
111 $this->value = $value === 'Y';
112 }
113 else
114 {
115 $this->value = (bool)$value;
116 }
117 break;
118
119 case self::TYPE_STRING:
120 $this->value = (string)$value;
121 break;
122
123 default:
124 $this->value = $value;
125 }
126
128 if ($this->value === $defaultValue)
129 {
130 return $this->unsetValue();
131 }
132
133 $this->markChanged();
134
135 return $this;
136 }
137
141 public function getDefaultValue()
142 {
143 if ($this->defaultValue === null)
144 {
145 $type = Params::getType($this->name);
146 if (isset($type['default']))
147 {
148 $value = $type['default'];
149
150 switch ($this->type)
151 {
152 case self::TYPE_INT:
153 $this->defaultValue = (int)$value;
154 break;
155
156 case self::TYPE_BOOL:
157 $this->defaultValue = (bool)$value;
158 break;
159
160 case self::TYPE_STRING:
161 $this->defaultValue = (string)$value;
162 break;
163
164 default:
165 $this->defaultValue = $value;
166 }
167 }
168 }
169
170 return $this->defaultValue;
171 }
172
176 public function hasValue(): bool
177 {
178 return $this->value !== null;
179 }
180
184 public function getValue()
185 {
186 if ($this->value === null)
187 {
188 return $this->getDefaultValue();
189 }
190 switch ($this->type)
191 {
192 case self::TYPE_INT:
193 return (int)$this->value;
194
195 case self::TYPE_BOOL:
196 if (is_string($this->value))
197 {
198 $this->value = $this->value === 'Y';
199 }
200 return (bool)$this->value;
201
202 case self::TYPE_STRING:
203 return (string)$this->value;
204
205 default:
206 return $this->value;
207 }
208 }
209
214 public function addValue($value): self
215 {
216 return $this->setValue($value);
217 }
218
222 public function unsetValue(): self
223 {
224 $this->value = null;
225 $this->markDrop();
226
227 if ($this->getRegistry())
228 {
229 unset($this->getRegistry()[$this->getName()]);
230 }
231
232 return $this;
233 }
234
235 public function isHidden(): bool
236 {
237 return Params::getType($this->name)['isHidden'] ?? false;
238 }
239
243 public function toRestFormat()
244 {
245 switch ($this->type)
246 {
247 case self::TYPE_BOOL:
248 return $this->getValue() ? 'Y' : 'N';
249
250 case self::TYPE_INT:
251 return (string)$this->getValue();
252
253 case self::TYPE_STRING:
254 default:
255 return $this->getValue();
256
257 }
258 }
259
263 public function toPullFormat()
264 {
265 return $this->toRestFormat();
266 }
267
268 //endregion
269
270 //region Setters & Getters
271
272 public function setParamId(int $paramId): self
273 {
274 if (!$this->paramId)
275 {
276 $this->paramId = $paramId;
277 }
278 return $this;
279 }
280
281 public function getParamId(): ?int
282 {
283 return $this->paramId;
284 }
285
286 public function setMessageId(int $messageId): self
287 {
288 if ($this->messageId != $messageId)
289 {
290 $this->markChanged();
291 }
292 $this->messageId = $messageId;
293 return $this;
294 }
295
296 public function getMessageId(): ?int
297 {
298 return $this->messageId;
299 }
300
301 public function setName(string $name): self
302 {
303 $name = mb_substr(trim($name), 0, 100);
304 if ($this->name != $name)
305 {
306 $this->markChanged();
307 }
308 $this->name = $name;
309 $this->detectType();
310
311 return $this;
312 }
313
314 public function getName(): ?string
315 {
316 return $this->name;
317 }
318
319 public function setType(string $type): self
320 {
321 switch ($type)
322 {
324 $type = Param::TYPE_INT;
325 break;
326
328 $type = Param::TYPE_STRING;
329 }
330 if ($this->type != $type)
331 {
332 $this->markChanged();
333 }
334 $this->type = $type;
335
336 return $this;
337 }
338
339 public function getType(): string
340 {
341 return $this->type ?? Param::TYPE_STRING;
342 }
343
344 public function detectType(): self
345 {
346 if (empty($this->type) && !empty($this->name))
347 {
348 $type = Params::getType($this->name);
349 $this->setType($type['type'] ?? Param::TYPE_STRING);
350 }
351
352 return $this;
353 }
354
355 public function setJsonValue($value): self
356 {
357 $this->jsonValue = $value;
358 $this->markChanged();
359
360 return $this;
361 }
362
363 public function getJsonValue()
364 {
365 return $this->jsonValue;
366 }
367
368 //endregion
369
370 //region Data storage
371
375 protected static function mirrorDataEntityFields(): array
376 {
377 return [
378 'ID' => [
379 'primary' => true,
380 'field' => 'paramId',
381 'get' => 'getParamId',
382 'set' => 'setParamId',
383 ],
384 'MESSAGE_ID' => [
385 'field' => 'messageId',
386 'set' => 'setMessageId',
387 'get' => 'getMessageId',
388 ],
389 'TYPE' => [
390 'set' => 'setType',
391 'get' => 'getType',
392 ],
393 'PARAM_NAME' => [
394 'field' => 'name',
395 'set' => 'setName',
396 'get' => 'getName',
397 ],
398 'PARAM_VALUE' => [
399 'field' => 'value',
400 'set' => 'setValue',
401 'get' => 'getValue',
402 'saveFilter' => 'saveValueFilter',
403 'loadFilter' => 'loadValueFilter',
404 ],
405 'PARAM_JSON' => [
406 'field' => 'jsonValue',
407 'set' => 'setJsonValue',
408 'get' => 'getJsonValue',
409 'saveFilter' => 'saveJsonFilter',
410 'loadFilter' => 'loadJsonFilter',
411 ],
412 ];
413 }
414
418 public static function getDataClass(): string
419 {
420 return MessageParamTable::class;
421 }
422
426 public function getPrimaryId(): ?int
427 {
428 return $this->getParamId();
429 }
430
435 public function setPrimaryId(int $primaryId): self
436 {
437 return $this->setParamId($primaryId);
438 }
439
440 public function isValid(): Result
441 {
442 return new Result();
443 }
444
449 public function saveValueFilter($value)
450 {
451 $type = Params::getType($this->name);
452
453 if (
454 isset($type['saveValueFilter'])
455 && ($saveFilter = $type['saveValueFilter'])
456 )
457 {
458 if (is_string($saveFilter) && is_callable([$this, $saveFilter]))
459 {
460 $value = $this->$saveFilter($value);
461 }
462 elseif (is_callable($saveFilter))
463 {
464 $value = call_user_func($saveFilter, $value);
465 }
466 }
467 elseif ($type['type'] == Param::TYPE_BOOL)
468 {
469 $value = $value ? 'Y' : 'N';
470 }
471
472 return $value;
473 }
474
479 public function loadValueFilter($value)
480 {
481 $type = Params::getType($this->name);
482
483 if (
484 isset($type['loadValueFilter'])
485 && ($loadFilter = $type['loadValueFilter'])
486 )
487 {
488 if (is_string($loadFilter) && is_callable([$this, $loadFilter]))
489 {
490 $value = $this->$loadFilter($value);
491 }
492 elseif (is_callable($loadFilter))
493 {
494 $value = call_user_func($loadFilter, $value);
495 }
496 }
497 elseif (($type['type'] ?? null) == Param::TYPE_BOOL) //TODO replace to normal variant
498 {
499 $value = $value == 'Y';
500 }
501
502 return $value;
503 }
504
509 public function saveJsonFilter($value)
510 {
511 return $value;
512 }
513
518 public function loadJsonFilter($value)
519 {
520 return $value;
521 }
522
523 //endregion
524}
setPrimaryId(int $primaryId)
Definition Param.php:435
setParamId(int $paramId)
Definition Param.php:272
setName(string $name)
Definition Param.php:301
__construct($source=null)
Definition Param.php:56
setMessageId(int $messageId)
Definition Param.php:286
static mirrorDataEntityFields()
Definition Param.php:375
setType(string $type)
Definition Param.php:319
static getType(string $paramName)
Definition Params.php:277
getRegistry()