1
|
|
|
<?php |
2
|
|
|
namespace Ellumilel\Helpers; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class PHPExcelHelper |
6
|
|
|
* @package Ellumilel\Helpers |
7
|
|
|
*/ |
8
|
|
|
class ExcelHelper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @link http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx |
12
|
|
|
* XFE1048577 |
13
|
|
|
*/ |
14
|
|
|
const EXCEL_MAX_ROW = 1048576; |
15
|
|
|
const EXCEL_MAX_RANGE = 2147483647; |
16
|
|
|
const EXCEL_MAX_COL = 16384; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $dateInput |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public static function convertDateTime($dateInput) |
24
|
|
|
{ |
25
|
|
|
$epoch = 1900; |
26
|
|
|
$norm = 300; |
27
|
|
|
$year = $month = $day = $offset = $seconds = 0; |
|
|
|
|
28
|
|
|
$dateTime = $dateInput; |
29
|
|
|
if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $dateTime, $matches)) { |
30
|
|
|
$year = $matches[1]; |
31
|
|
|
$month = $matches[2]; |
32
|
|
|
$day = $matches[3]; |
33
|
|
|
} |
34
|
|
|
$seconds = self::getSeconds($dateTime); |
35
|
|
|
if ("$year-$month-$day" == '1899-12-31' || "$year-$month-$day" == '1900-01-00') { |
36
|
|
|
return $seconds; |
37
|
|
|
} |
38
|
|
|
if ("$year-$month-$day" == '1900-02-29') { |
39
|
|
|
return 60 + $seconds; |
40
|
|
|
} |
41
|
|
|
$range = $year - $epoch; |
42
|
|
|
// check leapDay |
43
|
|
|
$leap = (new \DateTime($dateInput))->format('L'); |
44
|
|
|
$mDays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
45
|
|
|
if (($year < $epoch || $year > 9999) || ($month < 1 || $month > 12) || $day < 1 || $day > $mDays[$month - 1]) { |
46
|
|
|
return 0; |
47
|
|
|
} |
48
|
|
|
$days = $day + ($range * 365) + array_sum(array_slice($mDays, 0, $month - 1)); |
49
|
|
|
$days += intval(($range) / 4) - intval(($range + $offset) / 100); |
50
|
|
|
$days += intval(($range + $offset + $norm) / 400) - intval($leap); |
51
|
|
|
if ($days > 59) { |
52
|
|
|
$days++; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $days + $seconds; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $val |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public static function xmlspecialchars($val) |
64
|
|
|
{ |
65
|
|
|
return str_replace("'", "'", htmlspecialchars($val)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $rowNumber |
70
|
|
|
* @param int $columnNumber |
71
|
|
|
* |
72
|
|
|
* @return string Cell label/coordinates (A1, C3, AA42) |
73
|
|
|
*/ |
74
|
|
|
public static function xlsCell($rowNumber, $columnNumber) |
75
|
|
|
{ |
76
|
|
|
$n = $columnNumber; |
77
|
|
|
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) { |
78
|
|
|
$r = chr($n % 26 + 0x41).$r; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $r.($rowNumber + 1); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @link https://msdn.microsoft.com/ru-RU/library/aa365247%28VS.85%29.aspx |
86
|
|
|
* |
87
|
|
|
* @param string $filename |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public static function checkFilename($filename) |
92
|
|
|
{ |
93
|
|
|
$invalidCharacter = array_merge( |
94
|
|
|
array_map('chr', range(0, 31)), |
95
|
|
|
['<', '>', '?', '"', ':', '|', '\\', '/', '*', '&'] |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return str_replace($invalidCharacter, '', $filename); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @todo check escaping |
103
|
|
|
* |
104
|
|
|
* @param string $cellFormat |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public static function escapeCellFormat($cellFormat) |
109
|
|
|
{ |
110
|
|
|
$ignoreUntil = ''; |
111
|
|
|
$escaped = ''; |
112
|
|
|
for ($i = 0, $ix = strlen($cellFormat); $i < $ix; $i++) { |
113
|
|
|
$c = $cellFormat[$i]; |
114
|
|
|
if ($ignoreUntil == '' && $c == '[') { |
115
|
|
|
$ignoreUntil = ']'; |
116
|
|
|
} else { |
117
|
|
|
if ($ignoreUntil == '' && $c == '"') { |
118
|
|
|
$ignoreUntil = '"'; |
119
|
|
|
} else { |
120
|
|
|
if ($ignoreUntil == $c) { |
121
|
|
|
$ignoreUntil = ''; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
if ($ignoreUntil == '' && |
126
|
|
|
($c == ' ' || $c == '-' || $c == '(' || $c == ')') && |
127
|
|
|
($i == 0 || $cellFormat[$i - 1] != '_') |
128
|
|
|
) { |
129
|
|
|
$escaped .= "\\".$c; |
130
|
|
|
} else { |
131
|
|
|
$escaped .= $c; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $escaped; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $dateTime |
140
|
|
|
* |
141
|
|
|
* @return float|int |
142
|
|
|
*/ |
143
|
|
|
private function getSeconds($dateTime) |
144
|
|
|
{ |
145
|
|
|
$seconds = 0; |
146
|
|
|
|
147
|
|
|
if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $dateTime, $matches)) { |
148
|
|
|
$seconds = ($matches[1] * 60 * 60 + $matches[2] * 60 + $matches[3]) / 86400; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $seconds; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.