Helper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\utils;
  8. /**
  9. * 帮助类
  10. */
  11. class Helper
  12. {
  13. /**
  14. * 数据树形化
  15. * @param array $data 数据
  16. * @param string $childrenname 子数据名
  17. * @param string $keyName 数据key名
  18. * @param string $pidName 数据上级key名
  19. * @return array
  20. */
  21. public static function makeTree(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'parent_id')
  22. {
  23. $list = [];
  24. foreach ($data as $value) {
  25. $list[$value[$keyName]] = $value;
  26. }
  27. $tree = []; //格式化好的树
  28. foreach ($list as $item) {
  29. if (isset($list[$item[$pidName]])) {
  30. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  31. } else {
  32. $tree[] = &$list[$item[$keyName]];
  33. }
  34. }
  35. return $tree;
  36. }
  37. /**
  38. * 生成Arco菜单
  39. * @param array $data 数据
  40. * @param string $childrenname 子数据名
  41. * @param string $keyName 数据key名
  42. * @param string $pidName 数据上级key名
  43. * @return array
  44. */
  45. public static function makeArcoMenus(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'parent_id')
  46. {
  47. $list = [];
  48. foreach ($data as $value) {
  49. if ($value['type'] === 'M'){
  50. $path = '/'.$value['route'];
  51. $layout = isset($value['is_layout']) ? $value['is_layout'] : 1;
  52. $temp = [
  53. $keyName => $value[$keyName],
  54. $pidName => $value[$pidName],
  55. 'name' => $value['route'],
  56. 'path' => $path,
  57. 'component' => $value['component'],
  58. 'redirect' => $value['redirect'],
  59. 'meta' => [
  60. 'title' => $value['name'],
  61. 'type' => $value['type'],
  62. 'hidden' => $value['is_hidden'] === 1,
  63. 'layout' => $layout === 1,
  64. 'hiddenBreadcrumb' => false,
  65. 'icon' => $value['icon'],
  66. ],
  67. ];
  68. $list[$value[$keyName]] = $temp;
  69. }
  70. if ($value['type'] === 'I' || $value['type'] === 'L'){
  71. $temp = [
  72. $keyName => $value[$keyName],
  73. $pidName => $value[$pidName],
  74. 'name' => $value['code'],
  75. 'path' => $value['route'],
  76. 'meta' => [
  77. 'title' => $value['name'],
  78. 'type' => $value['type'],
  79. 'hidden' => $value['is_hidden'] === 1,
  80. 'hiddenBreadcrumb' => false,
  81. 'icon' => $value['icon'],
  82. ],
  83. ];
  84. $list[$value[$keyName]] = $temp;
  85. }
  86. }
  87. $tree = []; //格式化好的树
  88. foreach ($list as $item) {
  89. if (isset($list[$item[$pidName]])) {
  90. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  91. } else {
  92. $tree[] = &$list[$item[$keyName]];
  93. }
  94. }
  95. return $tree;
  96. }
  97. /**
  98. * 下划线转驼峰
  99. */
  100. public static function camelize($uncamelized_words,$separator='_')
  101. {
  102. $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
  103. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
  104. }
  105. /**
  106. * 驼峰命名转下划线命名
  107. */
  108. public static function uncamelize($camelCaps,$separator='_')
  109. {
  110. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  111. }
  112. /**
  113. * 转换为驼峰
  114. * @param string $value
  115. * @return string
  116. */
  117. public static function camel(string $value): string
  118. {
  119. static $cache = [];
  120. $key = $value;
  121. if (isset($cache[$key])) {
  122. return $cache[$key];
  123. }
  124. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  125. return $cache[$key] = str_replace(' ', '', $value);
  126. }
  127. /**
  128. * 获取业务名称
  129. * @param string $tableName
  130. * @return mixed
  131. */
  132. public static function get_business(string $tableName)
  133. {
  134. $start = strrpos($tableName,'_');
  135. if ($start !== false) {
  136. $result = substr($tableName, $start + 1);
  137. } else {
  138. $result = $tableName;
  139. }
  140. return static::camelize($result);
  141. }
  142. /**
  143. * 获取业务名称
  144. * @param string $tableName
  145. * @return mixed
  146. */
  147. public static function get_big_business(string $tableName)
  148. {
  149. $start = strrpos($tableName,'_');
  150. $result = substr($tableName, $start + 1);
  151. return static::camel($result);
  152. }
  153. /**
  154. * 只替换一次字符串
  155. * @param $needle
  156. * @param $replace
  157. * @param $haystack
  158. * @return array|mixed|string|string[]
  159. */
  160. public static function str_replace_once($needle, $replace, $haystack)
  161. {
  162. $pos = strpos($haystack, $needle);
  163. if ($pos === false) {
  164. return $haystack;
  165. }
  166. return substr_replace($haystack, $replace, $pos, strlen($needle));
  167. }
  168. /**
  169. * 遍历目录
  170. * @param $template_name
  171. * @return array
  172. */
  173. public static function get_dir($template_name)
  174. {
  175. $dir = base_path($template_name);
  176. $fileDir = [];
  177. if (is_dir($dir)){
  178. if ($dh = opendir($dir)){
  179. while (($file = readdir($dh)) !== false){
  180. if($file != "." && $file != ".."){
  181. array_push($fileDir, $file);
  182. }
  183. }
  184. closedir($dh);
  185. }
  186. }
  187. return $fileDir;
  188. }
  189. }