helper.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. public static function bccomp($leftOperand, $rightOperand, $scale = 2)
  148. {
  149. return \bccomp($leftOperand, $rightOperand, $scale);
  150. }
  151. public static function bcequal($leftOperand, $rightOperand, $scale = 2)
  152. {
  153. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  154. }
  155. /**
  156. * 数组转为json
  157. * @param $data
  158. * @param int $options
  159. * @return string
  160. */
  161. public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
  162. {
  163. return json_encode($data, $options);
  164. }
  165. /**
  166. * json转义为数组
  167. * @param $json
  168. * @return array
  169. */
  170. public static function jsonDecode($json)
  171. {
  172. return json_decode($json, true);
  173. }
  174. /**
  175. * 检查目录是否可写
  176. * @param $path
  177. * @return bool
  178. */
  179. public static function checkWriteable($path)
  180. {
  181. try {
  182. !is_dir($path) && mkdir($path, 0755);
  183. if (!is_dir($path))
  184. return false;
  185. $fileName = $path . '/_test_write.txt';
  186. if ($fp = fopen($fileName, 'w')) {
  187. return fclose($fp) && unlink($fileName);
  188. }
  189. } catch (\Exception $e) {
  190. }
  191. return false;
  192. }
  193. public static function curlRequest($url,$post='',$headers= array('Content-Type: text/plain')){
  194. $curl = curl_init();
  195. curl_setopt($curl, CURLOPT_URL, $url);
  196. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  197. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  198. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  199. //设置https
  200. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
  201. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
  202. if($post) {
  203. curl_setopt($curl, CURLOPT_POST, 1);
  204. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  205. }
  206. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers
  207. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  208. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  209. $data = curl_exec($curl);
  210. if (curl_errno($curl)) {
  211. return curl_error($curl);
  212. }
  213. curl_close($curl);
  214. return $data;
  215. }
  216. }