helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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 bcsubs(array $values, $scale = 2): string
  205. {
  206. $result = null;
  207. foreach ($values as $value) {
  208. $result = is_null($result) ? $value : self::bcsub($result, $value, $scale);
  209. }
  210. return $result;
  211. }
  212. public static function bcadd($leftOperand, $rightOperand, $scale = 2): string
  213. {
  214. return \bcadd($leftOperand, $rightOperand, $scale);
  215. }
  216. public static function bcmul($leftOperand, $rightOperand, $scale = 2): string
  217. {
  218. return \bcmul($leftOperand, $rightOperand, $scale);
  219. }
  220. public static function bcdiv($leftOperand, $rightOperand, int $scale = 2): ?string
  221. {
  222. return \bcdiv($leftOperand, $rightOperand, $scale);
  223. }
  224. /**
  225. * 浮点数比较
  226. * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
  227. * @param $leftOperand
  228. * @param $rightOperand
  229. * @param int $scale
  230. * @return int
  231. */
  232. public static function bccomp($leftOperand, $rightOperand, int $scale = 2): int
  233. {
  234. return \bccomp($leftOperand, $rightOperand, $scale);
  235. }
  236. /**
  237. * 比较两个数值是否相等
  238. * @param $leftOperand
  239. * @param $rightOperand
  240. * @param int $scale
  241. * @return bool
  242. */
  243. public static function bcequal($leftOperand, $rightOperand, int $scale = 2): bool
  244. {
  245. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  246. }
  247. /**
  248. * 数组转为json
  249. * @param $data
  250. * @param int $options
  251. * @return false|string
  252. */
  253. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
  254. {
  255. return json_encode($data, $options);
  256. }
  257. /**
  258. * json转义为数组
  259. * @param $json
  260. * @return array|mixed
  261. */
  262. public static function jsonDecode($json)
  263. {
  264. return json_decode($json, true);
  265. }
  266. /**
  267. * 将xml数据转换为array
  268. * @param string $xml
  269. * @return array|mixed
  270. */
  271. public static function xmlToArray(string $xml)
  272. {
  273. // 禁止引用外部xml实体
  274. if (PHP_VERSION_ID < 80000) {
  275. libxml_disable_entity_loader(true);
  276. }
  277. return self::jsonDecode(self::jsonEncode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)));
  278. }
  279. /**
  280. * URL生成
  281. * @param string $url
  282. * @param array $vars
  283. * @return string
  284. */
  285. public static function buildUrl(string $url, array $vars = []): string
  286. {
  287. // 生成完整的url (包含域名)
  288. $url = Route::buildUrl($url, $vars)->domain(true)->build();
  289. // 判断当前访问模式是否为兼容模式
  290. if (strpos(request()->url(), '.php?s=') && strpos($url, '.php/')) {
  291. $url = str_replace('.php/', '.php?s=/', $url);
  292. }
  293. return $url;
  294. }
  295. /**
  296. * 检查目录是否可写
  297. * @param $path
  298. * @return bool
  299. */
  300. public static function checkWriteable($path): bool
  301. {
  302. try {
  303. !is_dir($path) && mkdir($path, 0755);
  304. if (!is_dir($path))
  305. return false;
  306. $fileName = $path . '/_test_write.txt';
  307. if ($fp = fopen($fileName, 'w')) {
  308. return fclose($fp) && unlink($fileName);
  309. }
  310. } catch (\Exception $e) {
  311. }
  312. return false;
  313. }
  314. /**
  315. * 记录info日志
  316. * @param string $name
  317. * @param array $content
  318. */
  319. public static function logInfo(string $name, array $content)
  320. {
  321. $content['name'] = $name;
  322. log_record($content, 'info');
  323. }
  324. /**
  325. * 根据指定路径创建文件夹
  326. * @param string $dirPath
  327. * @return string
  328. */
  329. public static function mkdir(string $dirPath): string
  330. {
  331. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  332. return $dirPath;
  333. }
  334. /**
  335. * 将字符串转换为字节
  336. * @param string $from
  337. * @return int|null
  338. */
  339. public static function convertToBytes(string $from): ?int
  340. {
  341. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  342. $number = substr($from, 0, -2);
  343. $suffix = strtoupper(substr($from, -2));
  344. // B or no suffix
  345. if (is_numeric(substr($suffix, 0, 1))) {
  346. return preg_replace('/[^\d]/', '', $from);
  347. }
  348. $exponent = array_flip($units)[$suffix] ?? null;
  349. if ($exponent === null) {
  350. return null;
  351. }
  352. return $number * (1024 ** $exponent);
  353. }
  354. /**
  355. * 设置默认的检索数据
  356. * @param array $query
  357. * @param array $default
  358. * @return array
  359. */
  360. public static function setQueryDefaultValue(array $query, array $default = []): array
  361. {
  362. $data = \array_merge($default, $query);
  363. foreach ($query as $field => $value) {
  364. // 不存在默认值跳出循环
  365. if (!isset($default[$field])) continue;
  366. // 如果传参为空, 设置默认值
  367. if (empty($value) && $value !== '0'&& $value !== 0) {
  368. $data[$field] = $default[$field];
  369. }
  370. }
  371. return $data;
  372. }
  373. /**
  374. * 科学计数格式转化为字符串
  375. * @param string $num
  376. * @param int $double
  377. * @return string
  378. */
  379. public static function scToStr(string $num, int $double = 6): string
  380. {
  381. if (false !== stripos($num, "e")) {
  382. $a = explode("e", strtolower($num));
  383. return bcmul($a[0], bcpow('10', $a[1], $double), $double);
  384. }
  385. return $num;
  386. }
  387. }