From 2bb12c62984842b096129a2d37227fc406fb9edd Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Wed, 7 May 2025 14:34:51 +0800 Subject: [PATCH] * [story#74518] add function: setValueByPath. --- module/screen/model.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/module/screen/model.php b/module/screen/model.php index 897fc4501a..28294947b0 100644 --- a/module/screen/model.php +++ b/module/screen/model.php @@ -337,6 +337,40 @@ class screenModel extends model return $component; } + /** + * 通过路径设置值。 + * Set value by path. + * + * @param object $option + * @param string $path + * @param string $value + * @access public + * @return void + */ + public function setValueByPath(object &$option, string $path, mixed $value): void + { + $keys = explode('.', $path); + + $current = &$option; + foreach ($keys as $key) { + if(is_numeric($key)) + { + if(!isset($current[$key])) $current[$key] = array(); + } + else + { + if(!isset($current->$key)) $current->$key = new stdclass(); + } + + if (is_array($current)) { + $current = &$current[$key]; + } else { + $current = &$current->$key; + } + } + $current = $value; + } + /** * Update piovt or chart component chartConfig filters. *