* @package zin * @version $Id * @link https://www.zentao.net */ namespace zin; require_once dirname(__DIR__) . DS . 'utils' . DS . 'dataset.class.php'; class style extends \zin\utils\dataset implements iDirective { /** * Magic method for setting style. * * @access public * @param string $name - Property name. * @param array $args - Property values. * @return style */ public function __call(string $name, array $args): style { $value = empty($args) ? true : (count($args) === 1 ? $args[0] : $args); return $this->setVal($name, $value); } /** * Method for sub class to hook on setting it. * * @access protected * @param string $name Property name or properties list. * @param mixed $value Property value. * @return style */ protected function setVal(string $name, mixed $value): style { $name = static::formatStyleName($name); if($value === null) { $this->storedData[$name] = $value; return $this; } if(!is_bool($value)) $value = static::formatStyleValue($name, $value); $this->storedData[$name] = $value; return $this; } public function apply(node $node, string $blockName): void { $style = array(); $class = array(); foreach ($this->storedData as $name => $value) { if(is_bool($value)) $class[$name] = $value; else $style[$name] = $value; } if($style) $node->setProp('style', $style); if($class) $node->setProp('class', $class); } public function var(string|array $nameOrMap, mixed $value = null): style { if(is_array($nameOrMap)) { foreach($nameOrMap as $name => $val) $this->setVal('--' . $name, $val); return $this; } return $this->setVal('--' . $nameOrMap, $value); } /** * Magic static method for style property value. * * @access public * @param string $name - Property name. * @param array $args - Property values. * @return style */ public static function __callStatic($name, $args): style { $style = new style(); $style->$name(...$args); return $style; } public static function formatStyleName(string $name): string { return strtolower(preg_replace('/(?