常用操作方法封装(文件处理、数据处理),复制即用

文件压缩

    /**
     * @description:创建压缩文件,使用前需要use ZipArchive;
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function zipCreate(array $filePaths, string $zipPath): void
    {
        $zip = new ZipArchive;
        $zip->open($zipPath, ZipArchive::CREATE);
        foreach ($filePaths as $file) {
            $zip->addFile($file, basename($file));   //向压缩包中添加文件
        }
        $zip->close();  //关闭压缩包
    }

文件下载 

/**
     * @description:文件下载,下载完自动删除,配合前端文件流下载 
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function fileDownload(string $filePath): void
    {
        $fp = fopen($filePath, "r");
        $file_size = filesize($filePath);
        $buffer = 1024;  //设置一次读取的字节数,每读取一次,就输出数据(即返回给浏览器)
        $file_count = 0; //读取的总字节数
        //向浏览器返回数据 
        while (!feof($fp) && $file_count < $file_size) {
            $file_con = fread($fp, $buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
        //下载完成后删除文件
        if ($file_count >= $file_size) {
            unlink($filePath);
        }
    }

文件更新 

   /**
     * @description:文件更新,新文件内容更新到原文件中,并删除新文件 
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function fileUpdate(string $newPath, string $oldPath): void
    {
        $path = $newPath;
        $oldPath = $oldPath;
        if (file_exists($path) && $path !== $oldPath && $newPath) {
            $content = file_get_contents($path);
            file_put_contents($oldPath, $content);
            unlink($path);
        }
    }

文件删除相关

 /**
     * @description:批量删除文件 
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function deleteBatchFile(array  $paths): void
    {
        array_map(function ($path) {     
                $path = $path;
            file_exists($path) && is_file($path) && unlink($path);
        }, $paths);
    }
    /**
     * @description:删除单个文件 
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function deleteFile(string   $path): void
    {
        file_exists($path) && is_file($path) && unlink($path);
    }

数据处理相关

    /**
     * treeData 生成树状数据
     * @author Quan
     * @param  array $items  原数据
     * @param  string $son 存放孩子节点字段名
     * @param  string $id 排序显示的键,一般是主键 
     * @param  array  $pid  父id
     * @return array  树状数据
     */
    protected function treeData(array $items = [], string  $pid = 'parent_id', string $id = 'id', string  $son = 'children'): array
    {
        $tree = [];
        $tmpData = []; //临时数据
        foreach ($items as $item) {
            $tmpData[$item[$id]] = $item;
        }
        foreach ($items as $item) {
            if (isset($tmpData[$item[$pid]])) {
                $tmpData[$item[$pid]][$son][] = &$tmpData[$item[$id]];
            } else {
                $tree[] = &$tmpData[$item[$id]];
            }
        }
        unset($tmpData);
        return $tree;
    }   
 /**
     * @description:过滤数组为null和''的字段,array_filter也能过滤,但其默认会把0、false这样具体的值过滤掉
     * @Author: Quan
     * @param  array  $arr
     * @return array
     */
    static protected function filterArray(array $arr): array
    {
        foreach ($arr as $k => $v) {
            if ($v === '' || $v === null) {
                unset($arr[$k]);
            }
        }
        return $arr;
    }   
 /**
     * @description: 10进制转36进制
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    protected function createCode(string $number): string
    {
        static $sourceString = [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x',
            'y', 'z'
        ];

        $num = $number;
        $code = '';
        while ($num) {
            $mod = bcmod($num, '36');
            $num = bcdiv($num, '36');
            $code = "{$sourceString[$mod]}{$code}"; //邀请码拼接
        }
        //判断code的长度
        if (empty($code[4]))
            $code = str_pad($code, 5, '0', STR_PAD_LEFT); //长度不够拼接'0'

        return $code;
    }
    /**
     * @description: 删除html和空格
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    public function htmlTagFilter(?string $str): string
    {
        $resStr = '';
        if (!empty($str)) {
            $tmpStr = strip_tags($str);
            $resStr = str_replace(array("&nbsp;", "&ensp;", "&emsp;", "&thinsp;", "&zwnj;", "&zwj;", "&ldquo;", "&rdquo;"), "", $tmpStr);
        }
        return $resStr;
    }
    /**
     * @description: 字符串截取
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    public function strCut(?string $str, int $startIndex, int $length, string $prefix = '...'): string
    {
        $resStr = '';
        if (!empty($str)) {
            $resStr = mb_strlen($str) > $length ? mb_substr($str, $startIndex, $length) . $prefix : $str;
        }
        return $resStr;
    }
    /**
     * @description: xml 转换数组
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    public function xmlToArray(string $xml): array
    {

        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }
    /**
     * @description: 号码打码
     * @Author: Quan
     * @param {type} 
     * @return: 
     */
    public function mobileMosaicStr(string $mobile): string
    {
        return  substr($mobile, 0, 3) . '****' . substr($mobile, 7);
    }
    /**
     * 数组转对象
     * @param Array $array
     * @author Quan
     * @return Object
     */
    protected function arrayTransitionObject(array $array): object
    {
        if (is_array($array)) {
            $obj = new stdClass();
            foreach ($array as $key => $val) {
                $obj->$key = $val;
            }
        } else {
            $obj = $array;
        }
        return $obj;
    }
    /**
     * @description: 分析枚举类型配置值 格式 a:名称1,b:名称2
     * @param {str:string} 
     * @return: 
     */
    protected function parseValue(string $str): array
    {
        $tmp = preg_split('/[,;\r\n]+/', $str);
        $value = [];
        if (strpos($str, ':')) {
            foreach ($tmp as $val) {
                list($k, $v) = explode(':', $val);
                $value[$k] = $v;
            }
        } else {
            $value = $tmp;
        }
        return $value;
    }
    /**
     * @description:数据分组 
     * @param {dataArr:需要分组的数据;keyStr:分组依据} 
     * @author Quan
     * @return: 
     */
    static protected function dataGroup(array $dataArr, string $keyStr): array
    {
        $newArr = [];
        foreach ($dataArr as $k => $val) {    //数据根据日期分组
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章