Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ormconverter.php
1<?php
2
4
7use Bitrix\Location\Model\EO_Source;
9
15final class OrmConverter
16{
24 public function convertToOrm(Source $source): EO_Source
25 {
29 $result = SourceTable::getById($source->getCode())->fetchObject();
30 if (!$result)
31 {
32 $result = (new EO_Source())
33 ->setCode($source->getCode());
34 }
35
36 return $result
37 ->setName($source->getName())
38 ->setConfig($this->buildConfigString($source->getConfig()));
39 }
40
45 public function convertFromOrm(EO_Source $ormSource): Source
46 {
47 return Factory::makeSource($ormSource->getCode())
48 ->setName($ormSource->getName())
49 ->setConfig($this->buildConfig($ormSource->getConfig()));
50 }
51
56 private function buildConfig(string $configString): ?Config
57 {
58 if (!$configString)
59 {
60 return null;
61 }
62
63 if (!CheckSerializedData($configString))
64 {
65 return null;
66 }
67
68 $result = new Config();
69
70 $configArray = unserialize($configString, ['allowed_classes' => false]);
71 foreach ($configArray as $configArrayItem)
72 {
73 $result->addItem(
74 $this->convertArrayToConfigItem($configArrayItem)
75 );
76 }
77
78 return $result;
79 }
80
85 private function buildConfigString(?Config $config)
86 {
87 if (is_null($config))
88 {
89 return '';
90 }
91
92 $configArray = [];
94 foreach ($config as $configItem)
95 {
96 $configArray[] = $this->convertConfigItemToArray($configItem);
97 }
98
99 return serialize($configArray);
100 }
101
106 private function convertConfigItemToArray(ConfigItem $configItem)
107 {
108 return [
109 'code' => $configItem->getCode(),
110 'type' => $configItem->getType(),
111 'is_visible' => $configItem->isVisible(),
112 'sort' => $configItem->getSort(),
113 'value' => $configItem->getValue(),
114 ];
115 }
116
121 private function convertArrayToConfigItem(array $array)
122 {
123 if (!isset($array['code']))
124 {
125 throw new RuntimeException('code is not specified');
126 }
127
128 $result = new ConfigItem($array['code'], $array['type']);
129
130 if (isset($array['is_visible']))
131 {
132 $result->setIsVisible($array['is_visible']);
133 }
134 if (isset($array['sort']))
135 {
136 $result->setSort((int)$array['sort']);
137 }
138 if (isset($array['value']))
139 {
140 $result->setValue(isset($array['value']) ? (string)$array['value'] : null);
141 }
142
143 return $result;
144 }
145}