Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Attach.php
1<?php
2
4
8
9class Attach extends Param
10{
11 private ?\CIMMessageParamAttach $attach = null;
12 private bool $isValid = true;
13
14 protected ?string $type = Param::TYPE_JSON;
15
20 public function setValue($value): self
21 {
22 if ($value === null || $value === $this->getDefaultValue())
23 {
24 return $this->unsetValue();
25 }
26 if ($value instanceof \CIMMessageParamAttach)
27 {
28 $this->attach = $value;
29 }
30 elseif (!empty($value))
31 {
32 $this->attach = \CIMMessageParamAttach::GetAttachByJson($value);
33 if ($this->attach === null)
34 {
35 $this->isValid = false;
36 }
37 }
38
39 if (isset($this->attach))
40 {
41 $this->value = $this->attach->getArray();
42 $this->jsonValue = $this->attach->getJson();
43 }
44
45 return $this;
46 }
47
51 public function getValue()
52 {
53 return $this->value;
54 }
55
60 public function loadValueFilter($value)
61 {
62 if (!empty($value))
63 {
64 $value = \Bitrix\Im\Text::decodeEmoji($value);
65 }
66 else
67 {
68 $value = null;
69 }
70
71 return $value;
72 }
73
78 public function saveValueFilter($value)
79 {
80 $value = '';
81 if (!empty($this->value['DESCRIPTION']))
82 {
83 $value = \Bitrix\Im\Text::encodeEmoji($this->value['DESCRIPTION']);
84 }
85
86 return $value;
87 }
88
93 public function saveJsonFilter($value)
94 {
95 return $this->jsonValue;
96 }
97
102 public function loadJsonFilter($value)
103 {
104 if (!empty($value))
105 {
106 try
107 {
108 $val = \Bitrix\Main\Web\Json::decode($value);
109 $this->value = \CIMMessageParamAttach::PrepareAttach($val);
110 }
111 catch (ArgumentException $ext)
112 {}
113 }
114 else
115 {
116 $value = null;
117 }
118
119 return $value;
120 }
121
125 public function toRestFormat(): ?array
126 {
127 return $this->getValue();
128 }
129
133 public function toPullFormat()
134 {
135 return \CIMMessageParamAttach::PrepareAttach($this->getValue());
136 }
137
141 public function isValid(): Result
142 {
143 $result = new Result();
144
145 if ($this->isValid && (!isset($this->attach) || $this->attach->IsAllowSize()))
146 {
147 return $result;
148 }
149
150 return $result->addError(new ParamError(ParamError::ATTACH_ERROR));
151 }
152}