Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
setup.php
1<?php
2
4
10
11final class Setup implements IConfig
12{
13 // public constants
14 const BUSINESS_ID = 'external_business_id';
15 const TIMEZONE = 'timezone';
16 const CURRENCY = 'currency';
17 const CHANNEL = 'channel';
18 const BUSINESS_TYPE = 'business_vertical';
19
21 private static $current;
22
24 private $value = [];
25
26 // non public constants
27 private const MODULE_ID = 'seo';
28 private const CONFIG_OPTION = '~facebook_business_setup';
29
30 private const FIELDS_MAP = [
31 self::CHANNEL => SetupFields\Channel::class,
32 self::CURRENCY => SetupFields\Currency::class,
33 self::TIMEZONE => SetupFields\Timezone::class,
34 self::BUSINESS_ID => SetupFields\BusinessId::class,
35 self::BUSINESS_TYPE => SetupFields\BusinessType::class,
36 ];
37
39 private static $fieldInstances = [];
40
41 private static function getField(string $name) : Fields\IField
42 {
43 if($field = self::FIELDS_MAP[$name])
44 {
45 return static::$fieldInstances[$name] = static::$fieldInstances[$name] ?? new $field;
46 }
48 }
49
53 public static function create(): IConfig
54 {
55 return new self();
56 }
57
61 public function __construct()
62 {
63 $this->value[self::BUSINESS_ID] = SetupFields\BusinessId::getDefaultValue();
64 }
65
70 public function toArray(): array
71 {
72 return array_reduce(
73 array_keys(self::FIELDS_MAP),
74 (function (array $result,string $code) : array
75 {
76 $field = $this::getField($code);
77 if($field::available())
78 {
79 if($value = $this->get($code))
80 {
81 $result[$code] = $value;
82 }
83 elseif($field::required())
84 {
86 }
87 }
88 return $result;
89
90 })->bindTo($this,$this),
91 array()
92 );
93 }
94
95 public function set(string $name, $value): IConfig
96 {
97 if(self::getField($name)::available())
98 {
99 if(self::getField($name)::checkValue($value))
100 {
101 $this->value[$name] = $value;
102 return $this;
103 }
105 }
106 return $this;
107 }
108
109 public function get(string $name)
110 {
111 return $this->value[$name] ?? null;
112 }
113
114 public function delete() : void
115 {
116 Config\Option::delete(self::MODULE_ID,['name' => self::CONFIG_OPTION]);
117 }
118
119 public function save() : bool
120 {
121 try
122 {
123 Config\Option::set(self::MODULE_ID,self::CONFIG_OPTION,Json::encode($this->value));
124 }
125 catch (\Throwable $exception)
126 {
127 return false;
128 }
129 return true;
130 }
131
132 public function jsonSerialize() : array
133 {
134 return array_reduce(
135 array_keys(self::FIELDS_MAP),
136 function(array $result, string $code) : array
137 {
138 $field = static::getField($code);
139 if($field::available())
140 {
141 $result[$code] = ['value' => $this->value[$code] ?? $field::getDefaultValue()];
142 if($field instanceof Fields\IAvailableFieldList)
143 {
144 $result[$code]['set'] = $field::getAvailableValues();
145 }
146 }
147 return $result;
148 },
149 array()
150 );
151 }
152
156 public static function load(): ?IConfig
157 {
158 if(!self::$current)
159 {
160 if($data = Config\Option::get(self::MODULE_ID,self::CONFIG_OPTION,false))
161 {
162 [$data,self::$current] = [Json::decode($data),self::default()];
163 foreach ($data as $key => $value)
164 {
165 self::$current->set($key,$value ?? self::getField($key)::getDefaultValue());
166 }
167 }
168 }
169
170 return self::$current;
171 }
172
177 public static function default(): IConfig
178 {
179 return array_reduce(
180 array_keys(self::FIELDS_MAP),
181 function(IConfig $instance,$code)
182 {
183 return $instance->set($code,self::getField($code)::getDefaultValue());
184 },
186 );
187 }
188
195 public static function loadFromArray(array $array) : IConfig
196 {
197 return array_reduce(
198 array_keys(self::FIELDS_MAP),
199 function(IConfig $instance,string $code) use ($array)
200 {
201 return ((array_key_exists($code,$array)) ? $instance->set($code,$array[$code]) : $instance);
202 },
204 );
205 }
206}