From 7f0f08f898a6d612e9290664d2d0b8bd6c5faf09 Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Fri, 13 Jun 2025 02:34:56 +0000 Subject: [PATCH 1/4] * [bug] fix setValueByPath. --- module/screen/model.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/module/screen/model.php b/module/screen/model.php index fd2f426c52..76322466a0 100644 --- a/module/screen/model.php +++ b/module/screen/model.php @@ -350,17 +350,18 @@ 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]; From 1ba2f6dc40395312f6f7e28a14eb17d73c4a6f65 Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Fri, 13 Jun 2025 02:35:39 +0000 Subject: [PATCH 2/4] * [misc] add unit test: setValueByPath. --- module/screen/test/lib/screen.unittest.class.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/module/screen/test/lib/screen.unittest.class.php b/module/screen/test/lib/screen.unittest.class.php index 5a8ffa1011..c87f924945 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 object + */ + public function setValueByPathTest(object &$option, string $path, mixed $value): object + { + return $this->objectModel->setValueByPath($option, $path, $value); + } + /** * 初始化过滤条件。 * Initialize filter conditions. From 58944f3d3efac99cc8196ca04b231b218fb2ded2 Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Fri, 13 Jun 2025 03:03:52 +0000 Subject: [PATCH 3/4] * [misc] finish unit test: setValueByPath. --- .../screen/test/lib/screen.unittest.class.php | 6 +-- module/screen/test/model/setvaluebypath.php | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 module/screen/test/model/setvaluebypath.php diff --git a/module/screen/test/lib/screen.unittest.class.php b/module/screen/test/lib/screen.unittest.class.php index c87f924945..67d7f780d1 100755 --- a/module/screen/test/lib/screen.unittest.class.php +++ b/module/screen/test/lib/screen.unittest.class.php @@ -438,11 +438,11 @@ class screenTest * @param string $path * @param string $value * @access public - * @return object + * @return void */ - public function setValueByPathTest(object &$option, string $path, mixed $value): object + public function setValueByPathTest(object &$option, string $path, mixed $value): void { - return $this->objectModel->setValueByPath($option, $path, $value); + $this->objectModel->setValueByPath($option, $path, $value); } /** 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 From 06dbcc00026b3b4e99b46a2a82cd96a54d917270 Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Fri, 13 Jun 2025 03:09:04 +0000 Subject: [PATCH 4/4] * [misc] fix sonar. --- module/screen/model.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module/screen/model.php b/module/screen/model.php index 76322466a0..0597012cd8 100644 --- a/module/screen/model.php +++ b/module/screen/model.php @@ -353,7 +353,8 @@ class screenModel extends model $keyCount = count($keys); $current = &$option; - foreach ($keys as $index => $key) { + foreach ($keys as $index => $key) + { $isEnd = ($index + 1) >= $keyCount; $nextKey = $isEnd ? null : $keys[$index + 1];