helper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. public static function number2($number, $isMinimum = false, $minimum = 0.01): string
  80. {
  81. $isMinimum && $number = max($minimum, $number);
  82. return sprintf('%.2f', $number);
  83. }
  84. public static function getArrayItemByColumn($array, $column, $value)
  85. {
  86. foreach ($array as $item) {
  87. if ($item[$column] == $value) {
  88. return $item;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * 获取二维数组中指定字段的和
  95. * @param $array
  96. * @param $column
  97. * @return float|int
  98. */
  99. public static function getArrayColumnSum($array, $column)
  100. {
  101. $sum = 0;
  102. foreach ($array as $item) {
  103. $sum += $item[$column] * 100;
  104. }
  105. return $sum / 100;
  106. }
  107. /**
  108. * 在二维数组中查找指定值
  109. * @param array $array 二维数组
  110. * @param string $searchIdx 查找的索引
  111. * @param mixed $searchVal 查找的值
  112. * @return bool|mixed
  113. */
  114. public static function arraySearch(array $array, string $searchIdx, $searchVal)
  115. {
  116. foreach ($array as $item) {
  117. if ($item[$searchIdx] == $searchVal) {
  118. return $item;
  119. }
  120. }
  121. return false;
  122. }
  123. public static function setDataAttribute(&$source, $defaultData, $isArray = false)
  124. {
  125. if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
  126. foreach ($dataSource as &$item) {
  127. foreach ($defaultData as $key => $value) {
  128. $item[$key] = $value;
  129. }
  130. }
  131. return $source;
  132. }
  133. public static function bcsub($leftOperand, $rightOperand, $scale = 2): string
  134. {
  135. return \bcsub($leftOperand, $rightOperand, $scale);
  136. }
  137. public static function bcadd($leftOperand, $rightOperand, $scale = 2): string
  138. {
  139. return \bcadd($leftOperand, $rightOperand, $scale);
  140. }
  141. public static function bcmul($leftOperand, $rightOperand, $scale = 2): string
  142. {
  143. return \bcmul($leftOperand, $rightOperand, $scale);
  144. }
  145. public static function bcdiv($leftOperand, $rightOperand, int $scale = 2): ?string
  146. {
  147. return \bcdiv($leftOperand, $rightOperand, $scale);
  148. }
  149. /**
  150. * 浮点数比较
  151. * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
  152. * @param $leftOperand
  153. * @param $rightOperand
  154. * @param int $scale
  155. * @return int
  156. */
  157. public static function bccomp($leftOperand, $rightOperand, int $scale = 2): int
  158. {
  159. return \bccomp($leftOperand, $rightOperand, $scale);
  160. }
  161. /**
  162. * 比较两个数值是否相等
  163. * @param $leftOperand
  164. * @param $rightOperand
  165. * @param int $scale
  166. * @return bool
  167. */
  168. public static function bcequal($leftOperand, $rightOperand, int $scale = 2): bool
  169. {
  170. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  171. }
  172. /**
  173. * 数组转为json
  174. * @param $data
  175. * @param int $options
  176. * @return false|string
  177. */
  178. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
  179. {
  180. return json_encode($data, $options);
  181. }
  182. /**
  183. * json转义为数组
  184. * @param $json
  185. * @return array|mixed
  186. */
  187. public static function jsonDecode($json)
  188. {
  189. return json_decode($json, true);
  190. }
  191. /**
  192. * 检查目录是否可写
  193. * @param $path
  194. * @return bool
  195. */
  196. public static function checkWriteable($path): bool
  197. {
  198. try {
  199. !is_dir($path) && mkdir($path, 0755);
  200. if (!is_dir($path))
  201. return false;
  202. $fileName = $path . '/_test_write.txt';
  203. if ($fp = fopen($fileName, 'w')) {
  204. return fclose($fp) && unlink($fileName);
  205. }
  206. } catch (\Exception $e) {
  207. }
  208. return false;
  209. }
  210. /**
  211. * 记录info日志
  212. * @param string $name
  213. * @param array $content
  214. */
  215. public static function logInfo(string $name, array $content)
  216. {
  217. $content['name'] = $name;
  218. log_record($content, 'info');
  219. }
  220. }