/lib/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php

https://github.com/markn86/moodle · PHP · 174 lines · 109 code · 26 blank · 39 comment · 20 complexity · 0db98a8a721ef4be730260a2293f2ad1 MD5 · raw file

  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Cell;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  5. use PhpOffice\PhpSpreadsheet\Shared\Date;
  6. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  7. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  8. class AdvancedValueBinder extends DefaultValueBinder implements IValueBinder
  9. {
  10. /**
  11. * Bind value to a cell.
  12. *
  13. * @param Cell $cell Cell to bind value to
  14. * @param mixed $value Value to bind in cell
  15. *
  16. * @return bool
  17. */
  18. public function bindValue(Cell $cell, $value = null)
  19. {
  20. // sanitize UTF-8 strings
  21. if (is_string($value)) {
  22. $value = StringHelper::sanitizeUTF8($value);
  23. }
  24. // Find out data type
  25. $dataType = parent::dataTypeForValue($value);
  26. // Style logic - strings
  27. if ($dataType === DataType::TYPE_STRING && !$value instanceof RichText) {
  28. // Test for booleans using locale-setting
  29. if ($value == Calculation::getTRUE()) {
  30. $cell->setValueExplicit(true, DataType::TYPE_BOOL);
  31. return true;
  32. } elseif ($value == Calculation::getFALSE()) {
  33. $cell->setValueExplicit(false, DataType::TYPE_BOOL);
  34. return true;
  35. }
  36. // Check for number in scientific format
  37. if (preg_match('/^' . Calculation::CALCULATION_REGEXP_NUMBER . '$/', $value)) {
  38. $cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC);
  39. return true;
  40. }
  41. // Check for fraction
  42. if (preg_match('/^([+-]?)\s*(\d+)\s?\/\s*(\d+)$/', $value, $matches)) {
  43. // Convert value to number
  44. $value = $matches[2] / $matches[3];
  45. if ($matches[1] == '-') {
  46. $value = 0 - $value;
  47. }
  48. $cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC);
  49. // Set style
  50. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  51. ->getNumberFormat()->setFormatCode('??/??');
  52. return true;
  53. } elseif (preg_match('/^([+-]?)(\d*) +(\d*)\s?\/\s*(\d*)$/', $value, $matches)) {
  54. // Convert value to number
  55. $value = $matches[2] + ($matches[3] / $matches[4]);
  56. if ($matches[1] == '-') {
  57. $value = 0 - $value;
  58. }
  59. $cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC);
  60. // Set style
  61. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  62. ->getNumberFormat()->setFormatCode('# ??/??');
  63. return true;
  64. }
  65. // Check for percentage
  66. if (preg_match('/^\-?\d*\.?\d*\s?\%$/', $value)) {
  67. // Convert value to number
  68. $value = (float) str_replace('%', '', $value) / 100;
  69. $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
  70. // Set style
  71. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  72. ->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_PERCENTAGE_00);
  73. return true;
  74. }
  75. // Check for currency
  76. $currencyCode = StringHelper::getCurrencyCode();
  77. $decimalSeparator = StringHelper::getDecimalSeparator();
  78. $thousandsSeparator = StringHelper::getThousandsSeparator();
  79. if (preg_match('/^' . preg_quote($currencyCode, '/') . ' *(\d{1,3}(' . preg_quote($thousandsSeparator, '/') . '\d{3})*|(\d+))(' . preg_quote($decimalSeparator, '/') . '\d{2})?$/', $value)) {
  80. // Convert value to number
  81. $value = (float) trim(str_replace([$currencyCode, $thousandsSeparator, $decimalSeparator], ['', '', '.'], $value));
  82. $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
  83. // Set style
  84. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  85. ->getNumberFormat()->setFormatCode(
  86. str_replace('$', $currencyCode, NumberFormat::FORMAT_CURRENCY_USD_SIMPLE)
  87. );
  88. return true;
  89. } elseif (preg_match('/^\$ *(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) {
  90. // Convert value to number
  91. $value = (float) trim(str_replace(['$', ','], '', $value));
  92. $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
  93. // Set style
  94. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  95. ->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  96. return true;
  97. }
  98. // Check for time without seconds e.g. '9:45', '09:45'
  99. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) {
  100. // Convert value to number
  101. [$h, $m] = explode(':', $value);
  102. $days = $h / 24 + $m / 1440;
  103. $cell->setValueExplicit($days, DataType::TYPE_NUMERIC);
  104. // Set style
  105. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  106. ->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_TIME3);
  107. return true;
  108. }
  109. // Check for time with seconds '9:45:59', '09:45:59'
  110. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) {
  111. // Convert value to number
  112. [$h, $m, $s] = explode(':', $value);
  113. $days = $h / 24 + $m / 1440 + $s / 86400;
  114. // Convert value to number
  115. $cell->setValueExplicit($days, DataType::TYPE_NUMERIC);
  116. // Set style
  117. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  118. ->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_TIME4);
  119. return true;
  120. }
  121. // Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10'
  122. if (($d = Date::stringToExcel($value)) !== false) {
  123. // Convert value to number
  124. $cell->setValueExplicit($d, DataType::TYPE_NUMERIC);
  125. // Determine style. Either there is a time part or not. Look for ':'
  126. if (strpos($value, ':') !== false) {
  127. $formatCode = 'yyyy-mm-dd h:mm';
  128. } else {
  129. $formatCode = 'yyyy-mm-dd';
  130. }
  131. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  132. ->getNumberFormat()->setFormatCode($formatCode);
  133. return true;
  134. }
  135. // Check for newline character "\n"
  136. if (strpos($value, "\n") !== false) {
  137. $value = StringHelper::sanitizeUTF8($value);
  138. $cell->setValueExplicit($value, DataType::TYPE_STRING);
  139. // Set style
  140. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  141. ->getAlignment()->setWrapText(true);
  142. return true;
  143. }
  144. }
  145. // Not bound yet? Use parent...
  146. return parent::bindValue($cell, $value);
  147. }
  148. }