* zin: support to specific key on set items.

This commit is contained in:
sunhao
2024-01-12 16:12:53 +08:00
parent 12c6cb7834
commit c7eeebc50d
3 changed files with 32 additions and 21 deletions
+4 -4
View File
@@ -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)
+19 -3
View File
@@ -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));
}
/**
+9 -14
View File
@@ -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