common.php 876 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. // 应用公共文件
  3. /**
  4. * @param bool $bool
  5. * @param $message
  6. * @param int $code
  7. * @throws \app\exception\BaseException
  8. */
  9. function predicate(bool $bool, $message, $code = 1012) {
  10. if (!$bool) {
  11. throw new \app\exception\BaseException($message, $code);
  12. }
  13. }
  14. /**
  15. * 格式化字符串
  16. * @param $value
  17. * @param null $def
  18. * @return null
  19. */
  20. function format_string($value, $def = null) {
  21. return $value != null && strlen(strval($value)) > 0 ? $value : $def;
  22. }
  23. /**
  24. * @param $array
  25. * @param $id
  26. * @return array 树状Menu
  27. */
  28. function recursion($array, $id = 0)
  29. {
  30. $resArray = [];
  31. foreach ($array as $key => $value)
  32. {
  33. if ($value['pid'] == $id)
  34. {
  35. $resArray[$key] = $array[$key];
  36. $resArray[$key]['children'] = recursion($array, $value['id']);
  37. }
  38. }
  39. return array_merge($resArray);
  40. }