Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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