helper.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\library;
  12. /**
  13. * 工具类
  14. * Class helper
  15. * @package app\common\library
  16. */
  17. class helper
  18. {
  19. /**
  20. * 从object中选取属性
  21. * @param $source
  22. * @param array $columns
  23. * @return array
  24. */
  25. public static function pick($source, array $columns): array
  26. {
  27. $dataset = [];
  28. foreach ($source as $key => $item) {
  29. in_array($key, $columns) && $dataset[$key] = $item;
  30. }
  31. return $dataset;
  32. }
  33. /**
  34. * 获取数组中指定的列
  35. * @param $source
  36. * @param $column
  37. * @return array
  38. */
  39. public static function getArrayColumn($source, $column): array
  40. {
  41. $columnArr = [];
  42. foreach ($source as $item) {
  43. isset($item[$column]) && $columnArr[] = $item[$column];
  44. }
  45. return $columnArr;
  46. }
  47. /**
  48. * 获取数组中指定的列 [支持多列]
  49. * @param $source
  50. * @param $columns
  51. * @return array
  52. */
  53. public static function getArrayColumns($source, $columns): array
  54. {
  55. $columnArr = [];
  56. foreach ($source as $item) {
  57. $temp = [];
  58. foreach ($columns as $index) {
  59. $temp[$index] = $item[$index];
  60. }
  61. $columnArr[] = $temp;
  62. }
  63. return $columnArr;
  64. }
  65. /**
  66. * 把二维数组中某列设置为key返回
  67. * @param $source
  68. * @param $index
  69. * @return array
  70. */
  71. public static function arrayColumn2Key($source, $index): array
  72. {
  73. $data = [];
  74. foreach ($source as $item) {
  75. $data[$item[$index]] = $item;
  76. }
  77. return $data;
  78. }
  79. /**
  80. * 格式化价格显示
  81. * @param mixed $number
  82. * @param bool $isMinimum 是否存在最小值
  83. * @param float $minimum
  84. * @return string
  85. */
  86. public static function number2($number, bool $isMinimum = false, float $minimum = 0.01): string
  87. {
  88. $isMinimum && $number = max($minimum, $number);
  89. return sprintf('%.2f', $number);
  90. }
  91. public static function getArrayItemByColumn($array, $column, $value)
  92. {
  93. foreach ($array as $item) {
  94. if ($item[$column] == $value) {
  95. return $item;
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * 获取二维数组中指定字段的和
  102. * @param $array
  103. * @param $column
  104. * @return float|int
  105. */
  106. public static function getArrayColumnSum($array, $column)
  107. {
  108. $sum = 0;
  109. foreach ($array as $item) {
  110. $sum = self::bcadd($sum, $item[$column]);
  111. }
  112. return $sum;
  113. }
  114. /**
  115. * 在二维数组中查找指定值
  116. * @param array $array 二维数组
  117. * @param string $searchIdx 查找的索引
  118. * @param mixed $searchVal 查找的值
  119. * @return bool|mixed
  120. */
  121. public static function arraySearch(array $array, string $searchIdx, $searchVal)
  122. {
  123. foreach ($array as $item) {
  124. if ($item[$searchIdx] == $searchVal) {
  125. return $item;
  126. }
  127. }
  128. return false;
  129. }
  130. public static function setDataAttribute(&$source, $defaultData, $isArray = false)
  131. {
  132. if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
  133. foreach ($dataSource as &$item) {
  134. foreach ($defaultData as $key => $value) {
  135. $item[$key] = $value;
  136. }
  137. }
  138. return $source;
  139. }
  140. public static function bcsub($leftOperand, $rightOperand, $scale = 2): string
  141. {
  142. return \bcsub($leftOperand, $rightOperand, $scale);
  143. }
  144. public static function bcadd($leftOperand, $rightOperand, $scale = 2): string
  145. {
  146. return \bcadd($leftOperand, $rightOperand, $scale);
  147. }
  148. public static function bcmul($leftOperand, $rightOperand, $scale = 2): string
  149. {
  150. return \bcmul($leftOperand, $rightOperand, $scale);
  151. }
  152. public static function bcdiv($leftOperand, $rightOperand, int $scale = 2): ?string
  153. {
  154. return \bcdiv($leftOperand, $rightOperand, $scale);
  155. }
  156. /**
  157. * 浮点数比较
  158. * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
  159. * @param $leftOperand
  160. * @param $rightOperand
  161. * @param int $scale
  162. * @return int
  163. */
  164. public static function bccomp($leftOperand, $rightOperand, int $scale = 2): int
  165. {
  166. return \bccomp($leftOperand, $rightOperand, $scale);
  167. }
  168. /**
  169. * 比较两个数值是否相等
  170. * @param $leftOperand
  171. * @param $rightOperand
  172. * @param int $scale
  173. * @return bool
  174. */
  175. public static function bcequal($leftOperand, $rightOperand, int $scale = 2): bool
  176. {
  177. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  178. }
  179. /**
  180. * 数组转为json
  181. * @param $data
  182. * @param int $options
  183. * @return false|string
  184. */
  185. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
  186. {
  187. return json_encode($data, $options);
  188. }
  189. /**
  190. * json转义为数组
  191. * @param $json
  192. * @return array|mixed
  193. */
  194. public static function jsonDecode($json)
  195. {
  196. return json_decode($json, true);
  197. }
  198. /**
  199. * 检查目录是否可写
  200. * @param $path
  201. * @return bool
  202. */
  203. public static function checkWriteable($path): bool
  204. {
  205. try {
  206. !is_dir($path) && mkdir($path, 0755);
  207. if (!is_dir($path))
  208. return false;
  209. $fileName = $path . '/_test_write.txt';
  210. if ($fp = fopen($fileName, 'w')) {
  211. return fclose($fp) && unlink($fileName);
  212. }
  213. } catch (\Exception $e) {
  214. }
  215. return false;
  216. }
  217. /**
  218. * 记录info日志
  219. * @param string $name
  220. * @param array $content
  221. */
  222. public static function logInfo(string $name, array $content)
  223. {
  224. $content['name'] = $name;
  225. log_record($content, 'info');
  226. }
  227. /**
  228. * 将字符串转换为字节
  229. * @param string $from
  230. * @return int|null
  231. */
  232. public static function convertToBytes(string $from): ?int
  233. {
  234. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  235. $number = substr($from, 0, -2);
  236. $suffix = strtoupper(substr($from, -2));
  237. // B or no suffix
  238. if (is_numeric(substr($suffix, 0, 1))) {
  239. return preg_replace('/[^\d]/', '', $from);
  240. }
  241. $exponent = array_flip($units)[$suffix] ?? null;
  242. if ($exponent === null) {
  243. return null;
  244. }
  245. return $number * (1024 ** $exponent);
  246. }
  247. }