常用操作方法封裝(文件處理、數據處理),複製即用

文件壓縮

    /**
     * @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;
    }

 

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