<?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/9/3
 * Time: 下午10:05
 */

namespace common\helpers;


use yii\base\Exception;
use yii\helpers\Url;
use yii\httpclient\Client;

class Tool {

    /**
     * @param array $cate
     * @param int $pid
     * @param int $level
     * @param string $html
     * @return array
     */
    public static function categoryTree(array $cate, $pid=0, $level=0, $html='--') {
        $tree = [];
        foreach ($cate as $key=>$value){
            if($value['pid'] == $pid) {
                $value['level'] = $level + 1;
                $html = str_repeat($html,$value['level']);
                $tree[] = $value;
                $tree = array_merge($tree, self::categoryTree($cate,$value['id'],$level+1,$html));
            }
        }
        return $tree;
    }

    /**
     * 解析数据
     * @param $args
     * @param null $defaults
     * @return array
     */
    public static function deepParse($args, $defaults = NULL){
        $result = array();
        if (is_object($args)){
            $result = get_object_vars( $args );
        } elseif (is_array($args)){
            $result =& $args;
        }else{
            parse_str($args, $result);
        }
        if (is_array($defaults))
            return array_merge($defaults, $result);
        return $result;
    }

    /**
     * 格式化金额,保留两位小数
     * @param $price
     * @param int $auto  是否自动四舍五入
     * @return string
     */
    public static function formatPrice($price ,$auto = 1){
        if(!$price) return '0.00';
        if($auto==1){
            return sprintf("%.2f", $price);
        }else{
            return substr(sprintf("%.3f",$price),0,-1);
        }
    }

    /**
     * @param $perf
     * @param int $auto
     * @param int $zoom
     * @return bool|string
     */
    public static function formatFrontPerf($perf, $auto = 0, $zoom = 100) {
        if (!$perf) return '0.00';
        $perf = $perf / $zoom;
        if ($auto == 1) {
            return sprintf("%.2f", $perf);
        } else {
            return substr(sprintf("%.3f", $perf), 0, -1);
        }
    }

    /**
     * @param $calcBonus
     * @return string
     */
    public static function formatCalcBonus($calcBonus) {
        return sprintf("%.3f", $calcBonus);
    }

    /**
     * @param $file
     * @return string
     */
    public static function getExt($file) {
        $ext = pathinfo($file ,PATHINFO_EXTENSION);
        return strtolower($ext);
    }

    /**
     * @return string
     */
    public static function getUploadUrl(){
        return \Yii::getAlias('@frontendUrl').'/'.\Yii::$app->params['upload']['dir'];
    }

