diff --git a/lib/zin/core/setting.class.php b/lib/zin/core/setting.class.php new file mode 100644 index 0000000000..7228a26a46 --- /dev/null +++ b/lib/zin/core/setting.class.php @@ -0,0 +1,122 @@ + + * @package zin + * @version $Id + * @link https://www.zentao.net + */ + +namespace zin; + +require_once dirname(__DIR__) . DS . 'utils' . DS . 'dataset.class.php'; + +class setting extends \zin\utils\dataset +{ + /** + * Set property values. + * + * @access public + * @param string $name - Property name. + * @param array $args - Property values. + * @return setting + */ + public function setValues(string $name, mixed ...$args): setting + { + if(empty($args)) + { + $value = true; + } + elseif(($name === 'url' || $name === 'href' || $name === 'link') && count($args) > 1) + { + /* Support to set url with createLink params. */ + $value = call_user_func_array('\helper::createLink', $args); + } + else if(count($args) > 1) + { + $value = array(); + foreach($args as $key => $val) + { + $value[$key] = ($val instanceof setting) ? $val->toArray() : $val; + } + } + else + { + $value = array_shift($args); + } + return $this->setVal($name, $value); + } + + /** + * Method for sub class to hook on setting it. + * + * @access protected + * @param string $prop Property name or properties list. + * @param mixed $value Property value. + * @return setting + */ + protected function setVal(string $prop, mixed $value): setting + { + if($value instanceof setting) $value = $value->toArray(); + $this->_data[$prop] = $value; + return $this; + } + + /** + * Convert to directive. + * + * @access protected + * @param string $type - Directive type. + * @return directive + */ + public function toDirective(string $type = 'prop'): directive + { + return new directive($type, $this->_data); + } + + /** + * Magic method for setting property value. + * + * @access public + * @param string $name - Property name. + * @param array $args - Property values. + * @return setting + */ + public function __call(string $name, array $args): setting + { + return $this->setValues($name, ...$args); + } + + /** + * Magic static method for setting property value. + * + * @access public + * @param string $name - Property name. + * @param array $args - Property values. + * @return setting + */ + public static function __callStatic($name, $args): setting + { + /* Compatible with zui prop className. */ + if($name === '_className') $name = 'className'; + + $set = new setting(); + + return $set->setValues($name, ...$args); + } +} + +/** + * Create a setting instance. + * + * @param array|string $setting - Setting data or setting property name. + * @param mixed $value - Setting value. + * @return setting + */ +function setting(array|string $setting = null, mixed $value = null): setting +{ + return new setting($setting, $value); +} diff --git a/lib/zin/core/wg.class.php b/lib/zin/core/wg.class.php index 4984dd1785..883e4c6d57 100644 --- a/lib/zin/core/wg.class.php +++ b/lib/zin/core/wg.class.php @@ -14,6 +14,7 @@ namespace zin; require_once __DIR__ . DS . 'props.class.php'; require_once __DIR__ . DS . 'directive.class.php'; +require_once __DIR__ . DS . 'setting.class.php'; require_once __DIR__ . DS . 'zin.class.php'; require_once __DIR__ . DS . 'context.class.php'; require_once __DIR__ . DS . 'selector.func.php'; @@ -380,10 +381,12 @@ class wg zin::disableGlobalRender(); - if($item instanceof wg) $this->addToBlock($blockName, $item); - elseif(is_string($item)) $this->addToBlock($blockName, htmlspecialchars($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, null, false)); - elseif(isDirective($item)) $this->directive($item, $blockName); - else $this->addToBlock($blockName, htmlspecialchars(strval($item), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, null, false)); + if($item instanceof setting) $item = $item->toDirective(); + + if($item instanceof wg) $this->addToBlock($blockName, $item); + elseif(is_string($item)) $this->addToBlock($blockName, htmlspecialchars($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, null, false)); + elseif(isDirective($item)) $this->directive($item, $blockName); + else $this->addToBlock($blockName, htmlspecialchars(strval($item), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, null, false)); zin::enableGlobalRender(); diff --git a/lib/zin/core/wg.func.php b/lib/zin/core/wg.func.php index b441afc766..cdddfdce2a 100644 --- a/lib/zin/core/wg.func.php +++ b/lib/zin/core/wg.func.php @@ -14,6 +14,7 @@ namespace zin; require_once dirname(__DIR__) . DS . 'utils' . DS . 'flat.func.php'; require_once __DIR__ . DS . 'props.class.php'; require_once __DIR__ . DS . 'directive.class.php'; +require_once __DIR__ . DS . 'setting.class.php'; require_once __DIR__ . DS . 'rawcontent.class.php'; require_once __DIR__ . DS . 'wg.class.php'; require_once __DIR__ . DS . 'context.func.php';