helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\library;
  12. use think\facade\Route;
  13. /**
  14. * 工具类
  15. * Class helper
  16. * @package app\common\library
  17. */
  18. class helper
  19. {
  20. /**
  21. * 从object中选取属性
  22. * @param $source
  23. * @param array $columns
  24. * @return array
  25. */
  26. public static function pick($source, array $columns): array
  27. {
  28. $dataset = [];
  29. foreach ($source as $key => $item) {
  30. in_array($key, $columns) && $dataset[$key] = $item;
  31. }
  32. return $dataset;
  33. }
  34. /**
  35. * 获取数组中指定的列
  36. * @param $source
  37. * @param $column
  38. * @return array
  39. */
  40. public static function getArrayColumn($source, $column): array
  41. {
  42. $columnArr = [];
  43. foreach ($source as $item) {
  44. isset($item[$column]) && $columnArr[] = $item[$column];
  45. }
  46. return $columnArr;
  47. }
  48. /**
  49. * 获取数组中指定的列 [支持多列]
  50. * @param $source
  51. * @param $columns
  52. * @return array
  53. */
  54. public static function getArrayColumns($source, $columns): array
  55. {
  56. $columnArr = [];
  57. foreach ($source as $item) {
  58. $temp = [];
  59. foreach ($columns as $index) {
  60. $temp[$index] = $item[$index];
  61. }
  62. $columnArr[] = $temp;
  63. }
  64. return $columnArr;
  65. }
  66. /**
  67. * 随机获取指定数量的数组元素
  68. * @param array $source 数组源
  69. * @param int $num 指定数量
  70. * @return array
  71. */
  72. public static function getArrayRand(array $source, int $num): array
  73. {
  74. if (count($source) < $num) {
  75. return [];
  76. }
  77. $keys = array_rand($source, $num);
  78. if (!is_array($keys)) {
  79. return [$source[$keys]];
  80. }
  81. $data = [];
  82. foreach ($keys as $key) {
  83. $data[] = $source[$key];
  84. }
  85. return $data;
  86. }
  87. /**
  88. * 把二维数组或对象中某列设置为key返回
  89. * @param $source
  90. * @param $index
  91. * @return mixed
  92. */
  93. public static function arrayColumn2Key($source, $index)
  94. {
  95. $data = [];
  96. foreach ($source as $item) {
  97. $data[$item[$index]] = $item;
  98. }
  99. return $data;
  100. }
  101. /**
  102. * 二维数组去重
  103. * @param $source
  104. * @param $uniqueKey
  105. * @return array
  106. */
  107. public static function arrayUnique($source, $uniqueKey): array
  108. {
  109. $tmpKeys[] = [];
  110. foreach ($source as $key => &$item) {
  111. if (is_array($item) && isset($item[$uniqueKey])) {
  112. if (in_array($item[$uniqueKey], $tmpKeys)) {
  113. unset($source[$key]);
  114. } else {
  115. $tmpKeys[] = $item[$uniqueKey];
  116. }
  117. }
  118. }
  119. // 重置一下二维数组的索引
  120. return array_slice($source, 0, count($source), false);
  121. }
  122. /**
  123. * 格式化价格显示
  124. * @param mixed $number
  125. * @param bool $isMinimum 是否存在最小值
  126. * @param float $minimum
  127. * @return string
  128. */
  129. public static function number2($number, bool $isMinimum = false, float $minimum = 0.01): string
  130. {
  131. $isMinimum && $number = max($minimum, $number);
  132. return sprintf('%.2f', $number);
  133. }
  134. public static function getArrayItemByColumn($array, $column, $value)
  135. {
  136. foreach ($array as $item) {
  137. if ($item[$column] == $value) {
  138. return $item;
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * 获取二维数组中指定字段的和
  145. * @param $array
  146. * @param $column
  147. * @return float|int
  148. */
  149. public static function getArrayColumnSum($array, $column)
  150. {
  151. $sum = 0;
  152. foreach ($array as $item) {
  153. $sum = self::bcadd($sum, $item[$column]);
  154. }
  155. return $sum;
  156. }
  157. /**
  158. * 在二维数组中查找指定值
  159. * @param iterable $dataset 二维数组/或可变量对象
  160. * @param string $searchIdx 查找的索引
  161. * @param mixed $searchVal 查找的值
  162. * @return bool|mixed
  163. */
  164. public static function arraySearch(iterable $dataset, string $searchIdx, $searchVal)
  165. {
  166. foreach ($dataset as $item) {
  167. if ($item[$searchIdx] == $searchVal) {
  168. return $item;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * 过滤二维数组
  175. * @param mixed|array $array 二维数组
  176. * @param string $searchIdx 查找的索引
  177. * @param mixed $searchVal 查找的值
  178. * @return iterable|array
  179. */
  180. public static function arrayFilterAsVal($array, string $searchIdx, $searchVal)
  181. {
  182. $data = [];
  183. foreach ($array as $key => $item) {
  184. if (isset($item[$searchIdx]) && $item[$searchIdx] == $searchVal) {
  185. $data[$key] = $item;
  186. }
  187. }
  188. return $data;
  189. }
  190. public static function setDataAttribute(&$source, $defaultData, $isArray = false)
  191. {
  192. if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
  193. foreach ($dataSource as &$item) {
  194. foreach ($defaultData as $key => $value) {
  195. $item[$key] = $value;
  196. }
  197. }
  198. return $source;
  199. }
  200. public static function bcsub($leftOperand, $rightOperand, $scale = 2): string
  201. {
  202. return \bcsub($leftOperand, $rightOperand, $scale);
  203. }
  204. public static function bcadd($leftOperand, $rightOperand, $scale = 2): string
  205. {
  206. return \bcadd($leftOperand, $rightOperand, $scale);
  207. }
  208. public static function bcmul($leftOperand, $rightOperand, $scale = 2): string
  209. {
  210. return \bcmul($leftOperand, $rightOperand, $scale);
  211. }
  212. public static function bcdiv($leftOperand, $rightOperand, int $scale = 2): ?string
  213. {
  214. return \bcdiv($leftOperand, $rightOperand, $scale);
  215. }
  216. /**
  217. * 浮点数比较
  218. * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
  219. * @param $leftOperand
  220. * @param $rightOperand
  221. * @param int $scale
  222. * @return int
  223. */
  224. public static function bccomp($leftOperand, $rightOperand, int $scale = 2): int
  225. {
  226. return \bccomp($leftOperand, $rightOperand, $scale);
  227. }
  228. /**
  229. * 比较两个数值是否相等
  230. * @param $leftOperand
  231. * @param $rightOperand
  232. * @param int $scale
  233. * @return bool
  234. */
  235. public static function bcequal($leftOperand, $rightOperand, int $scale = 2): bool
  236. {
  237. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  238. }
  239. /**
  240. * 数组转为json
  241. * @param $data
  242. * @param int $options
  243. * @return false|string
  244. */
  245. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
  246. {
  247. return json_encode($data, $options);
  248. }
  249. /**
  250. * json转义为数组
  251. * @param $json
  252. * @return array|mixed
  253. */
  254. public static function jsonDecode($json)
  255. {
  256. return json_decode($json, true);
  257. }
  258. /**
  259. * 将xml数据转换为array
  260. * @param string $xml
  261. * @return array|mixed
  262. */
  263. public static function xmlToArray(string $xml)
  264. {
  265. // 禁止引用外部xml实体
  266. if (PHP_VERSION_ID < 80000) {
  267. libxml_disable_entity_loader(true);
  268. }
  269. return self::jsonDecode(self::jsonEncode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)));
  270. }
  271. /**
  272. * URL生成
  273. * @param string $url
  274. * @param array $vars
  275. * @return string
  276. */
  277. public static function buildUrl(string $url, array $vars = []): string
  278. {
  279. // 生成完整的url (包含域名)
  280. $url = Route::buildUrl($url, $vars)->domain(true)->build();
  281. // 判断当前访问模式是否为兼容模式
  282. if (strpos(request()->url(), '.php?s=') && strpos($url, '.php/')) {
  283. $url = str_replace('.php/', '.php?s=/', $url);
  284. }
  285. return $url;
  286. }
  287. /**
  288. * 检查目录是否可写
  289. * @param $path
  290. * @return bool
  291. */
  292. public static function checkWriteable($path): bool
  293. {
  294. try {
  295. !is_dir($path) && mkdir($path, 0755);
  296. if (!is_dir($path))
  297. return false;
  298. $fileName = $path . '/_test_write.txt';
  299. if ($fp = fopen($fileName, 'w')) {
  300. return fclose($fp) && unlink($fileName);
  301. }
  302. } catch (\Exception $e) {
  303. }
  304. return false;
  305. }
  306. /**
  307. * 记录info日志
  308. * @param string $name
  309. * @param array $content
  310. */
  311. public static function logInfo(string $name, array $content)
  312. {
  313. $content['name'] = $name;
  314. log_record($content, 'info');
  315. }
  316. /**
  317. * 根据指定路径创建文件夹
  318. * @param string $dirPath
  319. * @return string
  320. */
  321. public static function mkdir(string $dirPath): string
  322. {
  323. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  324. return $dirPath;
  325. }
  326. /**
  327. * 将字符串转换为字节
  328. * @param string $from
  329. * @return int|null
  330. */
  331. public static function convertToBytes(string $from): ?int
  332. {
  333. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  334. $number = substr($from, 0, -2);
  335. $suffix = strtoupper(substr($from, -2));
  336. // B or no suffix
  337. if (is_numeric(substr($suffix, 0, 1))) {
  338. return preg_replace('/[^\d]/', '', $from);
  339. }
  340. $exponent = array_flip($units)[$suffix] ?? null;
  341. if ($exponent === null) {
  342. return null;
  343. }
  344. return $number * (1024 ** $exponent);
  345. }
  346. /**
  347. * 设置默认的检索数据
  348. * @param array $query
  349. * @param array $default
  350. * @return array
  351. */
  352. public static function setQueryDefaultValue(array $query, array $default = []): array
  353. {
  354. $data = array_merge($default, $query);
  355. foreach ($query as $field => $value) {
  356. // 不存在默认值跳出循环
  357. if (!isset($default[$field])) continue;
  358. // 如果传参为空, 设置默认值
  359. if (empty($value) && $value !== '0'&& $value !== 0) {
  360. $data[$field] = $default[$field];
  361. }
  362. }
  363. return $data;
  364. }
  365. /**
  366. * 科学计数格式转化为字符串
  367. * @param string $num
  368. * @param int $double
  369. * @return string
  370. */
  371. public static function scToStr(string $num, int $double = 6): string
  372. {
  373. if (false !== stripos($num, "e")) {
  374. $a = explode("e", strtolower($num));
  375. return bcmul($a[0], bcpow('10', $a[1], $double), $double);
  376. }
  377. return $num;
  378. }
  379. }