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_S = 'text:s'; |
||
26 | const XML_NODE_A = 'text:a'; |
||
27 | const XML_NODE_SPAN = 'text:span'; |
||
28 | |||
29 | /** Definition of XML attribute used to parse data */ |
||
30 | const XML_ATTRIBUTE_TYPE = 'office:value-type'; |
||
31 | const XML_ATTRIBUTE_VALUE = 'office:value'; |
||
32 | const XML_ATTRIBUTE_BOOLEAN_VALUE = 'office:boolean-value'; |
||
33 | const XML_ATTRIBUTE_DATE_VALUE = 'office:date-value'; |
||
34 | const XML_ATTRIBUTE_TIME_VALUE = 'office:time-value'; |
||
35 | const XML_ATTRIBUTE_CURRENCY = 'office:currency'; |
||
36 | const XML_ATTRIBUTE_C = 'text:c'; |
||
37 | |||
38 | /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ |
||
39 | protected $shouldFormatDates; |
||
40 | |||
41 | /** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */ |
||
42 | protected $escaper; |
||
43 | |||
44 | /** |
||
45 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
46 | */ |
||
47 | public function __construct($shouldFormatDates) |
||
54 | |||
55 | /** |
||
56 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
57 | * @see http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13 |
||
58 | * |
||
59 | * @param \DOMNode $node |
||
60 | * @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error |
||
61 | */ |
||
62 | public function extractAndFormatNodeValue($node) |
||
86 | |||
87 | /** |
||
88 | * Returns the cell String value. |
||
89 | * |
||
90 | * @param \DOMNode $node |
||
91 | * @return string The value associated with the cell |
||
92 | */ |
||
93 | protected function formatStringCellValue($node) |
||
120 | |||
121 | /** |
||
122 | * Returns the cell Numeric value from the given node. |
||
123 | * |
||
124 | * @param \DOMNode $node |
||
125 | * @return int|float The value associated with the cell |
||
126 | */ |
||
127 | protected function formatFloatCellValue($node) |
||
135 | |||
136 | /** |
||
137 | * Returns the cell Boolean value from the given node. |
||
138 | * |
||
139 | * @param \DOMNode $node |
||
140 | * @return bool The value associated with the cell |
||
141 | */ |
||
142 | protected function formatBooleanCellValue($node) |
||
149 | |||
150 | /** |
||
151 | * Returns the cell Date value from the given node. |
||
152 | * |
||
153 | * @param \DOMNode $node |
||
154 | * @return \DateTime|string|null The value associated with the cell or NULL if invalid date value |
||
155 | */ |
||
156 | protected function formatDateCellValue($node) |
||
177 | |||
178 | /** |
||
179 | * Returns the cell Time value from the given node. |
||
180 | * |
||
181 | * @param \DOMNode $node |
||
182 | * @return \DateInterval|string|null The value associated with the cell or NULL if invalid time value |
||
183 | */ |
||
184 | protected function formatTimeCellValue($node) |
||
205 | |||
206 | /** |
||
207 | * Returns the cell Currency value from the given node. |
||
208 | * |
||
209 | * @param \DOMNode $node |
||
210 | * @return string The value associated with the cell (e.g. "100 USD" or "9.99 EUR") |
||
211 | */ |
||
212 | protected function formatCurrencyCellValue($node) |
||
219 | |||
220 | /** |
||
221 | * Returns the cell Percentage value from the given node. |
||
222 | * |
||
223 | * @param \DOMNode $node |
||
224 | * @return int|float The value associated with the cell |
||
225 | */ |
||
226 | protected function formatPercentageCellValue($node) |
||
231 | } |
||
232 |