1 | <?php |
||
13 | class CellValueFormatter |
||
14 | { |
||
15 | /** Definition of all possible cell types */ |
||
16 | const CELL_TYPE_INLINE_STRING = 'inlineStr'; |
||
17 | const CELL_TYPE_STR = 'str'; |
||
18 | const CELL_TYPE_SHARED_STRING = 's'; |
||
19 | const CELL_TYPE_BOOLEAN = 'b'; |
||
20 | const CELL_TYPE_NUMERIC = 'n'; |
||
21 | const CELL_TYPE_DATE = 'd'; |
||
22 | const CELL_TYPE_ERROR = 'e'; |
||
23 | |||
24 | /** Definition of XML nodes names used to parse data */ |
||
25 | const XML_NODE_VALUE = 'v'; |
||
26 | const XML_NODE_INLINE_STRING_VALUE = 't'; |
||
27 | |||
28 | /** Definition of XML attributes used to parse data */ |
||
29 | const XML_ATTRIBUTE_TYPE = 't'; |
||
30 | const XML_ATTRIBUTE_STYLE_ID = 's'; |
||
31 | |||
32 | /** Constants used for date formatting */ |
||
33 | const NUM_SECONDS_IN_ONE_DAY = 86400; |
||
34 | const NUM_SECONDS_IN_ONE_HOUR = 3600; |
||
35 | const NUM_SECONDS_IN_ONE_MINUTE = 60; |
||
36 | |||
37 | /** |
||
38 | * February 29th, 1900 is NOT a leap year but Excel thinks it is... |
||
39 | * @see https://en.wikipedia.org/wiki/Year_1900_problem#Microsoft_Excel |
||
40 | */ |
||
41 | const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60; |
||
42 | |||
43 | /** @var SharedStringsManager Manages shared strings */ |
||
44 | protected $sharedStringsManager; |
||
45 | |||
46 | /** @var StyleManager Manages styles */ |
||
47 | protected $styleManager; |
||
48 | |||
49 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
50 | protected $shouldFormatDates; |
||
51 | |||
52 | /** @var bool Whether date/time values should use a calendar starting in 1904 instead of 1900 */ |
||
53 | protected $shouldUse1904Dates; |
||
54 | |||
55 | /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ |
||
56 | protected $escaper; |
||
57 | |||
58 | /** |
||
59 | * @param SharedStringsManager $sharedStringsManager Manages shared strings |
||
60 | * @param StyleManager $styleManager Manages styles |
||
61 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
62 | * @param bool $shouldUse1904Dates Whether date/time values should use a calendar starting in 1904 instead of 1900 |
||
63 | * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data |
||
64 | */ |
||
65 | 83 | public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $shouldUse1904Dates, $escaper) |
|
73 | |||
74 | /** |
||
75 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
76 | * |
||
77 | * @param \DOMNode $node |
||
78 | * @throws InvalidValueException If the value is not valid |
||
79 | * @return string|int|float|bool|\DateTime The value associated with the cell |
||
80 | */ |
||
81 | 62 | public function extractAndFormatNodeValue($node) |
|
109 | |||
110 | /** |
||
111 | * Returns the cell's string value from a node's nested value node |
||
112 | * |
||
113 | * @param \DOMNode $node |
||
114 | * @return string The value associated with the cell |
||
115 | */ |
||
116 | 62 | protected function getVNodeValue($node) |
|
124 | |||
125 | /** |
||
126 | * Returns the cell String value where string is inline. |
||
127 | * |
||
128 | * @param \DOMNode $node |
||
129 | * @return string The value associated with the cell |
||
130 | */ |
||
131 | 11 | protected function formatInlineStringCellValue($node) |
|
140 | |||
141 | /** |
||
142 | * Returns the cell String value from shared-strings file using nodeValue index. |
||
143 | * |
||
144 | * @param string $nodeValue |
||
145 | * @return string The value associated with the cell |
||
146 | */ |
||
147 | 21 | protected function formatSharedStringCellValue($nodeValue) |
|
157 | |||
158 | /** |
||
159 | * Returns the cell String value, where string is stored in value node. |
||
160 | * |
||
161 | * @param string $nodeValue |
||
162 | * @return string The value associated with the cell |
||
163 | */ |
||
164 | 1 | protected function formatStrCellValue($nodeValue) |
|
171 | |||
172 | /** |
||
173 | * Returns the cell Numeric value from string of nodeValue. |
||
174 | * The value can also represent a timestamp and a DateTime will be returned. |
||
175 | * |
||
176 | * @param string $nodeValue |
||
177 | * @param int $cellStyleId 0 being the default style |
||
178 | * @return int|float|\DateTime The value associated with the cell |
||
179 | */ |
||
180 | 52 | protected function formatNumericCellValue($nodeValue, $cellStyleId) |
|
196 | |||
197 | /** |
||
198 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
199 | * NOTE: The timestamp is a float representing the number of days since the base Excel date: |
||
200 | * Dec 30th 1899, 1900 or Jan 1st, 1904, depending on the Workbook setting. |
||
201 | * NOTE: The timestamp can also represent a time, if it is a value between 0 and 1. |
||
202 | * |
||
203 | * @see ECMA-376 Part 1 - §18.17.4 |
||
204 | * |
||
205 | * @param float $nodeValue |
||
206 | * @param int $cellStyleId 0 being the default style |
||
207 | * @throws InvalidValueException If the value is not a valid timestamp |
||
208 | * @return \DateTime The value associated with the cell |
||
209 | */ |
||
210 | 34 | protected function formatExcelTimestampValue($nodeValue, $cellStyleId) |
|
220 | |||
221 | /** |
||
222 | * Returns whether the given timestamp is supported by SpreadsheetML |
||
223 | * @see ECMA-376 Part 1 - §18.17.4 - this specifies the timestamp boundaries. |
||
224 | * |
||
225 | * @param float $timestampValue |
||
226 | * @return bool |
||
227 | */ |
||
228 | 34 | protected function isValidTimestampValue($timestampValue) |
|
236 | |||
237 | /** |
||
238 | * Returns a cell's PHP DateTime value, associated to the given timestamp. |
||
239 | * Only the time value matters. The date part is set to the base Excel date: |
||
240 | * Dec 30th 1899, 1900 or Jan 1st, 1904, depending on the Workbook setting. |
||
241 | * |
||
242 | * @param float $nodeValue |
||
243 | * @param int $cellStyleId 0 being the default style |
||
244 | * @return \DateTime|string The value associated with the cell |
||
245 | */ |
||
246 | 30 | protected function formatExcelTimestampValueAsDateTimeValue($nodeValue, $cellStyleId) |
|
268 | |||
269 | /** |
||
270 | * Returns the cell Boolean value from a specific node's Value. |
||
271 | * |
||
272 | * @param string $nodeValue |
||
273 | * @return bool The value associated with the cell |
||
274 | */ |
||
275 | 1 | protected function formatBooleanCellValue($nodeValue) |
|
279 | |||
280 | /** |
||
281 | * Returns a cell's PHP Date value, associated to the given stored nodeValue. |
||
282 | * @see ECMA-376 Part 1 - §18.17.4 |
||
283 | * |
||
284 | * @param string $nodeValue ISO 8601 Date string |
||
285 | * @throws InvalidValueException If the value is not a valid date |
||
286 | * @return \DateTime|string The value associated with the cell |
||
287 | */ |
||
288 | 2 | protected function formatDateCellValue($nodeValue) |
|
299 | } |
||
300 |