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 | |||
35 | /** @var SharedStringsManager Manages shared strings */ |
||
36 | protected $sharedStringsManager; |
||
37 | |||
38 | /** @var StyleManager Manages styles */ |
||
39 | protected $styleManager; |
||
40 | |||
41 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
42 | protected $shouldFormatDates; |
||
43 | |||
44 | /** @var bool Whether date/time values should use a calendar starting in 1904 instead of 1900 */ |
||
45 | protected $shouldUse1904Dates; |
||
46 | |||
47 | /** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to unescape XML data */ |
||
48 | protected $escaper; |
||
49 | |||
50 | /** |
||
51 | * @param SharedStringsManager $sharedStringsManager Manages shared strings |
||
52 | * @param StyleManager $styleManager Manages styles |
||
53 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
54 | * @param bool $shouldUse1904Dates Whether date/time values should use a calendar starting in 1904 instead of 1900 |
||
55 | * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data |
||
56 | */ |
||
57 | 86 | public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $shouldUse1904Dates, $escaper) |
|
65 | |||
66 | /** |
||
67 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
68 | * |
||
69 | * @param \DOMNode $node |
||
70 | * @throws InvalidValueException If the value is not valid |
||
71 | * @return string|int|float|bool|\DateTime The value associated with the cell |
||
72 | */ |
||
73 | 65 | public function extractAndFormatNodeValue($node) |
|
101 | |||
102 | /** |
||
103 | * Returns the cell's string value from a node's nested value node |
||
104 | * |
||
105 | * @param \DOMNode $node |
||
106 | * @return string The value associated with the cell |
||
107 | */ |
||
108 | 65 | 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 |
||
122 | */ |
||
123 | 13 | protected function formatInlineStringCellValue($node) |
|
137 | |||
138 | /** |
||
139 | * Returns the cell String value from shared-strings file using nodeValue index. |
||
140 | * |
||
141 | * @param string $nodeValue |
||
142 | * @return string The value associated with the cell |
||
143 | */ |
||
144 | 22 | protected function formatSharedStringCellValue($nodeValue) |
|
154 | |||
155 | /** |
||
156 | * Returns the cell String value, where string is stored in value node. |
||
157 | * |
||
158 | * @param string $nodeValue |
||
159 | * @return string The value associated with the cell |
||
160 | */ |
||
161 | 1 | protected function formatStrCellValue($nodeValue) |
|
168 | |||
169 | /** |
||
170 | * Returns the cell Numeric value from string of nodeValue. |
||
171 | * The value can also represent a timestamp and a DateTime will be returned. |
||
172 | * |
||
173 | * @param string $nodeValue |
||
174 | * @param int $cellStyleId 0 being the default style |
||
175 | * @return int|float|\DateTime The value associated with the cell |
||
176 | */ |
||
177 | 54 | protected function formatNumericCellValue($nodeValue, $cellStyleId) |
|
193 | |||
194 | /** |
||
195 | * Returns a cell's PHP Date value, associated to the given timestamp. |
||
196 | * NOTE: The timestamp is a float representing the number of days since the base Excel date: |
||
197 | * Dec 30th 1899, 1900 or Jan 1st, 1904, depending on the Workbook setting. |
||
198 | * NOTE: The timestamp can also represent a time, if it is a value between 0 and 1. |
||
199 | * |
||
200 | * @see ECMA-376 Part 1 - §18.17.4 |
||
201 | * |
||
202 | * @param float $nodeValue |
||
203 | * @param int $cellStyleId 0 being the default style |
||
204 | * @throws InvalidValueException If the value is not a valid timestamp |
||
205 | * @return \DateTime The value associated with the cell |
||
206 | */ |
||
207 | 34 | protected function formatExcelTimestampValue($nodeValue, $cellStyleId) |
|
217 | |||
218 | /** |
||
219 | * Returns whether the given timestamp is supported by SpreadsheetML |
||
220 | * @see ECMA-376 Part 1 - §18.17.4 - this specifies the timestamp boundaries. |
||
221 | * |
||
222 | * @param float $timestampValue |
||
223 | * @return bool |
||
224 | */ |
||
225 | 34 | protected function isValidTimestampValue($timestampValue) |
|
233 | |||
234 | /** |
||
235 | * Returns a cell's PHP DateTime value, associated to the given timestamp. |
||
236 | * Only the time value matters. The date part is set to the base Excel date: |
||
237 | * Dec 30th 1899, 1900 or Jan 1st, 1904, depending on the Workbook setting. |
||
238 | * |
||
239 | * @param float $nodeValue |
||
240 | * @param int $cellStyleId 0 being the default style |
||
241 | * @return \DateTime|string The value associated with the cell |
||
242 | */ |
||
243 | 30 | protected function formatExcelTimestampValueAsDateTimeValue($nodeValue, $cellStyleId) |
|
265 | |||
266 | /** |
||
267 | * Returns the cell Boolean value from a specific node's Value. |
||
268 | * |
||
269 | * @param string $nodeValue |
||
270 | * @return bool The value associated with the cell |
||
271 | */ |
||
272 | 1 | protected function formatBooleanCellValue($nodeValue) |
|
276 | |||
277 | /** |
||
278 | * Returns a cell's PHP Date value, associated to the given stored nodeValue. |
||
279 | * @see ECMA-376 Part 1 - §18.17.4 |
||
280 | * |
||
281 | * @param string $nodeValue ISO 8601 Date string |
||
282 | * @throws InvalidValueException If the value is not a valid date |
||
283 | * @return \DateTime|string The value associated with the cell |
||
284 | */ |
||
285 | 3 | protected function formatDateCellValue($nodeValue) |
|
296 | } |
||
297 |