1 | <?php |
||
11 | class CellValueFormatter |
||
12 | { |
||
13 | /** Definition of all possible cell types */ |
||
14 | const CELL_TYPE_INLINE_STRING = 'inlineStr'; |
||
15 | const CELL_TYPE_STR = 'str'; |
||
16 | const CELL_TYPE_SHARED_STRING = 's'; |
||
17 | const CELL_TYPE_BOOLEAN = 'b'; |
||
18 | const CELL_TYPE_NUMERIC = 'n'; |
||
19 | const CELL_TYPE_DATE = 'd'; |
||
20 | const CELL_TYPE_ERROR = 'e'; |
||
21 | |||
22 | /** Definition of XML nodes names used to parse data */ |
||
23 | const XML_NODE_VALUE = 'v'; |
||
24 | const XML_NODE_INLINE_STRING_VALUE = 't'; |
||
25 | |||
26 | /** Definition of XML attributes used to parse data */ |
||
27 | const XML_ATTRIBUTE_TYPE = 't'; |
||
28 | const XML_ATTRIBUTE_STYLE_ID = 's'; |
||
29 | |||
30 | /** Constants used for date formatting */ |
||
31 | const NUM_SECONDS_IN_ONE_DAY = 86400; |
||
32 | const NUM_SECONDS_IN_ONE_HOUR = 3600; |
||
33 | const NUM_SECONDS_IN_ONE_MINUTE = 60; |
||
34 | |||
35 | /** |
||
36 | * February 29th, 1900 is NOT a leap year but Excel thinks it is... |
||
37 | * @see https://en.wikipedia.org/wiki/Year_1900_problem#Microsoft_Excel |
||
38 | */ |
||
39 | const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60; |
||
40 | |||
41 | /** @var SharedStringsHelper Helper to work with shared strings */ |
||
42 | protected $sharedStringsHelper; |
||
43 | |||
44 | /** @var StyleHelper Helper to work with styles */ |
||
45 | protected $styleHelper; |
||
46 | |||
47 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
48 | protected $shouldFormatDates; |
||
49 | |||
50 | /** @var \Box\Spout\Common\Escaper\XLSX Used to unescape XML data */ |
||
51 | protected $escaper; |
||
52 | |||
53 | /** |
||
54 | * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings |
||
55 | * @param StyleHelper $styleHelper Helper to work with styles |
||
56 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
57 | */ |
||
58 | public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates) |
||
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 | 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 | protected function getVNodeValue($node) |
||
116 | |||
117 | /** |
||
118 | * Returns the cell String value where string is inline. |
||
119 | * |
||
120 | * @param \DOMNode $node |
||
121 | * @return string The value associated with the cell (null when the cell has an error) |
||
122 | */ |
||
123 | protected function formatInlineStringCellValue($node) |
||
131 | |||
132 | /** |
||
133 | * Returns the cell String value from shared-strings file using nodeValue index. |
||
134 | * |
||
135 | * @param string $nodeValue |
||
136 | * @return string The value associated with the cell (null when the cell has an error) |
||
137 | */ |
||
138 | protected function formatSharedStringCellValue($nodeValue) |
||
147 | |||
148 | /** |
||
149 | * Returns the cell String value, where string is stored in value node. |
||
150 | * |
||
151 | * @param string $nodeValue |
||
152 | * @return string The value associated with the cell (null when the cell has an error) |
||
153 | */ |
||
154 | protected function formatStrCellValue($nodeValue) |
||
160 | |||
161 | /** |
||
162 | * Returns the cell Numeric value from string of nodeValue. |
||
163 | * The value can also represent a timestamp and a DateTime will be returned. |
||
164 | * |
||
165 | * @param string $nodeValue |
||
166 | * @param int $cellStyleId 0 being the default style |
||
167 | * @return int|float|\DateTime|null The value associated with the cell |
||
168 | */ |
||
169 | protected function formatNumericCellValue($nodeValue, $cellStyleId) |
||
182 | |||
183 | /** |
||
184 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
185 | * NOTE: The timestamp is a float representing the number of days since January 1st, 1900. |
||
186 | * NOTE: The timestamp can also represent a time, if it is a value between 0 and 1. |
||
187 | * |
||
188 | * @param float $nodeValue |
||
189 | * @param int $cellStyleId 0 being the default style |
||
190 | * @return \DateTime|null The value associated with the cell or NULL if invalid date value |
||
191 | */ |
||
192 | protected function formatExcelTimestampValue($nodeValue, $cellStyleId) |
||
210 | |||
211 | /** |
||
212 | * Returns a cell's PHP DateTime value, associated to the given timestamp. |
||
213 | * Only the time value matters. The date part is set to Jan 1st, 1900 (base Excel date). |
||
214 | * |
||
215 | * @param float $nodeValue |
||
216 | * @param int $cellStyleId 0 being the default style |
||
217 | * @return \DateTime|string The value associated with the cell |
||
218 | */ |
||
219 | protected function formatExcelTimestampValueAsTimeValue($nodeValue, $cellStyleId) |
||
238 | |||
239 | /** |
||
240 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
241 | * NOTE: The timestamp is a float representing the number of days since January 1st, 1900. |
||
242 | * |
||
243 | * @param float $nodeValue |
||
244 | * @param int $cellStyleId 0 being the default style |
||
245 | * @return \DateTime|string|null The value associated with the cell or NULL if invalid date value |
||
246 | */ |
||
247 | protected function formatExcelTimestampValueAsDateValue($nodeValue, $cellStyleId) |
||
270 | |||
271 | /** |
||
272 | * Returns the cell Boolean value from a specific node's Value. |
||
273 | * |
||
274 | * @param string $nodeValue |
||
275 | * @return bool The value associated with the cell |
||
276 | */ |
||
277 | protected function formatBooleanCellValue($nodeValue) |
||
283 | |||
284 | /** |
||
285 | * Returns a cell's PHP Date value, associated to the given stored nodeValue. |
||
286 | * |
||
287 | * @param string $nodeValue |
||
288 | * @return \DateTime|null The value associated with the cell or NULL if invalid date value |
||
289 | */ |
||
290 | protected function formatDateCellValue($nodeValue) |
||
300 | } |
||
301 |