jscallback.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * The js callback 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 . 'zin.func.php';
  13. require_once __DIR__ . DS . 'jshelper.class.php';
  14. /**
  15. * JS callback class.
  16. */
  17. class jsCallback extends jsHelper
  18. {
  19. /**
  20. * The function name.
  21. * 函数名称。
  22. *
  23. * @access public
  24. * @var string|null
  25. */
  26. public $funcName;
  27. /**
  28. * The function arguments.
  29. * 函数参数列表。
  30. *
  31. * @access public
  32. * @var array
  33. */
  34. public $funcArgs = array();
  35. /**
  36. * Is arrow function.
  37. * 是否为箭头函数。
  38. *
  39. * @access public
  40. * @var string
  41. */
  42. public $isArrowFunc = false;
  43. /**
  44. * Parent node
  45. *
  46. * @access public
  47. * @var node
  48. */
  49. public $parent = null;
  50. /**
  51. * 构造函数。
  52. *
  53. * @access public
  54. * @param string ...$args The function arguments. 函数参数列表。
  55. */
  56. public function __construct(...$args)
  57. {
  58. parent::__construct();
  59. $this->funcArgs = $args;
  60. }
  61. /**
  62. * Set the function name.
  63. * 设置函数名称。
  64. *
  65. * @access public
  66. * @param string|null $name The function name. 函数名称。
  67. * @return self
  68. */
  69. public function name($name)
  70. {
  71. $this->funcName = $name;
  72. return $this;
  73. }
  74. /**
  75. * Set the function arguments.
  76. * 设置函数参数名称列表。
  77. *
  78. * @access public
  79. * @param string ...$args The function arguments. 函数参数名称列表。
  80. * @return self
  81. */
  82. public function args(...$args)
  83. {
  84. $this->funcArgs = $args;
  85. return $this;
  86. }
  87. /**
  88. * Set whether to use arrow functions.
  89. * 设置是否使用箭头函数。
  90. *
  91. * @access public
  92. * @param bool $arrow Is arrow function. 是否使用箭头函数。
  93. * @return self
  94. */
  95. public function arrow($arrow = true)
  96. {
  97. $this->isArrowFunc = $arrow;
  98. return $this;
  99. }
  100. /**
  101. * Convert to JS code.
  102. * 转换为 JS 代码。
  103. *
  104. * @access public
  105. * @param string $joiner The joiner. 连接符。
  106. * @return string
  107. */
  108. public function toJS($joiner = "\n")
  109. {
  110. $name = $this->funcName;
  111. $args = $this->funcArgs;
  112. $codes = array();
  113. $argsPart = implode(',', $args);
  114. if($this->isArrowFunc)
  115. {
  116. $codes[] = "($argsPart)=>{";
  117. }
  118. else
  119. {
  120. $codes[] = "function" . (empty($name) ? '' : " $name") . "($argsPart){";
  121. }
  122. $codes[] = $this->buildBody($joiner);
  123. $codes[] = '}';
  124. return implode($joiner, $codes);
  125. }
  126. /**
  127. * Build the body.
  128. * 构建函数体。
  129. *
  130. * @access function
  131. * @param string $joiner The joiner. 连接符。
  132. * @return string
  133. */
  134. public function buildBody($joiner = "\n")
  135. {
  136. return parent::toJS($joiner);
  137. }
  138. /**
  139. * Add return statement.
  140. * 添加 return 语句。
  141. *
  142. * @access public
  143. * @param mixed $data The return data. 返回数据。
  144. * @return self
  145. */
  146. public function returnData($data = null)
  147. {
  148. return $this->appendLine('return', static::json($data));
  149. }
  150. /**
  151. * Convert to variable.
  152. * 转换为变量。
  153. *
  154. * @access public
  155. * @param string|null $name The variable name. 变量名称。
  156. * @param bool $const Is const variable. 是否为常量。
  157. * @return string
  158. * @throws \ErrorException
  159. */
  160. public function toVar($name = null, $const = false)
  161. {
  162. if(is_null($name)) $name = $this->funcName;
  163. if(is_null($name))
  164. {
  165. trigger_error('The function name is required when converting to variable.', E_USER_ERROR);
  166. }
  167. $codes = array();
  168. $codes[] = $const ? 'const' : 'var';
  169. $codes[] = $name;
  170. $codes[] = '=';
  171. $codes[] = $this->toJS();
  172. $codes[] = ';';
  173. return implode(' ', $codes);
  174. }
  175. /**
  176. * Convert to const variable.
  177. * 转换为常量变量。
  178. *
  179. * @access public
  180. * @param string|null $name The variable name. 变量名称。
  181. * @return string
  182. */
  183. public function toConst($name = null)
  184. {
  185. return $this->toVar($name, true);
  186. }
  187. /**
  188. * Define callback with magic method.
  189. * 通过魔术方法定义回调函数。
  190. *
  191. * @access public
  192. * @param string $name The function name. 函数名称。
  193. * @param array $args The function arguments. 函数参数列表。
  194. * @return self
  195. * @static
  196. */
  197. public static function __callStatic($name, $args)
  198. {
  199. $callback = new jsCallback(...$args);
  200. return $callback->name($name);
  201. }
  202. }
  203. /**
  204. * Create a js callback.
  205. *
  206. * @param string ...$args The function arguments. 函数参数列表。
  207. * @return jsCallback
  208. */
  209. function jsCallback(...$args)
  210. {
  211. return new jsCallback(...$args);
  212. }