WorkSheetHelper   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatValue() 0 9 4
A cellName() 0 17 5
B verifyFile() 0 25 9
1
<?php
2
/**
3
 * @name: WorkSheetHelper
4
 * @author: JiaMeng <[email protected]>
5
 * @file: WorkSheetHelper.php
6
 * @Date: 2025/01/XX
7
 * @description: Excel工作表辅助工具类
8
 */
9
namespace tinymeng\spreadsheet\Util;
10
11
class WorkSheetHelper
12
{
13
    /**
14
     * 数字转英文列
15
     * @param int $columnIndex 列索引(从0开始)
16
     * @return string 列字母(如 A, B, C, AA, AB...)
17
     */
18
    public static function cellName(int $columnIndex): string
19
    {
20
        $columnIndex = (int)$columnIndex + 1;
21
        static $indexCache = [];
22
23
        if (!isset($indexCache[$columnIndex])) {
24
            $indexValue = $columnIndex;
25
            $base26 = null;
26
            do {
27
                $characterValue = ($indexValue % 26) ?: 26;
28
                $indexValue = ($indexValue - $characterValue) / 26;
29
                $base26 = chr($characterValue + 64) . ($base26 ?: '');
30
            } while ($indexValue > 0);
31
            $indexCache[$columnIndex] = $base26;
32
        }
33
34
        return $indexCache[$columnIndex];
35
    }
36
37
    /**
38
     * 格式化值
39
     * @param mixed $value 原始值
40
     * @param bool $format 是否格式化
41
     * @param string $formatDate 日期格式
42
     * @return mixed 格式化后的值
43
     */
44
    public static function formatValue($value, bool $format = true, string $formatDate = 'Y-m-d H:i:s')
45
    {
46
        if ($format === false) {
47
            return $value;
48
        }
49
        if (is_numeric($value) && strlen($value) === 10) {
50
            $value = date($formatDate, $value); // 时间戳转时间格式
51
        }
52
        return $value;
53
    }
54
55
    /**
56
     * 验证并处理文件路径
57
     * @param string $path 文件路径
58
     * @param bool $verifyFile 是否验证文件
59
     * @param \ZipArchive|null $zip ZIP归档对象
60
     * @return string 验证后的文件路径
61
     * @throws \PhpOffice\PhpSpreadsheet\Exception
62
     */
63
    public static function verifyFile(string $path, bool $verifyFile = true, $zip = null): string
64
    {
65
        if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) {
66
            // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
67
            if (filter_var($path, FILTER_VALIDATE_URL)) {
68
                $imageContents = file_get_contents($path);
69
                $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
70
                if ($filePath) {
71
                    file_put_contents($filePath, $imageContents);
72
                    if (file_exists($filePath)) {
73
                        return $filePath;
74
                    }
75
                }
76
            } elseif (file_exists($path)) {
77
                return $path;
78
            } elseif ($zip instanceof \ZipArchive) {
79
                $zipPath = explode('#', $path)[1];
80
                if ($zip->locateName($zipPath) !== false) {
81
                    return $path;
82
                }
83
            } else {
84
                throw new \PhpOffice\PhpSpreadsheet\Exception("File $path not found!");
85
            }
86
        }
87
        return $path;
88
    }
89
}
90
91