Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
arrayconverter.php
1<?php
2
4
7
13final class ArrayConverter
14{
21 public static function convertToArray(Format $format): array
22 {
23 return [
24 'code' => $format->getCode(),
25 'name' => $format->getName(),
26 'description' => $format->getDescription(),
27 'delimiter' => $format->getDelimiter(),
28 'languageId' => $format->getLanguageId(),
29 'templateCollection' => self::convertTemplateCollectionToArray(
30 $format->getTemplateCollection()
31 ),
32 'fieldCollection' => self::convertFieldCollectionToArray(
33 $format->getFieldCollection()
34 ),
35 'fieldForUnRecognized' => $format->getFieldForUnRecognized()
36 ];
37 }
38
43 private static function convertTemplateCollectionToArray(Format\TemplateCollection $templateCollection): array
44 {
45 $result = [];
46
48 foreach ($templateCollection as $template)
49 {
50 $result[$template->getType()] = $template->getTemplate();
51 }
52
53 return $result;
54 }
55
60 private static function convertFieldCollectionToArray(Format\FieldCollection $fieldCollection): array
61 {
62 $result = [];
63
65 foreach ($fieldCollection as $field)
66 {
67 $result[] = [
68 'sort' => $field->getSort(),
69 'type' => $field->getType(),
70 'name' => $field->getName(),
71 'description' => $field->getDescription()
72 ];
73 }
74
75 return $result;
76 }
77
86 public static function convertFromArray(array $data, string $languageId): Format
87 {
88 $result = (new Format($languageId))
89 ->setName((string)$data['name'])
90 ->setDescription((string)$data['description'])
91 ->setDelimiter((string)$data['delimiter'])
92 ->setCode((string)$data['code'])
93 ->setFieldForUnRecognized($data['fieldForUnRecognized']);
94
95 foreach ($data['fieldCollection'] as $field)
96 {
97 $result->getFieldCollection()->addItem(
98 (new Format\Field((int)$field['type']))
99 ->setName((string)$field['name'])
100 ->setDescription((string)$field['description'])
101 ->setSort((int)$field['sort'])
102 );
103 }
104
105 foreach ($data['templateCollection'] as $type => $template)
106 {
107 $result->getTemplateCollection()->addItem(
108 new Format\Template($type, $template)
109 );
110 }
111
112 return $result;
113 }
114}
static convertFromArray(array $data, string $languageId)