setting.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * The setting 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 dirname(__DIR__) . DS . 'utils' . DS . 'dataset.class.php';
  13. require_once dirname(__DIR__) . DS . 'utils' . DS . 'classlist.class.php';
  14. class setting extends \zin\utils\dataset
  15. {
  16. /**
  17. * Set property values.
  18. *
  19. * @access public
  20. * @param string $name - Property name.
  21. * @param mixed $args - Property values.
  22. * @return setting
  23. */
  24. public function setValues($name, ...$args)
  25. {
  26. if(empty($args))
  27. {
  28. $value = true;
  29. }
  30. elseif(($name === 'url' || $name === 'href' || $name === 'link') && count($args) > 1)
  31. {
  32. /* Support to set url with createLink params. */
  33. $value = call_user_func_array('\helper::createLink', $args);
  34. }
  35. else if(count($args) > 1)
  36. {
  37. $value = array();
  38. foreach($args as $key => $val)
  39. {
  40. $value[$key] = ($val instanceof setting) ? $val->toArray() : $val;
  41. }
  42. }
  43. else
  44. {
  45. $value = array_shift($args);
  46. }
  47. return $this->setVal($name, $value);
  48. }
  49. /**
  50. * Set property value as class list.
  51. *
  52. * @access public
  53. * @param string $name - Property name.
  54. * @param mixed $class - Class list.
  55. * @return setting
  56. */
  57. public function setClass($name, ...$class)
  58. {
  59. if(empty($class)) return $this;
  60. if(isset($class[0]) && $class[0] === true)
  61. {
  62. return $this->setVal($name, \zin\utils\classlist::format($class));
  63. }
  64. return $this->setVal($name, \zin\utils\classlist::format($this->getVal($name), $class));
  65. }
  66. /**
  67. * Method for sub class to hook on setting it.
  68. *
  69. * @access protected
  70. * @param string $prop Property name or properties list.
  71. * @param mixed $value Property value.
  72. * @return setting
  73. */
  74. protected function setVal($prop, $value)
  75. {
  76. if($value instanceof setting) $value = $value->toArray();
  77. $this->storedData[$prop] = $value;
  78. return $this;
  79. }
  80. /**
  81. * Convert to directive.
  82. *
  83. * @access protected
  84. * @param string $type - Directive type.
  85. * @return directive
  86. */
  87. public function toDirective($type = 'prop')
  88. {
  89. return new directive($type, $this->storedData);
  90. }
  91. /**
  92. * Magic method for setting property value.
  93. *
  94. * @access public
  95. * @param string $name - Property name.
  96. * @param array $args - Property values.
  97. * @return setting
  98. */
  99. public function __call($name, $args)
  100. {
  101. return $this->setValues($name, ...$args);
  102. }
  103. /**
  104. * Magic static method for setting property value.
  105. *
  106. * @access public
  107. * @param string $name - Property name.
  108. * @param array $args - Property values.
  109. * @return setting
  110. */
  111. public static function __callStatic($name, $args)
  112. {
  113. /* Compatible with zui prop className. */
  114. if($name === '_className') $name = 'className';
  115. $set = new setting();
  116. return $set->setValues($name, ...$args);
  117. }
  118. }
  119. /**
  120. * Create a setting instance.
  121. *
  122. * @param mixed $setting - Setting data or setting property name.
  123. * @param mixed $value - Setting value.
  124. * @return setting
  125. */
  126. function setting($setting = null, $value = null)
  127. {
  128. return new setting($setting, $value);
  129. }
  130. /**
  131. * Convert setting to array.
  132. *
  133. * @param array|setting $setting - Setting data or setting instance.
  134. * @return array
  135. */
  136. function toArray($setting)
  137. {
  138. return $setting instanceof setting ? $setting->toArray() : $setting;
  139. }
  140. /**
  141. * Convert setting list to array.
  142. *
  143. * @param array $array - Array data.
  144. * @return setting
  145. */
  146. function toArrayList($array)
  147. {
  148. $list = array();
  149. foreach($array as $key => $val)
  150. {
  151. $list[$key] = $val instanceof setting ? $val->toArray() : $val;
  152. }
  153. return $list;
  154. }