helper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  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. * @param mixed $condition
  18. * @param Throwable|string $exception
  19. * @param array ...$parameters
  20. * @return mixed
  21. *
  22. * @throws Throwable
  23. */
  24. function throw_if($condition, $exception, ...$parameters)
  25. {
  26. if ($condition) {
  27. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  28. }
  29. return $condition;
  30. }
  31. }
  32. if (!function_exists('throw_unless')) {
  33. /**
  34. * 按条件抛异常
  35. *
  36. * @param mixed $condition
  37. * @param Throwable|string $exception
  38. * @param array ...$parameters
  39. * @return mixed
  40. * @throws Throwable
  41. */
  42. function throw_unless($condition, $exception, ...$parameters)
  43. {
  44. if (!$condition) {
  45. throw (is_string($exception) ? new $exception(...$parameters) : $exception);
  46. }
  47. return $condition;
  48. }
  49. }
  50. if (!function_exists('tap')) {
  51. /**
  52. * 对一个值调用给定的闭包,然后返回该值
  53. *
  54. * @param mixed $value
  55. * @param callable|null $callback
  56. * @return mixed
  57. */
  58. function tap($value, $callback = null)
  59. {
  60. if (is_null($callback)) {
  61. return $value;
  62. }
  63. $callback($value);
  64. return $value;
  65. }
  66. }
  67. if (!function_exists('value')) {
  68. /**
  69. * Return the default value of the given value.
  70. *
  71. * @param mixed $value
  72. * @return mixed
  73. */
  74. function value($value)
  75. {
  76. return $value instanceof Closure ? $value() : $value;
  77. }
  78. }
  79. if (!function_exists('collect')) {
  80. /**
  81. * Create a collection from the given value.
  82. *
  83. * @param mixed $value
  84. * @return Collection
  85. */
  86. function collect($value = null)
  87. {
  88. return new Collection($value);
  89. }
  90. }
  91. if (!function_exists('data_fill')) {
  92. /**
  93. * Fill in data where it's missing.
  94. *
  95. * @param mixed $target
  96. * @param string|array $key
  97. * @param mixed $value
  98. * @return mixed
  99. */
  100. function data_fill(&$target, $key, $value)
  101. {
  102. return data_set($target, $key, $value, false);
  103. }
  104. }
  105. if (!function_exists('data_get')) {
  106. /**
  107. * Get an item from an array or object using "dot" notation.
  108. *
  109. * @param mixed $target
  110. * @param string|array|int $key
  111. * @param mixed $default
  112. * @return mixed
  113. */
  114. function data_get($target, $key, $default = null)
  115. {
  116. if (is_null($key)) {
  117. return $target;
  118. }
  119. $key = is_array($key) ? $key : explode('.', $key);
  120. while (!is_null($segment = array_shift($key))) {
  121. if ('*' === $segment) {
  122. if ($target instanceof Collection) {
  123. $target = $target->all();
  124. } elseif (!is_array($target)) {
  125. return value($default);
  126. }
  127. $result = [];
  128. foreach ($target as $item) {
  129. $result[] = data_get($item, $key);
  130. }
  131. return in_array('*', $key) ? Arr::collapse($result) : $result;
  132. }
  133. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  134. $target = $target[$segment];
  135. } elseif (is_object($target) && isset($target->{$segment})) {
  136. $target = $target->{$segment};
  137. } else {
  138. return value($default);
  139. }
  140. }
  141. return $target;
  142. }
  143. }
  144. if (!function_exists('data_set')) {
  145. /**
  146. * Set an item on an array or object using dot notation.
  147. *
  148. * @param mixed $target
  149. * @param string|array $key
  150. * @param mixed $value
  151. * @param bool $overwrite
  152. * @return mixed
  153. */
  154. function data_set(&$target, $key, $value, $overwrite = true)
  155. {
  156. $segments = is_array($key) ? $key : explode('.', $key);
  157. if (($segment = array_shift($segments)) === '*') {
  158. if (!Arr::accessible($target)) {
  159. $target = [];
  160. }
  161. if ($segments) {
  162. foreach ($target as &$inner) {
  163. data_set($inner, $segments, $value, $overwrite);
  164. }
  165. } elseif ($overwrite) {
  166. foreach ($target as &$inner) {
  167. $inner = $value;
  168. }
  169. }
  170. } elseif (Arr::accessible($target)) {
  171. if ($segments) {
  172. if (!Arr::exists($target, $segment)) {
  173. $target[$segment] = [];
  174. }
  175. data_set($target[$segment], $segments, $value, $overwrite);
  176. } elseif ($overwrite || !Arr::exists($target, $segment)) {
  177. $target[$segment] = $value;
  178. }
  179. } elseif (is_object($target)) {
  180. if ($segments) {
  181. if (!isset($target->{$segment})) {
  182. $target->{$segment} = [];
  183. }
  184. data_set($target->{$segment}, $segments, $value, $overwrite);
  185. } elseif ($overwrite || !isset($target->{$segment})) {
  186. $target->{$segment} = $value;
  187. }
  188. } else {
  189. $target = [];
  190. if ($segments) {
  191. data_set($target[$segment], $segments, $value, $overwrite);
  192. } elseif ($overwrite) {
  193. $target[$segment] = $value;
  194. }
  195. }
  196. return $target;
  197. }
  198. }
  199. if (!function_exists('trait_uses_recursive')) {
  200. /**
  201. * 获取一个trait里所有引用到的trait
  202. *
  203. * @param string $trait Trait
  204. * @return array
  205. */
  206. function trait_uses_recursive(string $trait): array
  207. {
  208. $traits = class_uses($trait);
  209. foreach ($traits as $trait) {
  210. $traits += trait_uses_recursive($trait);
  211. }
  212. return $traits;
  213. }
  214. }
  215. if (!function_exists('class_basename')) {
  216. /**
  217. * 获取类名(不包含命名空间)
  218. *
  219. * @param mixed $class 类名
  220. * @return string
  221. */
  222. function class_basename($class): string
  223. {
  224. $class = is_object($class) ? get_class($class) : $class;
  225. return basename(str_replace('\\', '/', $class));
  226. }
  227. }
  228. if (!function_exists('class_uses_recursive')) {
  229. /**
  230. *获取一个类里所有用到的trait,包括父类的
  231. *
  232. * @param mixed $class 类名
  233. * @return array
  234. */
  235. function class_uses_recursive($class): array
  236. {
  237. if (is_object($class)) {
  238. $class = get_class($class);
  239. }
  240. $results = [];
  241. $classes = array_merge([$class => $class], class_parents($class));
  242. foreach ($classes as $class) {
  243. $results += trait_uses_recursive($class);
  244. }
  245. return array_unique($results);
  246. }
  247. }