1 | <?php |
||
2 | namespace tinymeng\spreadsheet\Connector; |
||
3 | |||
4 | use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
||
5 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
||
6 | use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
||
7 | |||
8 | /** |
||
9 | * Gateway |
||
10 | */ |
||
11 | abstract class Gateway implements GatewayInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var Spreadsheet |
||
16 | */ |
||
17 | public $spreadSheet; |
||
18 | /** |
||
19 | * @var Worksheet |
||
20 | */ |
||
21 | public $workSheet; |
||
22 | |||
23 | /** |
||
24 | * 是否格式化内容 |
||
25 | * @var string |
||
26 | */ |
||
27 | public $format = true; |
||
28 | /** |
||
29 | * 是否格式化内容 |
||
30 | * @var string |
||
31 | */ |
||
32 | public $format_date = 'Y-m-d H:i:s'; |
||
33 | |||
34 | /** |
||
35 | * @var |
||
36 | */ |
||
37 | public $params; |
||
38 | |||
39 | /** |
||
40 | * 数字转英文列 |
||
41 | * @param $columnIndex |
||
42 | * @return string |
||
43 | * @author: Tinymeng <[email protected]> |
||
44 | * @time: 2022/4/24 17:35 |
||
45 | */ |
||
46 | protected function cellName($columnIndex){ |
||
47 | $columnIndex =(int)$columnIndex+1; |
||
48 | static $indexCache = []; |
||
49 | |||
50 | if (!isset($indexCache[$columnIndex])) { |
||
51 | $indexValue = $columnIndex; |
||
52 | $base26 = null; |
||
53 | do { |
||
54 | $characterValue = ($indexValue % 26) ?: 26; |
||
55 | $indexValue = ($indexValue - $characterValue) / 26; |
||
56 | $base26 = chr($characterValue + 64) . ($base26 ?: ''); |
||
57 | } while ($indexValue > 0); |
||
58 | $indexCache[$columnIndex] = $base26; |
||
59 | } |
||
60 | |||
61 | return $indexCache[$columnIndex]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * 格式化value |
||
66 | * @param string |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function formatValue($v){ |
||
70 | if($this->format === false) return $v; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
71 | if(is_numeric($v) && strlen($v)===10){ |
||
72 | $v = date($this->format_date,$v);//时间戳转时间格式 |
||
73 | } |
||
74 | return $v; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * 根据最后一列获取所有列数组 |
||
79 | * @param $lastCell |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function getCellName($lastCell){ |
||
83 | $cellName = array(); |
||
84 | for($i='A'; $i!=$lastCell; $i++) { |
||
85 | $cellName[] = $i; |
||
86 | } |
||
87 | $cellName[] = $i++; |
||
88 | return $cellName; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $path |
||
93 | * @param bool $verifyFile |
||
94 | * @param null $zip |
||
0 ignored issues
–
show
|
|||
95 | * @return array|string|string[] |
||
96 | * @throws PhpSpreadsheetException |
||
97 | */ |
||
98 | protected function verifyFile($path, $verifyFile = true, $zip = null){ |
||
99 | if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) { |
||
100 | // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979 |
||
101 | if (filter_var($path, FILTER_VALIDATE_URL)) { |
||
102 | $this->path = $path; |
||
0 ignored issues
–
show
|
|||
103 | // Implicit that it is a URL, rather store info than running check above on value in other places. |
||
104 | $this->isUrl = true; |
||
0 ignored issues
–
show
|
|||
105 | $imageContents = file_get_contents($path); |
||
106 | $filePath = tempnam(sys_get_temp_dir(), 'Drawing'); |
||
107 | if ($filePath) { |
||
108 | file_put_contents($filePath, $imageContents); |
||
109 | if (file_exists($filePath)) { |
||
110 | return $filePath; |
||
111 | } |
||
112 | } |
||
113 | } elseif (file_exists($path)) { |
||
114 | return $path; |
||
115 | } elseif ($zip instanceof ZipArchive) { |
||
0 ignored issues
–
show
|
|||
116 | $zipPath = explode('#', $path)[1]; |
||
117 | if ($zip->locateName($zipPath) !== false) { |
||
118 | return $path; |
||
119 | } |
||
120 | } else { |
||
121 | throw new PhpSpreadsheetException("File $path not found!"); |
||
122 | } |
||
123 | } |
||
124 | return $path; |
||
125 | } |
||
126 | } |
||
127 |