123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- use Illuminate\Support\Arr;
- if (!function_exists('make_tree')) {
- /**
- * @param array $list
- * @param int $parentId
- * @return array
- */
- function make_tree(array $list, $parentId = 0)
- {
- $tree = [];
- if (empty($list)) {
- return $tree;
- }
- $newList = [];
- foreach ($list as $k => $v) {
- $newList[$v['id']] = $v;
- }
- foreach ($newList as $value) {
- if ($parentId == $value['parent_id']) {
- $tree[] = &$newList[$value['id']];
- } elseif (isset($newList[$value['parent_id']])) {
- $newList[$value['parent_id']]['children'][] = &$newList[$value['id']];
- }
- }
- return $tree;
- }
- }
- if (!function_exists('text_diff')) {
- function text_diff(string $text1, string $text2): string
- {
- $text1 = str_replace(' ', '', trim($text1));
- $text2 = str_replace(' ', '', trim($text2));
- $w = explode("\n", $text1);
- $o = explode("\n", $text2);
- $w1 = array_diff_assoc($w, $o);
- $o1 = array_diff_assoc($o, $w);
- $w2 = array();
- $o2 = array();
- foreach ($w1 as $idx => $val)
- $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . "<del>" . trim($val) . "</del>";
- foreach ($o1 as $idx => $val)
- $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . "<ins>" . trim($val) . "</ins>";
- $diff = array_merge($w2, $o2);
- ksort($diff);
- return implode("\n", $diff);
- }
- }
- if (!function_exists('throw_validation_if')) {
- function throw_validation_if($condition, ...$parameters)
- {
- return throw_if($condition, \App\Exceptions\ValidationException::class, ...$parameters);
- }
- }
- if (!function_exists('make_array_list')) {
- function make_array_list(string $value): array
- {
- $array = [];
- if ($value == '') {
- return $array;
- }
- $array = explode(',', $value);
- $array = array_filter($array, function ($value) {
- return $value !== '';
- });
- $array = array_map(function ($value) {
- return intval($value);
- }, $array);
- return ($array);
- }
- }
- if (!function_exists('json_decode_arr')) {
- /**
- * 让json decode统一返回array
- * @param mixed $string
- * @return array
- */
- function json_decode_arr($string): array
- {
- $arr = json_decode($string, true);
- if (is_null($arr)) {
- $arr = [];
- }
- return $arr;
- }
- }
- if (!function_exists('cos_upload_prefix')) {
- /**
- * 返回cos上传路径前缀
- * @param int $companyId
- * @param string $objectType
- * @param mixed $extra
- * @return string
- */
- function cos_upload_prefix(int $companyId = 0, string $objectType = '', string $extra = ''): string
- {
- if (empty($extra)) {
- $extra = date("Ymd/Hi");
- }
- return sprintf("c%s/%s/%s", $companyId, $objectType, $extra);
- }
- }
- if (!function_exists('date_time_str')) {
- /**
- * 生成日期时间格式
- * @param int $companyId
- * @param string $objectType
- * @param mixed $extra
- * @return string
- */
- function date_time_str(int $timestamp = 0): string
- {
- if ($timestamp == 0) {
- $timestamp = time();
- }
- return date('Y-m-d H:i:s', $timestamp);
- }
- }
- //对树进行id重置封装生成新的id
- if (!function_exists('make_tree_display_id')) {
- function make_tree_display_id(&$tree, $parentId = 0, $index = 1, $isParent = true)
- {
- foreach ($tree as &$node) {
- // 设置当前节点的ID
- $node['display_id'] = ($parentId == 0 || $isParent == true) ? (string) $index : $parentId . '.' . $index;
- // 更新索引值
- $index++;
- // 如果当前节点有子节点,递归调用本函数,并传递新的父ID和索引值
- if (isset($node['children'])) {
- make_tree_display_id($node['children'], $node['display_id'], 1, false);
- }
- }
- }
- }
- //对普通列表装生成新的id
- if (!function_exists('make_display_id')) {
- function make_display_id($datas, $pageSize)
- {
- $page = $datas->currentPage();
- $newId = ($page - 1) * $pageSize + 1;
- foreach ($datas as $data) {
- $data->display_id = $newId++;
- }
- }
- }
- if (!function_exists('array_are_equal')) {
- function array_are_equal(array $array1, array $array2): bool
- {
- if (array_keys($array1) !== array_keys($array2)) {
- return false;
- }
- foreach ($array1 as $key => $value) {
- // 如果当前元素是数组,则递归比较
- if (is_array($value)) {
- if (!array_are_equal($value, $array2[$key])) {
- return false;
- }
- } else {
- if ($value != $array2[$key]) {
- return false;
- }
- }
- }
- return true;
- }
- }
|