ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NumberFormat.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class NumberFormat extends Supervisor
6 {
7  // Pre-defined formats
8  const FORMAT_GENERAL = 'General';
9 
10  const FORMAT_TEXT = '@';
11 
12  const FORMAT_NUMBER = '0';
13  const FORMAT_NUMBER_00 = '0.00';
14  const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
15  const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
16 
17  const FORMAT_PERCENTAGE = '0%';
18  const FORMAT_PERCENTAGE_00 = '0.00%';
19 
20  const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
21  const FORMAT_DATE_YYYYMMDD = 'yyyy-mm-dd';
22  const FORMAT_DATE_DDMMYYYY = 'dd/mm/yyyy';
23  const FORMAT_DATE_DMYSLASH = 'd/m/yy';
24  const FORMAT_DATE_DMYMINUS = 'd-m-yy';
25  const FORMAT_DATE_DMMINUS = 'd-m';
26  const FORMAT_DATE_MYMINUS = 'm-yy';
27  const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
28  const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
29  const FORMAT_DATE_XLSX16 = 'd-mmm';
30  const FORMAT_DATE_XLSX17 = 'mmm-yy';
31  const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
32  const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
33  const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
34  const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
35  const FORMAT_DATE_TIME3 = 'h:mm';
36  const FORMAT_DATE_TIME4 = 'h:mm:ss';
37  const FORMAT_DATE_TIME5 = 'mm:ss';
38  const FORMAT_DATE_TIME6 = 'h:mm:ss';
39  const FORMAT_DATE_TIME7 = 'i:s.S';
40  const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
41  const FORMAT_DATE_YYYYMMDDSLASH = 'yyyy/mm/dd;@';
42 
43  const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
44  const FORMAT_CURRENCY_USD = '$#,##0_-';
45  const FORMAT_CURRENCY_EUR_SIMPLE = '#,##0.00_-"€"';
46  const FORMAT_CURRENCY_EUR = '#,##0_-"€"';
47  const FORMAT_ACCOUNTING_USD = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
48  const FORMAT_ACCOUNTING_EUR = '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)';
49 
55  protected static $builtInFormats;
56 
62  protected static $flippedBuiltInFormats;
63 
69  protected $formatCode = self::FORMAT_GENERAL;
70 
76  protected $builtInFormatCode = 0;
77 
88  public function __construct($isSupervisor = false, $isConditional = false)
89  {
90  // Supervisor?
91  parent::__construct($isSupervisor);
92 
93  if ($isConditional) {
94  $this->formatCode = null;
95  $this->builtInFormatCode = false;
96  }
97  }
98 
105  public function getSharedComponent()
106  {
107  return $this->parent->getSharedComponent()->getNumberFormat();
108  }
109 
117  public function getStyleArray($array)
118  {
119  return ['numberFormat' => $array];
120  }
121 
137  public function applyFromArray(array $pStyles)
138  {
139  if ($this->isSupervisor) {
140  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
141  } else {
142  if (isset($pStyles['formatCode'])) {
143  $this->setFormatCode($pStyles['formatCode']);
144  }
145  }
146 
147  return $this;
148  }
149 
155  public function getFormatCode()
156  {
157  if ($this->isSupervisor) {
158  return $this->getSharedComponent()->getFormatCode();
159  }
160  if ($this->builtInFormatCode !== false) {
161  return self::builtInFormatCode($this->builtInFormatCode);
162  }
163 
164  return $this->formatCode;
165  }
166 
174  public function setFormatCode($pValue)
175  {
176  if ($pValue == '') {
177  $pValue = self::FORMAT_GENERAL;
178  }
179  if ($this->isSupervisor) {
180  $styleArray = $this->getStyleArray(['formatCode' => $pValue]);
181  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
182  } else {
183  $this->formatCode = $pValue;
184  $this->builtInFormatCode = self::builtInFormatCodeIndex($pValue);
185  }
186 
187  return $this;
188  }
189 
195  public function getBuiltInFormatCode()
196  {
197  if ($this->isSupervisor) {
198  return $this->getSharedComponent()->getBuiltInFormatCode();
199  }
200 
202  }
203 
211  public function setBuiltInFormatCode($pValue)
212  {
213  if ($this->isSupervisor) {
214  $styleArray = $this->getStyleArray(['formatCode' => self::builtInFormatCode($pValue)]);
215  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
216  } else {
217  $this->builtInFormatCode = $pValue;
218  $this->formatCode = self::builtInFormatCode($pValue);
219  }
220 
221  return $this;
222  }
223 
227  private static function fillBuiltInFormatCodes(): void
228  {
229  // [MS-OI29500: Microsoft Office Implementation Information for ISO/IEC-29500 Standard Compliance]
230  // 18.8.30. numFmt (Number Format)
231  //
232  // The ECMA standard defines built-in format IDs
233  // 14: "mm-dd-yy"
234  // 22: "m/d/yy h:mm"
235  // 37: "#,##0 ;(#,##0)"
236  // 38: "#,##0 ;[Red](#,##0)"
237  // 39: "#,##0.00;(#,##0.00)"
238  // 40: "#,##0.00;[Red](#,##0.00)"
239  // 47: "mmss.0"
240  // KOR fmt 55: "yyyy-mm-dd"
241  // Excel defines built-in format IDs
242  // 14: "m/d/yyyy"
243  // 22: "m/d/yyyy h:mm"
244  // 37: "#,##0_);(#,##0)"
245  // 38: "#,##0_);[Red](#,##0)"
246  // 39: "#,##0.00_);(#,##0.00)"
247  // 40: "#,##0.00_);[Red](#,##0.00)"
248  // 47: "mm:ss.0"
249  // KOR fmt 55: "yyyy/mm/dd"
250 
251  // Built-in format codes
252  if (self::$builtInFormats === null) {
253  self::$builtInFormats = [];
254 
255  // General
256  self::$builtInFormats[0] = self::FORMAT_GENERAL;
257  self::$builtInFormats[1] = '0';
258  self::$builtInFormats[2] = '0.00';
259  self::$builtInFormats[3] = '#,##0';
260  self::$builtInFormats[4] = '#,##0.00';
261 
262  self::$builtInFormats[9] = '0%';
263  self::$builtInFormats[10] = '0.00%';
264  self::$builtInFormats[11] = '0.00E+00';
265  self::$builtInFormats[12] = '# ?/?';
266  self::$builtInFormats[13] = '# ??/??';
267  self::$builtInFormats[14] = 'm/d/yyyy'; // Despite ECMA 'mm-dd-yy';
268  self::$builtInFormats[15] = 'd-mmm-yy';
269  self::$builtInFormats[16] = 'd-mmm';
270  self::$builtInFormats[17] = 'mmm-yy';
271  self::$builtInFormats[18] = 'h:mm AM/PM';
272  self::$builtInFormats[19] = 'h:mm:ss AM/PM';
273  self::$builtInFormats[20] = 'h:mm';
274  self::$builtInFormats[21] = 'h:mm:ss';
275  self::$builtInFormats[22] = 'm/d/yyyy h:mm'; // Despite ECMA 'm/d/yy h:mm';
276 
277  self::$builtInFormats[37] = '#,##0_);(#,##0)'; // Despite ECMA '#,##0 ;(#,##0)';
278  self::$builtInFormats[38] = '#,##0_);[Red](#,##0)'; // Despite ECMA '#,##0 ;[Red](#,##0)';
279  self::$builtInFormats[39] = '#,##0.00_);(#,##0.00)'; // Despite ECMA '#,##0.00;(#,##0.00)';
280  self::$builtInFormats[40] = '#,##0.00_);[Red](#,##0.00)'; // Despite ECMA '#,##0.00;[Red](#,##0.00)';
281 
282  self::$builtInFormats[44] = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
283  self::$builtInFormats[45] = 'mm:ss';
284  self::$builtInFormats[46] = '[h]:mm:ss';
285  self::$builtInFormats[47] = 'mm:ss.0'; // Despite ECMA 'mmss.0';
286  self::$builtInFormats[48] = '##0.0E+0';
287  self::$builtInFormats[49] = '@';
288 
289  // CHT
290  self::$builtInFormats[27] = '[$-404]e/m/d';
291  self::$builtInFormats[30] = 'm/d/yy';
292  self::$builtInFormats[36] = '[$-404]e/m/d';
293  self::$builtInFormats[50] = '[$-404]e/m/d';
294  self::$builtInFormats[57] = '[$-404]e/m/d';
295 
296  // THA
297  self::$builtInFormats[59] = 't0';
298  self::$builtInFormats[60] = 't0.00';
299  self::$builtInFormats[61] = 't#,##0';
300  self::$builtInFormats[62] = 't#,##0.00';
301  self::$builtInFormats[67] = 't0%';
302  self::$builtInFormats[68] = 't0.00%';
303  self::$builtInFormats[69] = 't# ?/?';
304  self::$builtInFormats[70] = 't# ??/??';
305 
306  // JPN
307  self::$builtInFormats[28] = '[$-411]ggge"年"m"月"d"日"';
308  self::$builtInFormats[29] = '[$-411]ggge"年"m"月"d"日"';
309  self::$builtInFormats[31] = 'yyyy"年"m"月"d"日"';
310  self::$builtInFormats[32] = 'h"時"mm"分"';
311  self::$builtInFormats[33] = 'h"時"mm"分"ss"秒"';
312  self::$builtInFormats[34] = 'yyyy"年"m"月"';
313  self::$builtInFormats[35] = 'm"月"d"日"';
314  self::$builtInFormats[51] = '[$-411]ggge"年"m"月"d"日"';
315  self::$builtInFormats[52] = 'yyyy"年"m"月"';
316  self::$builtInFormats[53] = 'm"月"d"日"';
317  self::$builtInFormats[54] = '[$-411]ggge"年"m"月"d"日"';
318  self::$builtInFormats[55] = 'yyyy"年"m"月"';
319  self::$builtInFormats[56] = 'm"月"d"日"';
320  self::$builtInFormats[58] = '[$-411]ggge"年"m"月"d"日"';
321 
322  // Flip array (for faster lookups)
323  self::$flippedBuiltInFormats = array_flip(self::$builtInFormats);
324  }
325  }
326 
334  public static function builtInFormatCode($pIndex)
335  {
336  // Clean parameter
337  $pIndex = (int) $pIndex;
338 
339  // Ensure built-in format codes are available
340  self::fillBuiltInFormatCodes();
341 
342  // Lookup format code
343  if (isset(self::$builtInFormats[$pIndex])) {
344  return self::$builtInFormats[$pIndex];
345  }
346 
347  return '';
348  }
349 
357  public static function builtInFormatCodeIndex($formatCode)
358  {
359  // Ensure built-in format codes are available
360  self::fillBuiltInFormatCodes();
361 
362  // Lookup format code
363  if (array_key_exists($formatCode, self::$flippedBuiltInFormats)) {
364  return self::$flippedBuiltInFormats[$formatCode];
365  }
366 
367  return false;
368  }
369 
375  public function getHashCode()
376  {
377  if ($this->isSupervisor) {
378  return $this->getSharedComponent()->getHashCode();
379  }
380 
381  return md5(
382  $this->formatCode .
383  $this->builtInFormatCode .
384  __CLASS__
385  );
386  }
387 
397  public static function toFormattedString($value, $format, $callBack = null)
398  {
399  return NumberFormat\Formatter::toFormattedString($value, $format, $callBack);
400  }
401 
402  protected function exportArray1(): array
403  {
404  $exportedArray = [];
405  $this->exportArray2($exportedArray, 'formatCode', $this->getFormatCode());
406 
407  return $exportedArray;
408  }
409 }
$format
Definition: metadata.php:141
applyFromArray(array $pStyles)
Apply styles from array.
__construct($isSupervisor=false, $isConditional=false)
Create a new NumberFormat.
static builtInFormatCodeIndex($formatCode)
Get built-in format code index.
setBuiltInFormatCode($pValue)
Set Built-In Format Code.
static toFormattedString($value, $format, $callBack=null)
Convert a value in a pre-defined format to a PHP string.
Definition: Formatter.php:102
static toFormattedString($value, $format, $callBack=null)
Convert a value in a pre-defined format to a PHP string.
getBuiltInFormatCode()
Get Built-In Format Code.
static builtInFormatCode($pIndex)
Get built-in format code.
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:87
static fillBuiltInFormatCodes()
Fill built-in format codes.
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
getStyleArray($array)
Build style array from subcomponents.
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:76
exportArray2(array &$exportedArray, string $index, $objOrValue)
Populate array from exportArray1.
Definition: Supervisor.php:150