21 private static $current;
27 private const MODULE_ID =
'seo';
28 private const CONFIG_OPTION =
'~facebook_business_setup';
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,
39 private static $fieldInstances = [];
41 private static function getField(
string $name) : Fields\IField
43 if($field = self::FIELDS_MAP[$name])
45 return static::$fieldInstances[$name] = static::$fieldInstances[$name] ??
new $field;
73 array_keys(self::FIELDS_MAP),
74 (
function (array $result,
string $code) : array
76 $field = $this::getField($code);
77 if($field::available())
79 if($value = $this->
get($code))
81 $result[$code] = $value;
83 elseif($field::required())
90 })->bindTo($this,$this),
95 public function set(
string $name, $value):
IConfig
97 if(self::getField($name)::available())
99 if(self::getField($name)::checkValue($value))
101 $this->value[$name] = $value;
109 public function get(
string $name)
111 return $this->value[$name] ??
null;
114 public function delete() : void
116 Config\Option::delete(self::MODULE_ID,[
'name' => self::CONFIG_OPTION]);
123 Config\Option::set(self::MODULE_ID,self::CONFIG_OPTION,Json::encode($this->value));
125 catch (\Throwable $exception)
135 array_keys(self::FIELDS_MAP),
136 function(array $result,
string $code) : array
138 $field = static::getField($code);
139 if($field::available())
141 $result[$code] = [
'value' => $this->value[$code] ?? $field::getDefaultValue()];
142 if($field instanceof Fields\IAvailableFieldList)
144 $result[$code][
'set'] = $field::getAvailableValues();
160 if($data =
Config\Option::get(self::MODULE_ID,self::CONFIG_OPTION,
false))
162 [$data,self::$current] = [Json::decode($data),
self::default()];
163 foreach ($data as $key => $value)
165 self::$current->
set($key,$value ?? self::getField($key)::getDefaultValue());
170 return self::$current;
180 array_keys(self::FIELDS_MAP),
181 function(
IConfig $instance,$code)
183 return $instance->
set($code,self::getField($code)::getDefaultValue());
198 array_keys(self::FIELDS_MAP),
199 function(
IConfig $instance,
string $code) use ($array)
201 return ((array_key_exists($code,$array)) ? $instance->
set($code,$array[$code]) : $instance);