From c7eeebc50d0dc8a1609554b2496d1e5a7239ec6e Mon Sep 17 00:00:00 2001 From: sunhao Date: Fri, 12 Jan 2024 16:12:46 +0800 Subject: [PATCH] * zin: support to specific key on set items. --- lib/zin/core/context.class.php | 8 ++++---- lib/zin/utils/dataset.class.php | 22 +++++++++++++++++++--- lib/zin/zentao/field.class.php | 23 +++++++++-------------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/zin/core/context.class.php b/lib/zin/core/context.class.php index c386ad83e6..c10bc02939 100644 --- a/lib/zin/core/context.class.php +++ b/lib/zin/core/context.class.php @@ -22,7 +22,7 @@ class context extends \zin\utils\dataset public function addHookFiles(string|array ...$files) { $files = flat($files); - return $this->addToList('hookFiles', ...$files); + return $this->mergeToList('hookFiles', $files); } public function getHookFiles(): array @@ -32,7 +32,7 @@ class context extends \zin\utils\dataset public function addImport(string ...$files) { - return $this->addToList('import', ...$files); + return $this->mergeToList('import', $files); } public function getImportList(): array @@ -42,7 +42,7 @@ class context extends \zin\utils\dataset public function addCSS(string ...$cssList) { - return $this->addToList('css', ...$cssList); + return $this->mergeToList('css', $cssList); } public function getCSS(): string @@ -52,7 +52,7 @@ class context extends \zin\utils\dataset public function addJS(string ...$jsList) { - return $this->addToList('js', ...$jsList); + return $this->mergeToList('js', $jsList); } public function addJSVar(string $name, mixed $value) diff --git a/lib/zin/utils/dataset.class.php b/lib/zin/utils/dataset.class.php index f9c85c11ab..0e11115ca6 100644 --- a/lib/zin/utils/dataset.class.php +++ b/lib/zin/utils/dataset.class.php @@ -246,13 +246,29 @@ class dataset * * @access public * @param string $prop Property name. - * @param mixed ...$values Values to add to list. + * @param mixed ...$item Item Value to add to list. * @return mixed */ - public function addToList(string $prop, mixed ...$values): dataset + public function addToList(string $prop, mixed $item, ?string $key = null): dataset { $list = $this->getList($prop); - return $this->set($prop, array_merge($list, $values)); + if($key) $list[$key] = $item; + else $list[] = $item; + return $this->set($prop, $list); + } + + /** + * Merge value to array list. + * + * @access public + * @param string $prop Property name. + * @param mixed $newList Values to add to list. + * @return mixed + */ + public function mergeToList(string $prop, $newList): dataset + { + $list = $this->getList($prop); + return $this->set($prop, array_merge($list, $newList)); } /** diff --git a/lib/zin/zentao/field.class.php b/lib/zin/zentao/field.class.php index 9d152ddda0..cce089a04d 100644 --- a/lib/zin/zentao/field.class.php +++ b/lib/zin/zentao/field.class.php @@ -17,7 +17,6 @@ require_once dirname(__DIR__) . DS . 'core' . DS . 'setting.class.php'; require_once dirname(__DIR__) . DS . 'utils' . DS . 'dataset.class.php'; use \zin\fieldList; -use \zin\utils\dataset; class field extends setting { @@ -167,21 +166,17 @@ class field extends setting return $this->setVal('labelHintIcon', $icon); } - function labelActions(array|false|null $actions, bool $reset = false): field + function labelActions(array|false|null $actions, bool $reset = false, ?string $key = null): field { if($actions === false) return $this->remove('actions'); - if($reset) return $this->setVal('actions', $actions); - - if(is_array($actions)) - { - foreach($actions as $item) $this->addToList('actions', $item); - } + if($reset) return $this->setVal('actions', $actions); + if(is_array($actions)) return $this->mergeToList('actions', $actions, $key); return $this; } - function labelAction(mixed ...$actions): field + function labelAction(mixed $action, ?string $key = null): field { - return $this->addToList('labelActions', ...$actions); + return $this->addToList('labelActions', $action, $key); } function labelActionsClass(mixed ...$classList): field @@ -286,7 +281,7 @@ class field extends setting } elseif(is_array($items)) { - foreach($items as $item) $this->addToList('items', $item); + $this->mergeToList('items', $items); } return $this; } @@ -294,9 +289,9 @@ class field extends setting /** * Add item. */ - function item(mixed ...$items): field + function item(mixed $item, ?string $key = null): field { - return $this->addToList('items', ...$items); + return $this->addToList('items', $item, $key); } function itemBegin(?string $itemName = null): field @@ -316,7 +311,7 @@ class field extends setting function children(mixed ...$children): field { - return $this->addToList('children', ...$children); + return $this->mergeToList('children', $children); } function setDefault(string|array|null $default): field