13 private int $iBlockId;
14 private int $sectionId;
15 private array $values;
16 private int $createdBy;
24 private function __construct(
31 $this->iBlockId = $iBlockId;
32 $this->sectionId = $sectionId;
33 $this->values = $values;
34 $this->createdBy = $createdBy;
45 if ($iBlockId ===
null || $iBlockId === 0)
51 if ($sectionId ===
null)
57 if ($createdBy ===
null || $createdBy === 0)
62 $values = self::validateValues($request->values, $iBlockId, $sectionId);
64 return new self($iBlockId, $sectionId, $values, $createdBy);
73 private static function validateValues(array $values,
int $iBlockId,
int $sectionId): array
76 $values[
'IBLOCK_ID'] = $iBlockId;
77 $values[
'IBLOCK_SECTION_ID'] = $sectionId;
87 return $this->iBlockId;
95 return $this->sectionId;
103 return $this->values;
111 return $this->createdBy;
120 return $this->values[$fieldsId] ??
null;
133 $preparedFields = $this->prepareFields($fields, $result);
134 $element = array_merge($element, $preparedFields);
135 unset($element[
'TIMESTAMP_X']);
137 $preparedProps = $this->prepareProps($props, $result);
140 $element[
'PROPERTY_VALUES'] = $preparedProps;
143 return $result->setData([
'element' => $element]);
146 private function prepareFields(array $fields,
Result $result): array
149 foreach ($fields as $fieldId => $property)
151 $showAddForm = !in_array($fieldId, [
'DATE_CREATE',
'TIMESTAMP_X',
'CREATED_BY',
'MODIFIED_BY']);
152 if (isset($property[
'SETTINGS'][
'SHOW_ADD_FORM']))
154 $showAddForm = $property[
'SETTINGS'][
'SHOW_ADD_FORM'] ===
'Y';
157 if (in_array($fieldId, [
'PREVIEW_TEXT',
'DETAIL_TEXT'],
true))
159 $useEditor = isset($property[
'SETTINGS'][
'USE_EDITOR']) && $property[
'SETTINGS'][
'USE_EDITOR'] ===
'Y';
160 $prepared[$fieldId .
'_TYPE'] = $useEditor ?
'html' :
'text';
163 $prepared[$fieldId] =
164 $showAddForm ? $this->
getFieldValueById($fieldId) : ($property[
'DEFAULT_VALUE'] ?? null)
171 private function prepareProps(array $props, Result $result): array
174 foreach ($props as $propId => $property)
176 $baseType = $property[
'TYPE'];
177 $type = $property[
'PROPERTY_TYPE'];
178 $isMultiple = $property[
'MULTIPLE'] ===
'Y';
181 isset($property[
'SETTINGS'][
'SHOW_ADD_FORM']) && $property[
'SETTINGS'][
'SHOW_ADD_FORM'] ===
'Y'
183 $defaultValue = ($property[
'DEFAULT_VALUE'] ??
null);
186 if ($showAddForm && is_array($requestValues))
189 foreach ($requestValues as $key => $value)
191 if (in_array($type, [
'L',
'E',
'G'],
true))
195 $values[$key] = $value;
203 if ($type ===
'N' && !empty($value))
205 if (is_numeric($value))
207 $value = (double)$value;
212 $result->addError(
new Error(
'incorrect number value'));
217 if ($type ===
'S' && $baseType ===
'S:HTML')
220 is_array($value) && isset($value[
'TYPE'], $value[
'TEXT'])
222 : [
'TYPE' =>
'html',
'TEXT' => (string)$value]
226 $values[$key] = [
'VALUE' => $value];
234 $prepared[$property[
'ID']] = $values;
236 elseif ($defaultValue)
238 $prepared[$property[
'ID']] = [
239 'n0' => [
'VALUE' => $defaultValue],