Gateway::verifyFile()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 27
rs 8.0555
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
The condition $this->format === false is always false.
Loading history...
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
Documentation Bug introduced by
Are you sure the doc-type for parameter $zip is correct as it would always require null to be passed?
Loading history...
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
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
Bug Best Practice introduced by
The property isUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
Bug introduced by
The type tinymeng\spreadsheet\Connector\ZipArchive was not found. Did you mean ZipArchive? If so, make sure to prefix the type with \.
Loading history...
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