Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Keyboard.php
1<?php
2
4
5use Bitrix\Im;
9
10class Keyboard extends Param
11{
13 protected bool $isValid = true;
14
19 public function setValue($value): self
20 {
21 if ($value === null || $value === $this->getDefaultValue())
22 {
23 return $this->unsetValue();
24 }
25
26 if ($value instanceof Im\Bot\Keyboard)
27 {
28 $this->keyboard = $value;
29 }
30 elseif (!empty($value))
31 {
32 $this->keyboard = Im\Bot\Keyboard::getKeyboardByJson($value);
33 $this->isValid = $this->keyboard !== null;
34 }
35
36 if (isset($this->keyboard))
37 {
38 $this->value = $this->keyboard->getArray();
39 $this->jsonValue = $this->keyboard->getJson();
40 }
41
42 return $this;
43 }
44
48 public function getValue()
49 {
50 return $this->value ?? $this->getDefaultValue();
51 }
52
53 public function getDefaultValue()
54 {
55 return 'N';
56 }
57
62 public function saveValueFilter($value)
63 {
64 return '';
65 }
66
71 public function loadValueFilter($value)
72 {
73 if (!empty($value))
74 {
75 $value = Im\Text::decodeEmoji($value);
76 }
77 else
78 {
79 $value = null;
80 }
81
82 return $value;
83 }
84
85
90 public function saveJsonFilter($value)
91 {
92 return $this->jsonValue;
93 }
94
99 public function loadJsonFilter($value)
100 {
101 if (!empty($value))
102 {
103 try
104 {
105 $this->value = \Bitrix\Main\Web\Json::decode($value);
106 }
107 catch (ArgumentException $ext)
108 {}
109 }
110 else
111 {
112 $value = null;
113 }
114
115 return $value;
116 }
117
121 public function toRestFormat()
122 {
123 return $this->getValue();
124 }
125
129 public function toPullFormat()
130 {
131 return $this->getValue();
132 }
133
137 public function isValid(): Result
138 {
139 $result = new Result();
140
141 if ($this->isValid && (!isset($this->keyboard) || $this->keyboard->IsAllowSize()))
142 {
143 return $result;
144 }
145
146 return $result->addError(new ParamError(ParamError::KEYBOARD_ERROR));
147 }
148}