1 | <?php |
||
11 | class CellValueFormatter |
||
12 | { |
||
13 | /** Definition of all possible cell types */ |
||
14 | const CELL_TYPE_STRING = 'string'; |
||
15 | const CELL_TYPE_FLOAT = 'float'; |
||
16 | const CELL_TYPE_BOOLEAN = 'boolean'; |
||
17 | const CELL_TYPE_DATE = 'date'; |
||
18 | const CELL_TYPE_TIME = 'time'; |
||
19 | const CELL_TYPE_CURRENCY = 'currency'; |
||
20 | const CELL_TYPE_PERCENTAGE = 'percentage'; |
||
21 | const CELL_TYPE_VOID = 'void'; |
||
22 | |||
23 | /** Definition of XML nodes names used to parse data */ |
||
24 | const XML_NODE_P = 'p'; |
||
25 | const XML_NODE_TEXT_A = 'text:a'; |
||
26 | const XML_NODE_TEXT_SPAN = 'text:span'; |
||
27 | const XML_NODE_TEXT_S = 'text:s'; |
||
28 | const XML_NODE_TEXT_TAB = 'text:tab'; |
||
29 | const XML_NODE_TEXT_LINE_BREAK = 'text:line-break'; |
||
30 | |||
31 | /** Definition of XML attributes used to parse data */ |
||
32 | const XML_ATTRIBUTE_TYPE = 'office:value-type'; |
||
33 | const XML_ATTRIBUTE_VALUE = 'office:value'; |
||
34 | const XML_ATTRIBUTE_BOOLEAN_VALUE = 'office:boolean-value'; |
||
35 | const XML_ATTRIBUTE_DATE_VALUE = 'office:date-value'; |
||
36 | const XML_ATTRIBUTE_TIME_VALUE = 'office:time-value'; |
||
37 | const XML_ATTRIBUTE_CURRENCY = 'office:currency'; |
||
38 | const XML_ATTRIBUTE_C = 'text:c'; |
||
39 | |||
40 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
41 | protected $shouldFormatDates; |
||
42 | |||
43 | /** @var \Box\Spout\Common\Helper\Escaper\ODS Used to unescape XML data */ |
||
44 | protected $escaper; |
||
45 | |||
46 | /** @var array List of XML nodes representing whitespaces and their corresponding value */ |
||
47 | private static $WHITESPACE_XML_NODES = [ |
||
48 | self::XML_NODE_TEXT_S => ' ', |
||
49 | self::XML_NODE_TEXT_TAB => "\t", |
||
50 | self::XML_NODE_TEXT_LINE_BREAK => "\n", |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
55 | * @param \Box\Spout\Common\Helper\Escaper\ODS $escaper Used to unescape XML data |
||
56 | */ |
||
57 | 29 | public function __construct($shouldFormatDates, $escaper) |
|
62 | |||
63 | /** |
||
64 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
65 | * @see http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13 |
||
66 | * |
||
67 | * @param \DOMNode $node |
||
68 | * @throws InvalidValueException If the node value is not valid |
||
69 | * @return string|int|float|bool|\DateTime|\DateInterval The value associated with the cell, empty string if cell's type is void/undefined |
||
70 | */ |
||
71 | 26 | public function extractAndFormatNodeValue($node) |
|
95 | |||
96 | /** |
||
97 | * Returns the cell String value. |
||
98 | * |
||
99 | * @param \DOMNode $node |
||
100 | * @return string The value associated with the cell |
||
101 | */ |
||
102 | 23 | protected function formatStringCellValue($node) |
|
116 | |||
117 | /** |
||
118 | * @param $pNode |
||
119 | * @return string |
||
120 | */ |
||
121 | 23 | private function extractTextValueFromNode($pNode) |
|
137 | |||
138 | /** |
||
139 | * Returns whether the given node is a whitespace node. It must be one of these: |
||
140 | * - <text:s /> |
||
141 | * - <text:tab /> |
||
142 | * - <text:line-break /> |
||
143 | * |
||
144 | * @param string $nodeName |
||
145 | * @return bool |
||
146 | */ |
||
147 | 4 | private function isWhitespaceNode($nodeName) |
|
151 | |||
152 | /** |
||
153 | * The "<text:p>" node can contain the string value directly |
||
154 | * or contain child elements. In this case, whitespaces contain in |
||
155 | * the child elements should be replaced by their XML equivalent: |
||
156 | * - space => <text:s /> |
||
157 | * - tab => <text:tab /> |
||
158 | * - line break => <text:line-break /> |
||
159 | * |
||
160 | * @see https://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1415200_253892949 |
||
161 | * |
||
162 | * @param \DOMNode $node The XML node representing a whitespace |
||
163 | * @return string The corresponding whitespace value |
||
164 | */ |
||
165 | 2 | private function transformWhitespaceNode($node) |
|
172 | |||
173 | /** |
||
174 | * Returns the cell Numeric value from the given node. |
||
175 | * |
||
176 | * @param \DOMNode $node |
||
177 | * @return int|float The value associated with the cell |
||
178 | */ |
||
179 | 8 | protected function formatFloatCellValue($node) |
|
189 | |||
190 | /** |
||
191 | * Returns the cell Boolean value from the given node. |
||
192 | * |
||
193 | * @param \DOMNode $node |
||
194 | * @return bool The value associated with the cell |
||
195 | */ |
||
196 | 2 | protected function formatBooleanCellValue($node) |
|
202 | |||
203 | /** |
||
204 | * Returns the cell Date value from the given node. |
||
205 | * |
||
206 | * @param \DOMNode $node |
||
207 | * @throws InvalidValueException If the value is not a valid date |
||
208 | * @return \DateTime|string The value associated with the cell |
||
209 | */ |
||
210 | 3 | protected function formatDateCellValue($node) |
|
233 | |||
234 | /** |
||
235 | * Returns the cell Time value from the given node. |
||
236 | * |
||
237 | * @param \DOMNode $node |
||
238 | * @throws InvalidValueException If the value is not a valid time |
||
239 | * @return \DateInterval|string The value associated with the cell |
||
240 | */ |
||
241 | 3 | protected function formatTimeCellValue($node) |
|
264 | |||
265 | /** |
||
266 | * Returns the cell Currency value from the given node. |
||
267 | * |
||
268 | * @param \DOMNode $node |
||
269 | * @return string The value associated with the cell (e.g. "100 USD" or "9.99 EUR") |
||
270 | */ |
||
271 | 1 | protected function formatCurrencyCellValue($node) |
|
278 | |||
279 | /** |
||
280 | * Returns the cell Percentage value from the given node. |
||
281 | * |
||
282 | * @param \DOMNode $node |
||
283 | * @return int|float The value associated with the cell |
||
284 | */ |
||
285 | 1 | protected function formatPercentageCellValue($node) |
|
290 | } |
||
291 |