helpers.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. use Illuminate\Support\Arr;
  3. if (!function_exists('make_tree')) {
  4. /**
  5. * @param array $list
  6. * @param int $parentId
  7. * @return array
  8. */
  9. function make_tree(array $list, $parentId = 0)
  10. {
  11. $tree = [];
  12. if (empty($list)) {
  13. return $tree;
  14. }
  15. $newList = [];
  16. foreach ($list as $k => $v) {
  17. $newList[$v['id']] = $v;
  18. }
  19. foreach ($newList as $value) {
  20. if ($parentId == $value['parent_id']) {
  21. $tree[] = &$newList[$value['id']];
  22. } elseif (isset($newList[$value['parent_id']])) {
  23. $newList[$value['parent_id']]['children'][] = &$newList[$value['id']];
  24. }
  25. }
  26. return $tree;
  27. }
  28. }
  29. if (!function_exists('text_diff')) {
  30. function text_diff(string $text1, string $text2): string
  31. {
  32. $text1 = str_replace('&nbsp;', '', trim($text1));
  33. $text2 = str_replace('&nbsp;', '', trim($text2));
  34. $w = explode("\n", $text1);
  35. $o = explode("\n", $text2);
  36. $w1 = array_diff_assoc($w, $o);
  37. $o1 = array_diff_assoc($o, $w);
  38. $w2 = array();
  39. $o2 = array();
  40. foreach ($w1 as $idx => $val)
  41. $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . "<del>" . trim($val) . "</del>";
  42. foreach ($o1 as $idx => $val)
  43. $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . "<ins>" . trim($val) . "</ins>";
  44. $diff = array_merge($w2, $o2);
  45. ksort($diff);
  46. return implode("\n", $diff);
  47. }
  48. }
  49. if (!function_exists('throw_validation_if')) {
  50. function throw_validation_if($condition, ...$parameters)
  51. {
  52. return throw_if($condition, \App\Exceptions\ValidationException::class, ...$parameters);
  53. }
  54. }
  55. if (!function_exists('make_array_list')) {
  56. function make_array_list(string $value): array
  57. {
  58. $array = [];
  59. if ($value == '') {
  60. return $array;
  61. }
  62. $array = explode(',', $value);
  63. $array = array_filter($array, function ($value) {
  64. return $value !== '';
  65. });
  66. $array = array_map(function ($value) {
  67. return intval($value);
  68. }, $array);
  69. return ($array);
  70. }
  71. }
  72. if (!function_exists('json_decode_arr')) {
  73. /**
  74. * 让json decode统一返回array
  75. * @param mixed $string
  76. * @return array
  77. */
  78. function json_decode_arr($string): array
  79. {
  80. $arr = json_decode($string, true);
  81. if (is_null($arr)) {
  82. $arr = [];
  83. }
  84. return $arr;
  85. }
  86. }
  87. if (!function_exists('cos_upload_prefix')) {
  88. /**
  89. * 返回cos上传路径前缀
  90. * @param int $companyId
  91. * @param string $objectType
  92. * @param mixed $extra
  93. * @return string
  94. */
  95. function cos_upload_prefix(int $companyId = 0, string $objectType = '', string $extra = ''): string
  96. {
  97. if (empty($extra)) {
  98. $extra = date("Ymd/Hi");
  99. }
  100. return sprintf("c%s/%s/%s", $companyId, $objectType, $extra);
  101. }
  102. }
  103. if (!function_exists('date_time_str')) {
  104. /**
  105. * 生成日期时间格式
  106. * @param int $companyId
  107. * @param string $objectType
  108. * @param mixed $extra
  109. * @return string
  110. */
  111. function date_time_str(int $timestamp = 0): string
  112. {
  113. if ($timestamp == 0) {
  114. $timestamp = time();
  115. }
  116. return date('Y-m-d H:i:s', $timestamp);
  117. }
  118. }
  119. //对树进行id重置封装生成新的id
  120. if (!function_exists('make_tree_display_id')) {
  121. function make_tree_display_id(&$tree, $parentId = 0, $index = 1, $isParent = true)
  122. {
  123. foreach ($tree as &$node) {
  124. // 设置当前节点的ID
  125. $node['display_id'] = ($parentId == 0 || $isParent == true) ? (string) $index : $parentId . '.' . $index;
  126. // 更新索引值
  127. $index++;
  128. // 如果当前节点有子节点,递归调用本函数,并传递新的父ID和索引值
  129. if (isset($node['children'])) {
  130. make_tree_display_id($node['children'], $node['display_id'], 1, false);
  131. }
  132. }
  133. }
  134. }
  135. //对普通列表装生成新的id
  136. if (!function_exists('make_display_id')) {
  137. function make_display_id($datas, $pageSize)
  138. {
  139. $page = $datas->currentPage();
  140. $newId = ($page - 1) * $pageSize + 1;
  141. foreach ($datas as $data) {
  142. $data->display_id = $newId++;
  143. }
  144. }
  145. }
  146. if (!function_exists('array_are_equal')) {
  147. function array_are_equal(array $array1, array $array2): bool
  148. {
  149. if (array_keys($array1) !== array_keys($array2)) {
  150. return false;
  151. }
  152. foreach ($array1 as $key => $value) {
  153. // 如果当前元素是数组,则递归比较
  154. if (is_array($value)) {
  155. if (!array_are_equal($value, $array2[$key])) {
  156. return false;
  157. }
  158. } else {
  159. if ($value != $array2[$key]) {
  160. return false;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. }