Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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       * @throws \PhpOffice\PhpSpreadsheet\Exception
  18       *
  19       * @return bool
  20       */
  21      public function bindValue(Cell $cell, $value)
  22      {
  23          // sanitize UTF-8 strings
  24          if (is_string($value)) {
  25              $value = StringHelper::sanitizeUTF8($value);
  26          } elseif (is_object($value)) {
  27              // Handle any objects that might be injected
  28              if ($value instanceof DateTimeInterface) {
  29                  $value = $value->format('Y-m-d H:i:s');
  30              } elseif (!($value instanceof RichText)) {
  31                  $value = (string) $value;
  32              }
  33          }
  34  
  35          // Set value explicit
  36          $cell->setValueExplicit($value, static::dataTypeForValue($value));
  37  
  38          // Done!
  39          return true;
  40      }
  41  
  42      /**
  43       * DataType for value.
  44       *
  45       * @param mixed $pValue
  46       *
  47       * @return string
  48       */
  49      public static function dataTypeForValue($pValue)
  50      {
  51          // Match the value against a few data types
  52          if ($pValue === null) {
  53              return DataType::TYPE_NULL;
  54          } elseif (is_float($pValue) || is_int($pValue)) {
  55              return DataType::TYPE_NUMERIC;
  56          } elseif (is_bool($pValue)) {
  57              return DataType::TYPE_BOOL;
  58          } elseif ($pValue === '') {
  59              return DataType::TYPE_STRING;
  60          } elseif ($pValue instanceof RichText) {
  61              return DataType::TYPE_INLINE;
  62          } elseif (is_string($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) {
  63              return DataType::TYPE_FORMULA;
  64          } elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
  65              $tValue = ltrim($pValue, '+-');
  66              if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
  67                  return DataType::TYPE_STRING;
  68              } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
  69                  return DataType::TYPE_STRING;
  70              }
  71  
  72              return DataType::TYPE_NUMERIC;
  73          } elseif (is_string($pValue)) {
  74              $errorCodes = DataType::getErrorCodes();
  75              if (isset($errorCodes[$pValue])) {
  76                  return DataType::TYPE_ERROR;
  77              }
  78          }
  79  
  80          return DataType::TYPE_STRING;
  81      }
  82  }