123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- namespace app\common\library;
- /**
- * 工具类
- * Class helper
- * @package app\common\library
- */
- class helper
- {
- /**
- * 从object中选取属性
- * @param $source
- * @param array $columns
- * @return array
- */
- public static function pick($source, array $columns)
- {
- $dataset = [];
- foreach ($source as $key => $item) {
- in_array($key, $columns) && $dataset[$key] = $item;
- }
- return $dataset;
- }
- /**
- * 获取数组中指定的列
- * @param $source
- * @param $column
- * @return array
- */
- public static function getArrayColumn($source, $column): array
- {
- $columnArr = [];
- foreach ($source as $item) {
- isset($item[$column]) && $columnArr[] = $item[$column];
- }
- return $columnArr;
- }
- /**
- * 获取数组中指定的列 [支持多列]
- * @param $source
- * @param $columns
- * @return array
- */
- public static function getArrayColumns($source, $columns)
- {
- $columnArr = [];
- foreach ($source as $item) {
- $temp = [];
- foreach ($columns as $index) {
- $temp[$index] = $item[$index];
- }
- $columnArr[] = $temp;
- }
- return $columnArr;
- }
- /**
- * 把二维数组中某列设置为key返回
- * @param $source
- * @param $index
- * @return array
- */
- public static function arrayColumn2Key($source, $index)
- {
- $data = [];
- foreach ($source as $item) {
- $data[$item[$index]] = $item;
- }
- return $data;
- }
- public static function number2($number, $isMinimum = false, $minimum = 0.01)
- {
- $isMinimum && $number = max($minimum, $number);
- return sprintf('%.2f', $number);
- }
- public static function getArrayItemByColumn($array, $column, $value)
- {
- foreach ($array as $item) {
- if ($item[$column] == $value) {
- return $item;
- }
- }
- return false;
- }
- /**
- * 获取二维数组中指定字段的和
- * @param $array
- * @param $column
- * @return float|int
- */
- public static function getArrayColumnSum($array, $column)
- {
- $sum = 0;
- foreach ($array as $item) {
- $sum += $item[$column] * 100;
- }
- return $sum / 100;
- }
- /**
- * 在二维数组中查找指定值
- * @param array $array 二维数组
- * @param string $searchIdx 查找的索引
- * @param string $searchVal 查找的值
- * @return bool
- */
- public static function arraySearch($array, $searchIdx, $searchVal)
- {
- foreach ($array as $item) {
- if ($item[$searchIdx] == $searchVal) return $item;
- }
- return false;
- }
- public static function setDataAttribute(&$source, $defaultData, $isArray = false)
- {
- if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
- foreach ($dataSource as &$item) {
- foreach ($defaultData as $key => $value) {
- $item[$key] = $value;
- }
- }
- return $source;
- }
- public static function bcsub($leftOperand, $rightOperand, $scale = 2)
- {
- return \bcsub($leftOperand, $rightOperand, $scale);
- }
- public static function bcadd($leftOperand, $rightOperand, $scale = 2)
- {
- return \bcadd($leftOperand, $rightOperand, $scale);
- }
- public static function bcmul($leftOperand, $rightOperand, $scale = 2)
- {
- return \bcmul($leftOperand, $rightOperand, $scale);
- }
- public static function bcdiv($leftOperand, $rightOperand, int $scale = 2)
- {
- return \bcdiv($leftOperand, $rightOperand, $scale);
- }
- public static function bccomp($leftOperand, $rightOperand, $scale = 2)
- {
- return \bccomp($leftOperand, $rightOperand, $scale);
- }
- public static function bcequal($leftOperand, $rightOperand, $scale = 2)
- {
- return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
- }
- /**
- * 数组转为json
- * @param $data
- * @param int $options
- * @return string
- */
- public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
- {
- return json_encode($data, $options);
- }
- /**
- * json转义为数组
- * @param $json
- * @return array
- */
- public static function jsonDecode($json)
- {
- return json_decode($json, true);
- }
- /**
- * 检查目录是否可写
- * @param $path
- * @return bool
- */
- public static function checkWriteable($path)
- {
- try {
- !is_dir($path) && mkdir($path, 0755);
- if (!is_dir($path))
- return false;
- $fileName = $path . '/_test_write.txt';
- if ($fp = fopen($fileName, 'w')) {
- return fclose($fp) && unlink($fileName);
- }
- } catch (\Exception $e) {
- }
- return false;
- }
- public static function curlRequest($url,$post='',$headers= array('Content-Type: text/plain')){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- //设置https
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
- if($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- curl_close($curl);
- return $data;
- }
- }
|