Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Cell;
   4  
   5  use DateTimeInterface;
   6  use PhpOffice\PhpSpreadsheet\RichText\RichText;
   7  use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
   8  
   9  class DefaultValueBinder implements IValueBinder
  10  {
  11      /**
  12       * Bind value to a cell.
  13       *
  14       * @param Cell $cell Cell to bind value to
  15       * @param mixed $value Value to bind in cell
  16       *
  17       * @return bool
  18       */
  19      public function bindValue(Cell $cell, $value)
  20      {
  21          // sanitize UTF-8 strings
  22          if (is_string($value)) {
  23              $value = StringHelper::sanitizeUTF8($value);
  24          } elseif (is_object($value)) {
  25              // Handle any objects that might be injected
  26              if ($value instanceof DateTimeInterface) {
  27                  $value = $value->format('Y-m-d H:i:s');
  28              } elseif (!($value instanceof RichText)) {
  29                  // Attempt to cast any unexpected objects to string
  30                  $value = (string) $value;
  31              }
  32          }
  33  
  34          // Set value explicit
  35          $cell->setValueExplicit($value, static::dataTypeForValue($value));
  36  
  37          // Done!
  38          return true;
  39      }
  40  
  41      /**
  42       * DataType for value.
  43       *
  44       * @param mixed $value
  45       *
  46       * @return string
  47       */
  48      public static function dataTypeForValue($value)
  49      {
  50          // Match the value against a few data types
  51          if ($value === null) {
  52              return DataType::TYPE_NULL;
  53          } elseif (is_float($value) || is_int($value)) {
  54              return DataType::TYPE_NUMERIC;
  55          } elseif (is_bool($value)) {
  56              return DataType::TYPE_BOOL;
  57          } elseif ($value === '') {
  58              return DataType::TYPE_STRING;
  59          } elseif ($value instanceof RichText) {
  60              return DataType::TYPE_INLINE;
  61          } elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=') {
  62              return DataType::TYPE_FORMULA;
  63          } elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $value)) {
  64              $tValue = ltrim($value, '+-');
  65              if (is_string($value) && strlen($tValue) > 1 && $tValue[0] === '0' && $tValue[1] !== '.') {
  66                  return DataType::TYPE_STRING;
  67              } elseif ((strpos($value, '.') === false) && ($value > PHP_INT_MAX)) {
  68                  return DataType::TYPE_STRING;
  69              } elseif (!is_numeric($value)) {
  70                  return DataType::TYPE_STRING;
  71              }
  72  
  73              return DataType::TYPE_NUMERIC;
  74          } elseif (is_string($value)) {
  75              $errorCodes = DataType::getErrorCodes();
  76              if (isset($errorCodes[$value])) {
  77                  return DataType::TYPE_ERROR;
  78              }
  79          }
  80  
  81          return DataType::TYPE_STRING;
  82      }
  83  }