helper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. use think\Collection;
  12. use think\helper\Arr;
  13. if (!function_exists('throw_if')) {
  14. /**
  15. * 按条件抛异常
  16. *
  17. * @template TValue
  18. * @template TException of \Throwable
  19. *
  20. * @param TValue $condition
  21. * @param TException|class-string<TException>|string $exception
  22. * @param mixed ...$parameters
  23. * @return TValue
  24. *
  25. * @throws TException
  26. */
  27. function throw_if($condition, $exception, ...$parameters)
  28. {
  29. if ($condition) {
  30. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  31. }
  32. return $condition;
  33. }
  34. }
  35. if (!function_exists('throw_unless')) {
  36. /**
  37. * 按条件抛异常
  38. *
  39. * @template TValue
  40. * @template TException of \Throwable
  41. *
  42. * @param TValue $condition
  43. * @param TException|class-string<TException>|string $exception
  44. * @param mixed ...$parameters
  45. * @return TValue
  46. *
  47. * @throws TException
  48. */
  49. function throw_unless($condition, $exception, ...$parameters)
  50. {
  51. if (!$condition) {
  52. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  53. }
  54. return $condition;
  55. }
  56. }
  57. if (!function_exists('tap')) {
  58. /**
  59. * 对一个值调用给定的闭包,然后返回该值
  60. *
  61. * @template TValue
  62. *
  63. * @param TValue $value
  64. * @param (callable(TValue): mixed)|null $callback
  65. * @return TValue
  66. */
  67. function tap($value, $callback = null)
  68. {
  69. if (is_null($callback)) {
  70. return $value;
  71. }
  72. $callback($value);
  73. return $value;
  74. }
  75. }
  76. if (!function_exists('value')) {
  77. /**
  78. * Return the default value of the given value.
  79. *
  80. * @template TValue
  81. *
  82. * @param TValue|\Closure(): TValue $value
  83. * @return TValue
  84. */
  85. function value($value)
  86. {
  87. return $value instanceof Closure ? $value() : $value;
  88. }
  89. }
  90. if (!function_exists('collect')) {
  91. /**
  92. * Create a collection from the given value.
  93. *
  94. * @param mixed $value
  95. * @return Collection
  96. */
  97. function collect($value = null)
  98. {
  99. return new Collection($value);
  100. }
  101. }
  102. if (!function_exists('data_fill')) {
  103. /**
  104. * Fill in data where it's missing.
  105. *
  106. * @param mixed $target
  107. * @param string|array $key
  108. * @param mixed $value
  109. * @return mixed
  110. */
  111. function data_fill(&$target, $key, $value)
  112. {
  113. return data_set($target, $key, $value, false);
  114. }
  115. }
  116. if (!function_exists('data_get')) {
  117. /**
  118. * Get an item from an array or object using "dot" notation.
  119. *
  120. * @param mixed $target
  121. * @param string|array|int $key
  122. * @param mixed $default
  123. * @return mixed
  124. */
  125. function data_get($target, $key, $default = null)
  126. {
  127. if (is_null($key)) {
  128. return $target;
  129. }
  130. $key = is_array($key) ? $key : explode('.', $key);
  131. while (!is_null($segment = array_shift($key))) {
  132. if ('*' === $segment) {
  133. if ($target instanceof Collection) {
  134. $target = $target->all();
  135. } elseif (!is_array($target)) {
  136. return value($default);
  137. }
  138. $result = [];
  139. foreach ($target as $item) {
  140. $result[] = data_get($item, $key);
  141. }
  142. return in_array('*', $key) ? Arr::collapse($result) : $result;
  143. }
  144. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  145. $target = $target[$segment];
  146. } elseif (is_object($target) && isset($target->{$segment})) {
  147. $target = $target->{$segment};
  148. } else {
  149. return value($default);
  150. }
  151. }
  152. return $target;
  153. }
  154. }
  155. if (!function_exists('data_set')) {
  156. /**
  157. * Set an item on an array or object using dot notation.
  158. *
  159. * @param mixed $target
  160. * @param string|array $key
  161. * @param mixed $value
  162. * @param bool $overwrite
  163. * @return mixed
  164. */
  165. function data_set(&$target, $key, $value, $overwrite = true)
  166. {
  167. $segments = is_array($key) ? $key : explode('.', $key);
  168. if (($segment = array_shift($segments)) === '*') {
  169. if (!Arr::accessible($target)) {
  170. $target = [];
  171. }
  172. if ($segments) {
  173. foreach ($target as &$inner) {
  174. data_set($inner, $segments, $value, $overwrite);
  175. }
  176. } elseif ($overwrite) {
  177. foreach ($target as &$inner) {
  178. $inner = $value;
  179. }
  180. }
  181. } elseif (Arr::accessible($target)) {
  182. if ($segments) {
  183. if (!Arr::exists($target, $segment)) {
  184. $target[$segment] = [];
  185. }
  186. data_set($target[$segment], $segments, $value, $overwrite);
  187. } elseif ($overwrite || !Arr::exists($target, $segment)) {
  188. $target[$segment] = $value;
  189. }
  190. } elseif (is_object($target)) {
  191. if ($segments) {
  192. if (!isset($target->{$segment})) {
  193. $target->{$segment} = [];
  194. }
  195. data_set($target->{$segment}, $segments, $value, $overwrite);
  196. } elseif ($overwrite || !isset($target->{$segment})) {
  197. $target->{$segment} = $value;
  198. }
  199. } else {
  200. $target = [];
  201. if ($segments) {
  202. data_set($target[$segment], $segments, $value, $overwrite);
  203. } elseif ($overwrite) {
  204. $target[$segment] = $value;
  205. }
  206. }
  207. return $target;
  208. }
  209. }
  210. if (!function_exists('trait_uses_recursive')) {
  211. /**
  212. * 获取一个trait里所有引用到的trait
  213. *
  214. * @param string $trait Trait
  215. * @return array
  216. */
  217. function trait_uses_recursive(string $trait): array
  218. {
  219. $traits = class_uses($trait);
  220. foreach ($traits as $trait) {
  221. $traits += trait_uses_recursive($trait);
  222. }
  223. return $traits;
  224. }
  225. }
  226. if (!function_exists('class_basename')) {
  227. /**
  228. * 获取类名(不包含命名空间)
  229. *
  230. * @param mixed $class 类名
  231. * @return string
  232. */
  233. function class_basename($class): string
  234. {
  235. $class = is_object($class) ? get_class($class) : $class;
  236. return basename(str_replace('\\', '/', $class));
  237. }
  238. }
  239. if (!function_exists('class_uses_recursive')) {
  240. /**
  241. *获取一个类里所有用到的trait,包括父类的
  242. *
  243. * @param mixed $class 类名
  244. * @return array
  245. */
  246. function class_uses_recursive($class): array
  247. {
  248. if (is_object($class)) {
  249. $class = get_class($class);
  250. }
  251. $results = [];
  252. $classes = array_merge([$class => $class], class_parents($class));
  253. foreach ($classes as $class) {
  254. $results += trait_uses_recursive($class);
  255. }
  256. return array_unique($results);
  257. }
  258. }
  259. if (!function_exists('array_is_list')) {
  260. /**
  261. * 判断数组是否为list
  262. *
  263. * @param array $array 数据
  264. * @return bool
  265. */
  266. function array_is_list(array $array): bool
  267. {
  268. return array_values($array) === $array;
  269. }
  270. }
  271. if (!function_exists('json_validate')) {
  272. /**
  273. * 判断是否为有效json数据
  274. *
  275. * @param string $string 数据
  276. * @return bool
  277. */
  278. function json_validate(string $string): bool
  279. {
  280. json_decode($string);
  281. return json_last_error() === JSON_ERROR_NONE;
  282. }
  283. }