Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stringtype.php
1<?php
2
4
8
13class StringType extends Base
14{
18 public static function getType()
19 {
20 return FieldType::STRING;
21 }
22
30 public static function toSingleValue(FieldType $fieldType, $value)
31 {
32 if (is_array($value))
33 {
34 $value = current(\CBPHelper::makeArrayFlat($value));
35 }
36
37 return $value;
38 }
39
40 public static function externalizeValue(FieldType $fieldType, $context, $value)
41 {
42 if (is_array($value))
43 {
44 return (string)current(\CBPHelper::makeArrayFlat($value));
45 }
46
47 return parent::externalizeValue($fieldType, $context, $value);
48 }
49
56 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
57 {
59 $type = $toTypeClass::getType();
60 switch ($type)
61 {
62 case FieldType::BOOL:
63 $value = mb_strtolower((string)$value);
64 $value = in_array($value, ['y', 'yes', 'true', '1']) ? 'Y' : 'N';
65 break;
66 case FieldType::DATE:
68 $value = (string)$value;
69
70 if (Bizproc\BaseType\Value\DateTime::isSerialized($value))
71 {
72 break;
73 }
74
75 if ($value)
76 {
77 $format = ($type == FieldType::DATE) ? \FORMAT_DATE : \FORMAT_DATETIME;
78 if (\CheckDateTime($value, $format))
79 {
80 $value = date(
81 Main\Type\Date::convertFormatToPhp($format),
82 \CBPHelper::makeTimestamp($value, $format)
83 );
84 }
85 else
86 {
87 $value = date(Main\Type\Date::convertFormatToPhp($format), strtotime($value));
88 }
89 }
90 break;
92 $value = str_replace(' ', '', str_replace(',', '.', $value));
93 $value = (float)$value;
94 break;
95 case FieldType::INT:
96 $value = str_replace(' ', '', $value);
97 $value = (int)$value;
98 break;
100 case FieldType::TEXT:
101 $value = (string)$value;
102 break;
103 case FieldType::USER:
104 $value = trim($value);
105 if (
106 mb_strpos($value, 'user_') === false
107 && mb_strpos($value, 'group_') === false
108 && !preg_match('#^[0-9]+$#', $value)
109 )
110 {
111 $value = null;
112 }
113 break;
114 default:
115 $value = null;
116 }
117
118 return $value;
119 }
120
126 public static function getConversionMap()
127 {
128 return [
129 [
138 ],
139 ];
140 }
141
150 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
151 {
152 if ($allowSelection && !($renderMode & FieldType::RENDER_MODE_PUBLIC))
153 {
154 return static::renderControlSelector($field, $value, 'combine', '', $fieldType);
155 }
156
157 return parent::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
158 }
159
164 public static function canRenderControl($renderMode)
165 {
166 return true;
167 }
168
177 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
178 {
179 $value = static::toSingleValue($fieldType, $value);
180
181 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
182 }
183
192 public static function renderControlMultiple(
193 FieldType $fieldType,
194 array $field,
195 $value,
196 $allowSelection,
197 $renderMode
198 )
199 {
200 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
201 {
202 $value = [$value];
203 }
204
205 if (empty($value))
206 {
207 $value[] = null;
208 }
209
210 $controls = [];
211
212 foreach ($value as $k => $v)
213 {
214 $singleField = $field;
215 $singleField['Index'] = $k;
216 $controls[] = static::renderControl(
217 $fieldType,
218 $singleField,
219 $v,
220 $allowSelection,
221 $renderMode
222 );
223 }
224
225 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
226 {
227 $renderResult = static::renderPublicMultipleWrapper($fieldType, $field, $controls);
228 }
229 else
230 {
231 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
232 }
233
234 return $renderResult;
235 }
236
237 public static function mergeValue(FieldType $fieldType, array $baseValue, $appendValue): array
238 {
239 if (\CBPHelper::isEmptyValue($baseValue))
240 {
241 return (array)$appendValue;
242 }
243
244 if (!is_array($appendValue))
245 {
246 $baseValue[] = $appendValue;
247
248 return $baseValue;
249 }
250
251 if (!\CBPHelper::isAssociativeArray($baseValue) && !\CBPHelper::isAssociativeArray($appendValue))
252 {
253 return array_values(array_merge($baseValue, $appendValue));
254 }
255
256 return $baseValue + $appendValue;
257 }
258}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static externalizeValue(FieldType $fieldType, $context, $value)
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static toSingleValue(FieldType $fieldType, $value)
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
static mergeValue(FieldType $fieldType, array $baseValue, $appendValue)
static canRenderControl($renderMode)