helper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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)
  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)
  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)
  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)
  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 string $searchVal 查找的值
  112. * @return bool
  113. */
  114. public static function arraySearch($array, $searchIdx, $searchVal)
  115. {
  116. foreach ($array as $item) {
  117. if ($item[$searchIdx] == $searchVal) return $item;
  118. }
  119. return false;
  120. }
  121. public static function setDataAttribute(&$source, $defaultData, $isArray = false)
  122. {
  123. if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
  124. foreach ($dataSource as &$item) {
  125. foreach ($defaultData as $key => $value) {
  126. $item[$key] = $value;
  127. }
  128. }
  129. return $source;
  130. }
  131. public static function bcsub($leftOperand, $rightOperand, $scale = 2)
  132. {
  133. return \bcsub($leftOperand, $rightOperand, $scale);
  134. }
  135. public static function bcadd($leftOperand, $rightOperand, $scale = 2)
  136. {
  137. return \bcadd($leftOperand, $rightOperand, $scale);
  138. }
  139. public static function bcmul($leftOperand, $rightOperand, $scale = 2)
  140. {
  141. return \bcmul($leftOperand, $rightOperand, $scale);
  142. }
  143. public static function bcdiv($leftOperand, $rightOperand, int $scale = 2)
  144. {
  145. return \bcdiv($leftOperand, $rightOperand, $scale);
  146. }
  147. /**
  148. * 浮点数比较
  149. * 若二个字符串一样大则返回 0;若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
  150. * @param $leftOperand
  151. * @param $rightOperand
  152. * @param int $scale
  153. * @return int
  154. */
  155. public static function bccomp($leftOperand, $rightOperand, $scale = 2)
  156. {
  157. return \bccomp($leftOperand, $rightOperand, $scale);
  158. }
  159. /**
  160. * 比较两个数值是否相等
  161. * @param $leftOperand
  162. * @param $rightOperand
  163. * @param int $scale
  164. * @return bool
  165. */
  166. public static function bcequal($leftOperand, $rightOperand, $scale = 2)
  167. {
  168. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  169. }
  170. /**
  171. * 数组转为json
  172. * @param $data
  173. * @param int $options
  174. * @return string
  175. */
  176. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE): string
  177. {
  178. return json_encode($data, $options);
  179. }
  180. /**
  181. * json转义为数组
  182. * @param $json
  183. * @return array
  184. */
  185. public static function jsonDecode($json): array
  186. {
  187. return json_decode($json, true);
  188. }
  189. /**
  190. * 检查目录是否可写
  191. * @param $path
  192. * @return bool
  193. */
  194. public static function checkWriteable($path): bool
  195. {
  196. try {
  197. !is_dir($path) && mkdir($path, 0755);
  198. if (!is_dir($path))
  199. return false;
  200. $fileName = $path . '/_test_write.txt';
  201. if ($fp = fopen($fileName, 'w')) {
  202. return fclose($fp) && unlink($fileName);
  203. }
  204. } catch (\Exception $e) {
  205. }
  206. return false;
  207. }
  208. }