set.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * The properties setter class file of zin lib.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin;
  12. require_once __DIR__ . DS . 'setting.class.php';
  13. require_once __DIR__ . DS . 'directive.class.php';
  14. require_once __DIR__ . DS . 'context.func.php';
  15. class set extends setting implements iDirective
  16. {
  17. /**
  18. * @var bool
  19. */
  20. public $notRenderInGlobal;
  21. /**
  22. * Create an instance, the initialed data can be passed.
  23. *
  24. * @access public
  25. * @param mixed $data Properties list array.
  26. * @param mixed $value Property value.
  27. */
  28. public function __construct($data = null, $value = null)
  29. {
  30. parent::__construct($data, $value);
  31. renderInGlobal($this);
  32. }
  33. /**
  34. * @param \zin\node $node
  35. * @param string $blockName
  36. */
  37. public function apply($node, $blockName)
  38. {
  39. $node->setProp($this->toArray());
  40. }
  41. public static function __callStatic($prop, $args)
  42. {
  43. $set = new set();
  44. if(empty($args)) return $set->set($prop, true);
  45. if($prop === 'class' || strtolower($prop) === 'classname')
  46. {
  47. global $config;
  48. if($prop === 'class' && isset($config->debug) && $config->debug)
  49. {
  50. trigger_error("[ZIN] Use set::className() instead of set::class() to compatible with php 5.4.", E_USER_WARNING);
  51. }
  52. return $set->setClass('class', $args);
  53. }
  54. /* Compatible with zui prop className. */
  55. if($prop === '_className')
  56. {
  57. return $set->setClass('className', $args);
  58. }
  59. /* Support to set url with createLink params. */
  60. if(($prop === 'url' || $prop === 'href' || $prop === 'link') && count($args) > 1)
  61. {
  62. $value = call_user_func_array('\helper::createLink', $args);
  63. }
  64. else
  65. {
  66. $value = count($args) > 1 ? $args : array_shift($args);
  67. }
  68. return $set->set($prop, $value);
  69. }
  70. }
  71. /**
  72. * Set widget properties.
  73. *
  74. * @param string|array|props|null $name
  75. * @param mixed $value
  76. * @return set
  77. */
  78. function set($name = null, $value = null)
  79. {
  80. $set = new set();
  81. if($name === null) return $set;
  82. $props = null;
  83. if($name instanceof props) $props = $name->toArray();
  84. else if(is_array($name)) $props = $name;
  85. else if(is_object($name)) $props = (array)$name;
  86. else if(is_string($name)) $props = array($name => $value);
  87. $set->set($props);
  88. return $set;
  89. }
  90. /**
  91. * Set widget CSS class attribute.
  92. *
  93. * @param mixed ...$class
  94. * @return set
  95. */
  96. function setClass(...$class)
  97. {
  98. return set()->setClass('class', ...$class);
  99. }
  100. /**
  101. * Set widget CSS variable.
  102. *
  103. * @return set
  104. * @param mixed[]|string $name
  105. */
  106. function setCssVar($name, $value = null)
  107. {
  108. return set()->addToMap('--', is_array($name) ? $name : array($name => $value));
  109. }
  110. /**
  111. * Set widget ID attribute.
  112. *
  113. * @return ?set
  114. */
  115. function setID($id = null)
  116. {
  117. return set('id', $id);
  118. }
  119. /**
  120. * Set widget element tag name.
  121. *
  122. * @return set
  123. */
  124. function setTag($id)
  125. {
  126. return set('tagName', $id);
  127. }
  128. /**
  129. * Set widget zui-key attribute.
  130. *
  131. * @param null|int|string $key
  132. * @return set
  133. */
  134. function setKey($key)
  135. {
  136. return set('zui-key', $key);
  137. }
  138. /**
  139. * Set widget data-* attribute.
  140. *
  141. * @param string|array $name
  142. * @param mixed $value
  143. * @return set
  144. */
  145. function setData($name, $value = null)
  146. {
  147. if($name === null) return set();
  148. if(is_object($name)) $name = (array)$name;
  149. $map = is_array($name) ? $name : array($name => $value);
  150. $attrs = array();
  151. foreach($map as $key => $value)
  152. {
  153. if(is_numeric($key)) $key = (string)$key;
  154. $name = 'data-' . strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $key));
  155. if(is_bool($value)) $attrs[$name] = $value ? 'true' : 'false';
  156. else if(is_array($value)) $attrs[$name] = json_encode($value);
  157. else $attrs[$name] = $value;
  158. }
  159. return set($attrs);
  160. }