utm ?? null) instanceof stdClass) throw new RuntimeException('Отсутствует объект utm'); $before=serialize($config); foreach (['registration'=>'reg','bchoice'=>'chob'] as $old=>$new) { if (property_exists($config->utm,$old)) { if (property_exists($config->utm,$new)) throw new RuntimeException('Конфликт старого и нового UTM: '.$new); $config->utm->$new=$config->utm->$old; unset($config->utm->$old); } } foreach (['theme'=>['dark'=>'1','light'=>'2'],'reg'=>['before'=>'1','after'=>'2'],'chob'=>['b'=>'1','a'=>'2'],'focus'=>['face'=>'1','center'=>'2']] as $key=>$map) { if (!property_exists($config->utm,$key)) continue; $setting=$config->utm->$key; if (!$setting instanceof stdClass || !($setting->weights ?? null) instanceof stdClass || !(is_string($setting->default ?? null) || is_int($setting->default ?? null))) throw new RuntimeException('Некорректный UTM: '.$key); $weights=new stdClass; foreach ($setting->weights as $variant=>$weight) { $digit=$map[$variant] ?? (string)$variant; if (!in_array($digit,['1','2'],true) || property_exists($weights,$digit)) throw new RuntimeException('Неоднозначный вариант UTM: '.$key); $weights->$digit=$weight; } $default=$map[$setting->default] ?? $setting->default; if (!in_array((string)$default,['1','2'],true)) throw new RuntimeException('Некорректный default UTM: '.$key); $setting->weights=$weights; $setting->default=$default; } if (serialize($config)===$before) { echo "Числовые UTM уже настроены; config не изменён.\n"; exit(0); } $encoded=json_encode($config,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_THROW_ON_ERROR)."\n"; if (file_put_contents($path,$encoded,LOCK_EX)===false) throw new RuntimeException('Не удалось записать config'); echo "Мигрированы только согласованные имена и enum UTM.\n"; exit(10); } catch(Throwable $error) { fwrite(STDERR,$error->getMessage()."\n"); exit(2); }