diff --git a/module/screen/model.php b/module/screen/model.php index fd2f426c52..0597012cd8 100644 --- a/module/screen/model.php +++ b/module/screen/model.php @@ -350,17 +350,19 @@ class screenModel extends model public function setValueByPath(object &$option, string $path, mixed $value): void { $keys = explode('.', $path); + $keyCount = count($keys); $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(); - } + foreach ($keys as $index => $key) + { + $isEnd = ($index + 1) >= $keyCount; + $nextKey = $isEnd ? null : $keys[$index + 1]; + + $isArray = is_numeric($key); + $nextIsArray = is_numeric($nextKey); + + if($isArray && !isset($current[$key])) $current[$key] = $nextIsArray ? array() : new stdclass(); + if(!$isArray && !isset($current->$key)) $current->$key = $nextIsArray ? array() : new stdclass(); if (is_array($current)) { $current = &$current[$key]; diff --git a/module/screen/test/lib/screen.unittest.class.php b/module/screen/test/lib/screen.unittest.class.php index 5a8ffa1011..67d7f780d1 100755 --- a/module/screen/test/lib/screen.unittest.class.php +++ b/module/screen/test/lib/screen.unittest.class.php @@ -431,6 +431,20 @@ class screenTest return $this->objectModel->filter; } + /** + * Test set value by path. + * + * @param object $option + * @param string $path + * @param string $value + * @access public + * @return void + */ + public function setValueByPathTest(object &$option, string $path, mixed $value): void + { + $this->objectModel->setValueByPath($option, $path, $value); + } + /** * 初始化过滤条件。 * Initialize filter conditions. diff --git a/module/screen/test/model/setvaluebypath.php b/module/screen/test/model/setvaluebypath.php new file mode 100644 index 0000000000..195ffc0d54 --- /dev/null +++ b/module/screen/test/model/setvaluebypath.php @@ -0,0 +1,44 @@ +#!/usr/bin/env php +setValueByPath(); +timeout=0 +cid=1 + +- 测试title.show=true @1 +- 测试series.0.color.0.colorStops.0.offset=1 @1 +- 测试series.0.color.0.x=#fff @#fff +- 测试series.0.color.1.y=#000 @#000 +- 测试series.0.0.name=data @data + +*/ + +$screen = new screenTest(); +$paths = array +( + 'title.show', + 'series.0.color.0.colorStops.0.offset', + 'series.0.color.0.x', + 'series.0.color.1.y', + 'series.0.0.name', +); + +$options = new stdclass(); +$screen->setValueByPathTest($options, $paths[0], true); +$screen->setValueByPathTest($options, $paths[1], 1); +$screen->setValueByPathTest($options, $paths[2], '#fff'); +$screen->setValueByPathTest($options, $paths[3], '#000'); + +r($options->title->show) && p('') && e("1"); //测试title.show=true +r($options->series[0]->color[0]->colorStops[0]->offset) && p('') && e("1"); //测试series.0.color.0.colorStops.0.offset=1 +r($options->series[0]->color[0]->x) && p('') && e("#fff"); //测试series.0.color.0.x=#fff +r($options->series[0]->color[1]->y) && p('') && e("#000"); //测试series.0.color.1.y=#000 + +$options = new stdclass(); +$screen->setValueByPathTest($options, $paths[4], 'data'); + +r($options->series[0][0]->name) && p('') && e("data"); //测试series.0.0.name=data \ No newline at end of file