1 | <?php |
||
12 | class CellValueFormatter |
||
13 | { |
||
14 | /** Definition of all possible cell types */ |
||
15 | const CELL_TYPE_INLINE_STRING = 'inlineStr'; |
||
16 | const CELL_TYPE_STR = 'str'; |
||
17 | const CELL_TYPE_SHARED_STRING = 's'; |
||
18 | const CELL_TYPE_BOOLEAN = 'b'; |
||
19 | const CELL_TYPE_NUMERIC = 'n'; |
||
20 | const CELL_TYPE_DATE = 'd'; |
||
21 | const CELL_TYPE_ERROR = 'e'; |
||
22 | |||
23 | /** Definition of XML nodes names used to parse data */ |
||
24 | const XML_NODE_VALUE = 'v'; |
||
25 | const XML_NODE_INLINE_STRING_VALUE = 't'; |
||
26 | |||
27 | /** Definition of XML attributes used to parse data */ |
||
28 | const XML_ATTRIBUTE_TYPE = 't'; |
||
29 | const XML_ATTRIBUTE_STYLE_ID = 's'; |
||
30 | |||
31 | /** Constants used for date formatting */ |
||
32 | const NUM_SECONDS_IN_ONE_DAY = 86400; |
||
33 | const NUM_SECONDS_IN_ONE_HOUR = 3600; |
||
34 | const NUM_SECONDS_IN_ONE_MINUTE = 60; |
||
35 | |||
36 | /** |
||
37 | * February 29th, 1900 is NOT a leap year but Excel thinks it is... |
||
38 | * @see https://en.wikipedia.org/wiki/Year_1900_problem#Microsoft_Excel |
||
39 | */ |
||
40 | const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60; |
||
41 | |||
42 | /** @var SharedStringsManager Manages shared strings */ |
||
43 | protected $sharedStringsManager; |
||
44 | |||
45 | /** @var StyleManager Manages styles */ |
||
46 | protected $styleManager; |
||
47 | |||
48 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
49 | protected $shouldFormatDates; |
||
50 | |||
51 | /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ |
||
52 | protected $escaper; |
||
53 | |||
54 | /** |
||
55 | * @param SharedStringsManager $sharedStringsManager Manages shared strings |
||
56 | * @param StyleManager $styleManager Manages styles |
||
57 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
58 | * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data |
||
59 | */ |
||
60 | 61 | public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper) |
|
61 | { |
||
62 | 61 | $this->sharedStringsManager = $sharedStringsManager; |
|
63 | 61 | $this->styleManager = $styleManager; |
|
64 | 61 | $this->shouldFormatDates = $shouldFormatDates; |
|
65 | 61 | $this->escaper = $escaper; |
|
66 | 61 | } |
|
67 | |||
68 | /** |
||
69 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
70 | * |
||
71 | * @param \DOMNode $node |
||
72 | * @return string|int|float|bool|\DateTime|null The value associated with the cell (null when the cell has an error) |
||
73 | */ |
||
74 | 41 | public function extractAndFormatNodeValue($node) |
|
102 | |||
103 | /** |
||
104 | * Returns the cell's string value from a node's nested value node |
||
105 | * |
||
106 | * @param \DOMNode $node |
||
107 | * @return string The value associated with the cell |
||
108 | */ |
||
109 | 41 | protected function getVNodeValue($node) |
|
117 | |||
118 | /** |
||
119 | * Returns the cell String value where string is inline. |
||
120 | * |
||
121 | * @param \DOMNode $node |
||
122 | * @return string The value associated with the cell (null when the cell has an error) |
||
123 | */ |
||
124 | 11 | protected function formatInlineStringCellValue($node) |
|
133 | |||
134 | /** |
||
135 | * Returns the cell String value from shared-strings file using nodeValue index. |
||
136 | * |
||
137 | * @param string $nodeValue |
||
138 | * @return string The value associated with the cell (null when the cell has an error) |
||
139 | */ |
||
140 | 18 | protected function formatSharedStringCellValue($nodeValue) |
|
150 | |||
151 | /** |
||
152 | * Returns the cell String value, where string is stored in value node. |
||
153 | * |
||
154 | * @param string $nodeValue |
||
155 | * @return string The value associated with the cell (null when the cell has an error) |
||
156 | */ |
||
157 | 1 | protected function formatStrCellValue($nodeValue) |
|
164 | |||
165 | /** |
||
166 | * Returns the cell Numeric value from string of nodeValue. |
||
167 | * The value can also represent a timestamp and a DateTime will be returned. |
||
168 | * |
||
169 | * @param string $nodeValue |
||
170 | * @param int $cellStyleId 0 being the default style |
||
171 | * @return int|float|\DateTime|null The value associated with the cell |
||
172 | */ |
||
173 | 34 | protected function formatNumericCellValue($nodeValue, $cellStyleId) |
|
189 | |||
190 | /** |
||
191 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
192 | * NOTE: The timestamp is a float representing the number of days since January 1st, 1900. |
||
193 | * NOTE: The timestamp can also represent a time, if it is a value between 0 and 1. |
||
194 | * |
||
195 | * @param float $nodeValue |
||
196 | * @param int $cellStyleId 0 being the default style |
||
197 | * @return \DateTime|null The value associated with the cell or NULL if invalid date value |
||
198 | */ |
||
199 | 16 | protected function formatExcelTimestampValue($nodeValue, $cellStyleId) |
|
219 | |||
220 | /** |
||
221 | * Returns a cell's PHP DateTime value, associated to the given timestamp. |
||
222 | * Only the time value matters. The date part is set to Jan 1st, 1900 (base Excel date). |
||
223 | * |
||
224 | * @param float $nodeValue |
||
225 | * @param int $cellStyleId 0 being the default style |
||
226 | * @return \DateTime|string The value associated with the cell |
||
227 | */ |
||
228 | 8 | protected function formatExcelTimestampValueAsTimeValue($nodeValue, $cellStyleId) |
|
249 | |||
250 | /** |
||
251 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
252 | * NOTE: The timestamp is a float representing the number of days since January 1st, 1900. |
||
253 | * |
||
254 | * @param float $nodeValue |
||
255 | * @param int $cellStyleId 0 being the default style |
||
256 | * @return \DateTime|string|null The value associated with the cell or NULL if invalid date value |
||
257 | */ |
||
258 | 9 | protected function formatExcelTimestampValueAsDateValue($nodeValue, $cellStyleId) |
|
283 | |||
284 | /** |
||
285 | * Returns the cell Boolean value from a specific node's Value. |
||
286 | * |
||
287 | * @param string $nodeValue |
||
288 | * @return bool The value associated with the cell |
||
289 | */ |
||
290 | 1 | protected function formatBooleanCellValue($nodeValue) |
|
294 | |||
295 | /** |
||
296 | * Returns a cell's PHP Date value, associated to the given stored nodeValue. |
||
297 | * @see ECMA-376 Part 1 - §18.17.4 |
||
298 | * |
||
299 | * @param string $nodeValue ISO 8601 Date string |
||
300 | * @return \DateTime|string|null The value associated with the cell or NULL if invalid date value |
||
301 | */ |
||
302 | 2 | protected function formatDateCellValue($nodeValue) |
|
313 | } |
||
314 |