    /**
     * 对二维数组排序
     *
     * @param $arrays
     * @param $sortField
     * @param int $sortOrder
     * @param int $sortType
     * @return bool
     */
    public static function sortMultiArray($arrays, $sortField, $sortOrder = SORT_ASC, $sortType = SORT_NUMERIC){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sortField];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sortOrder,$sortType,$arrays);
        return $arrays;
    }

    /**
     * 清除字符串中的所有空格
     * @param $string
     * @return mixed
     */
    public static function trimAll($string){
        $waitClean=array(" ","　");
        $cleaned=array("","");
        return str_replace($waitClean,$cleaned,$string);
    }

    /**
     * 去除两端的逗号和所有空格
     * @param $string
     * @return mixed|string
     */
    public static function trimCommaAndSpace($string){
        $result = self::trimAll($string);
        $result = trim($result, ',');
        return $result;
    }

    /**
     * 页面跳转
     * @param string $url
     * @param string $err
     * @param bool $parent
     * @return string
     */
    public static function jsJump($url = 'back' ,$err = '' ,$parent = false) {
        $output = '<script type="text/javascript">';
        if (!empty($err)) $output .= "alert('{$err}');";
        ('back' == $url)
            ? $output .= 'window.history.go(-1);'
            : ($output .= ($parent == true ? 'parent.' : '') . 'location.href="' . $url . '";');
        $output .= '</script>';
        return $output;
    }

    /**
     * 发送Curl请求
     * @param $method
     * @param $url
     * @param array $data
     * @return \yii\httpclient\Response
     * @throws \yii\httpclient\Exception
     */
    public static function sendCurlRequest($method, $url, $data = []){
        $client = new Client();
        $request = $client->createRequest()
            ->setHeaders(['content-type' => 'application/json'])
            ->addHeaders(['user-agent' => 'bonusSystem'])
            ->setFormat(Client::FORMAT_JSON)
            ->setMethod($method)
            ->setUrl($url);
        if(!empty($data)){
            $request->setData($data);
        }
        return $request->send();
    }

    /**
     * 数字补齐
     * @param $num
     * @param int $bit
     * @param string $fixStr
     * @return string
     */
    public static function numFix($num, $bit = 2, $fixStr = '0'){
        return str_pad(intval($num),$bit,$fixStr,STR_PAD_LEFT);
    }

    /**
     * 替换手机号为隐藏格式
     * @param $str
     * @return null|string|string[]
     */
    public static function hideMobile($str){
        return preg_replace('/^(\d{3})\d{4}(\d{4})$/', '${1}****${2}', $str);
    }

    /**
     * @param $str
     * @return null|string|string[]
     */
    public static function hideIdCard($str){
        return preg_replace('/^(\d{3})\d{3}(\d{4})\d{8}$/', '${1}***${2}********', $str);
    }

    /**
     * @param $str
     * @return null|string|string[]
     */
    public static function hideBankNo($str){
        if(preg_match('/^(\d{4})\d{11}(\d{4})$/', $str)){
            return preg_replace('/^(\d{4})\d{11}(\d{4})$/', '${1}***********${2}', $str);
        } elseif(preg_match('/^(\d{4})\d{8}(\d{4})$/', $str)){
            return preg_replace('/^(\d{4})\d{8}(\d{4})$/', '${1}********${2}', $str);
        } else {
            return $str;
        }
    }

    /**
     * 清空目录下的所有文件
     * @param $dir
     * @throws Exception
     */
    public static function clearDir($dir){
        $dirHandle = opendir( $dir );
        if($dirHandle === false){
            throw new Exception('打开目录失败');
        }
        while( ($file = readdir( $dirHandle )) !== false ){
            if ( $file != '.' && $file != '..' && $file != '.gitignore' ) {
                unlink( $dir . '/' . $file );
            }
        }
    }

    /**
     * 过滤特殊字符
     * @param $strParam
     * @return string|string[]|null
     */
    public static function filterSpecialChar($strParam){
        $regex = "/\/|\～|\，|\。|\！|\？|\“|\”|\【|\】|\『|\』|\：|\；|\《|\》|\’|\‘|\ |\·|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\.|\/|\;|\'|\`|\-|\=|\\\|\|/";
        return preg_replace($regex,"",$strParam);
    }

    public static function allow_area($area, $search) {
        $result = false;
        foreach ($search as $key => $value) {
            $count = count($value);
            //不够三项的，补充到三项
            if ($count < 3) {
                $num = 3 - $count;
                $value += array_fill($count, $num, '');
            }
            if ($value[0] == '') {  //说明地区选的全部
                $result = true;
                break;
            }
            if ($value[0] == $area[0] && ($value[1] == '' || $value[1] == $area[1]) && ($value[2] == '' || $value[2] == $area[2])) {
                $result = true;
                break;
            }
        }
        return $result;
    }

    /**
     * 转驼峰
     * @param $words
     * @param string $separator
     * @return string
     */
    public static function toCamelize( $words , $separator = '_') {
        if(!$words){
            return '';
        }
        $words = $separator. str_replace($separator, " ", strtolower($words));
        return ltrim(str_replace(" ", "", ucwords($words)), $separator );
    }

    public static function isConsoleApp(){
        return (\Yii::$app->id == 'app-console');
    }

    /**
     * 根据KEY合并两个数组
     * @param $arr1
     * @param $arr2
     * @param array $keyArr
     * @param string $keyField
     * @return mixed
     */
    public static function mergeArrayWithKey($arr1,$arr2,$keyArr=[],$keyField='ID'){
        foreach ($arr1 as $key=>$value){
            $arr1[$key]=array_merge($arr1[$key],$arr2[$key]??[]);
            if($keyArr){
                $arr1[$key][$keyField] = $keyArr[$key]??'';
            }
        }
        return $arr1;
    }

    /**
     * 中文UTF8 转 gbk
     * @param $text
     * @return false|string|string[]|null
     */
    public static function textConvert($text) {
        if ($text==='') {
            return '';
        }
        if (preg_match('/\,/', $text)){
            $text = preg_replace('/\,/', '|', $text);
        }
        // 只有是中文时才需要转码
        if (!preg_match('/[\x{4e00}-\x{9fa5}]/u', $text)) {
            return $text;
        }
        return mb_convert_encoding($text, 'gbk', 'utf-8');
    }

    /**
     * 数组里面的中文字符串全部转为GBK
     * @param array $arr
     * @return array
     */
    public static function arrTextConvert(array $arr){
        foreach($arr as $key => $str){
            $arr[$key] = self::textConvert($str);
        }
        return $arr;
    }

    /**
     * 获取目录内的所有文件
     * @param $dirPath
     * @return array
     */
    public static function dirFiles($dirPath){
        $files = [];
        //检测是否存在文件
        if (is_dir($dirPath)) {
            //打开目录
            if ($handle = opendir($dirPath)) {
                //返回当前文件的条目
                while (($file = readdir($handle)) !== false) {
                    //去除特殊目录
                    if ($file != "." && $file != "..") {
                        //判断子目录是否还存在子目录
                        if (!is_dir($dirPath . "/" . $file)) {
                            $files[] = $dirPath . "/" . $file;
                        } else {
                            $files[$file] = self::dirFiles($dirPath . "/" . $file);
                        }
                    }
                }
                //关闭文件夹
                closedir($handle);
            }
        }
        //返回文件夹内文件的数组
        return $files;
    }

    /**
     * 获取审核状态tag颜色
     * @param $val
     * @return string
     */
    public static function statusType($val) {
        switch ($val) {
            case '0':
                return 'info';
                break;
            case '1':
                return 'success';
                break;
            case '2':
                return 'warning';
                break;
            case '3':
                return 'danger';
                break;
            default:
                return '';
        }
    }

    /**
     * 格式化筛选
     * @param $selData
     * @param $id
     * @param $name
     * @return array
     */
    public static function formatFilter($selData, $id, $name) {
        $arr=[];
        foreach ($selData as $key=>$value){
            $arr[$key]['id']=$value[$id];
            $arr[$key]['name']=$value[$name];
        }
        return $arr;
    }

    /**
     * 随机字符串
     * @param int $length
     * @param string $prefix
     * @param string $type
     * @return string
     */
    public static function randomString($length = 10, $prefix = '', $type = 'digit') {
        if ($type == 'digit') {
            $chars = "0123456789";
        } else {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        if ($length - strlen($prefix) < 1) {
            return false;
        }
        $random_string = '';
        for ($i = 0; $i < $length - strlen($prefix); $i++) {
            $random_string .= $chars [mt_rand(0, strlen($chars) - 1)];
        }
        return $prefix.$random_string;
    }
}