helpers.php 3.9 KB

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